You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-07-05 06:00:30 +02:00
Be more consistent about tests vs. main (#2644)
The content slides all use `fn main`, with the exception of the testing segment. But with this change, where it makes sense exercises use tests instead, and not both tests and `fn main`. A small change in `book.js` supports running tests when a code sample does not have `fn main` but does have `#[test]`, so these work naturally. Fixes #1581.
This commit is contained in:
committed by
GitHub
parent
699c5137c7
commit
44a79741ff
@ -8,6 +8,6 @@ publish = false
|
||||
anyhow = "*"
|
||||
thiserror = "*"
|
||||
|
||||
[[bin]]
|
||||
[lib]
|
||||
name = "parser"
|
||||
path = "exercise.rs"
|
||||
|
@ -88,25 +88,30 @@ fn eval(e: Expression) -> Result<i64, DivideByZeroError> {
|
||||
// ANCHOR_END: solution
|
||||
|
||||
// ANCHOR: tests
|
||||
#[test]
|
||||
fn test_error() {
|
||||
assert_eq!(
|
||||
eval(Expression::Op {
|
||||
op: Operation::Div,
|
||||
left: Box::new(Expression::Value(99)),
|
||||
right: Box::new(Expression::Value(0)),
|
||||
}),
|
||||
Err(DivideByZeroError)
|
||||
);
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
fn main() {
|
||||
let expr = Expression::Op {
|
||||
op: Operation::Sub,
|
||||
left: Box::new(Expression::Value(20)),
|
||||
right: Box::new(Expression::Value(10)),
|
||||
};
|
||||
println!("expr: {expr:?}");
|
||||
println!("result: {:?}", eval(expr));
|
||||
#[test]
|
||||
fn test_error() {
|
||||
assert_eq!(
|
||||
eval(Expression::Op {
|
||||
op: Operation::Div,
|
||||
left: Box::new(Expression::Value(99)),
|
||||
right: Box::new(Expression::Value(0)),
|
||||
}),
|
||||
Err(DivideByZeroError)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ok() {
|
||||
let expr = Expression::Op {
|
||||
op: Operation::Sub,
|
||||
left: Box::new(Expression::Value(20)),
|
||||
right: Box::new(Expression::Value(10)),
|
||||
};
|
||||
assert_eq!(eval(expr), Ok(10));
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: tests
|
||||
|
Reference in New Issue
Block a user