From ba917801b6ed2415c06ea764f9157428f52eb591 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 29 Mar 2025 11:55:32 -0500 Subject: [PATCH] ch11.02 done --- ch11/running-tests/Cargo.lock | 7 ++++ ch11/running-tests/Cargo.toml | 6 +++ ch11/running-tests/src/lib.rs | 73 +++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 ch11/running-tests/Cargo.lock create mode 100644 ch11/running-tests/Cargo.toml create mode 100644 ch11/running-tests/src/lib.rs diff --git a/ch11/running-tests/Cargo.lock b/ch11/running-tests/Cargo.lock new file mode 100644 index 0000000..a91c2a8 --- /dev/null +++ b/ch11/running-tests/Cargo.lock @@ -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" diff --git a/ch11/running-tests/Cargo.toml b/ch11/running-tests/Cargo.toml new file mode 100644 index 0000000..43aeb15 --- /dev/null +++ b/ch11/running-tests/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "running-tests" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch11/running-tests/src/lib.rs b/ch11/running-tests/src/lib.rs new file mode 100644 index 0000000..2acdc58 --- /dev/null +++ b/ch11/running-tests/src/lib.rs @@ -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); + } +}