ch11.02 done

This commit is contained in:
Rowan Torbitzky-Lane 2025-03-29 11:55:32 -05:00
parent b433eb19d2
commit ba917801b6
3 changed files with 86 additions and 0 deletions

7
ch11/running-tests/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "running-tests"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "running-tests"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,73 @@
// Can control which command line arguments go to
// cargo test and which go to the actually binary itself
// with the `--`. Before the `--` the args go to cargo test
// then after go to the binary
//
// If want the fine-grained control over the number of
// threads, use the --test-threads option.
// $ cargo test -- --test-threads=1
fn prints_and_returns_10(a: i32) -> i32 {
println!("I got the value {a}");
10
}
pub fn add_two(a: usize) -> usize {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
// by default tests capture stdout and wont print anything
// if want to see output, use the `--show-output flag`
// $ cargo test -- --show-output
#[test]
fn this_test_will_pass() {
let value = prints_and_returns_10(4);
assert_eq!(value, 10);
}
// #[test]
// fn this_test_will_fail() {
// let value = prints_and_returns_10(4);
// assert_eq!(value, 5);
// }
// Can pass a string to `cargo test` to test all
// tests that contain said string.
// $ cargo test add
// will test all functions that have `add` in the
// name.
#[test]
fn add_two_and_two() {
let result = add_two(2);
assert_eq!(result, 4);
}
#[test]
fn add_three_and_two() {
let result = add_two(3);
assert_eq!(result, 5);
}
// Can pass a single name to `cargo test` to test only
// a single function.
// $ cargo test one_hundred
#[test]
fn one_hundred() {
let result = add_two(100);
assert_eq!(result, 102);
}
// Can tell rust to ignore specific tests unless requested
// Can choose to only run the ignored code with the
// `--ignored` flag
// $ cargo test -- --ignored
#[test]
#[ignore]
fn expensive_test() {
assert!(true);
}
}