unit test part of ch11.3

This commit is contained in:
Rowan Torbitzky-Lane 2025-03-29 12:20:52 -05:00
parent ba917801b6
commit 5015699e0d
3 changed files with 50 additions and 0 deletions

7
ch11/test-organization/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 = "test-organization"
version = "0.1.0"

View File

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

View File

@ -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);
}
}