From 5015699e0db879adaef18ca0bf48b16f6c26b558 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 29 Mar 2025 12:20:52 -0500 Subject: [PATCH] unit test part of ch11.3 --- ch11/test-organization/Cargo.lock | 7 ++++++ ch11/test-organization/Cargo.toml | 6 +++++ ch11/test-organization/src/lib.rs | 37 +++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 ch11/test-organization/Cargo.lock create mode 100644 ch11/test-organization/Cargo.toml create mode 100644 ch11/test-organization/src/lib.rs diff --git a/ch11/test-organization/Cargo.lock b/ch11/test-organization/Cargo.lock new file mode 100644 index 0000000..84da2bd --- /dev/null +++ b/ch11/test-organization/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "test-organization" +version = "0.1.0" diff --git a/ch11/test-organization/Cargo.toml b/ch11/test-organization/Cargo.toml new file mode 100644 index 0000000..a0b69d9 --- /dev/null +++ b/ch11/test-organization/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "test-organization" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch11/test-organization/src/lib.rs b/ch11/test-organization/src/lib.rs new file mode 100644 index 0000000..c1cc7d4 --- /dev/null +++ b/ch11/test-organization/src/lib.rs @@ -0,0 +1,37 @@ +// Unit tests are small and more focused, testing one module in isolation +// Integration tests are entirely external and use code in the same +// way that any other external code would, using only the public +// interface and potentially exercising mutliple modules per test +// +// For unit tests, convention is to create a module in each file +// named tests for the test functions and annotate the module +// with cfg(test). Integration tests don't get this +// +// For the integration tests section, go back to the adder folder +// https://rust-book.cs.brown.edu/ch11-03-test-organization.html#integration-tests + +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +// Can test internal functions +fn internal_adder(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } + + #[test] + fn internal() { + let result = internal_adder(2, 2); + assert_eq!(result, 4); + } +}