From da277eaccc062f1e46204d63d8294640a67108ee Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 8 Jul 2024 15:21:21 -0300 Subject: [PATCH] Playing with Results in tests --- resultteststest/Cargo.lock | 7 ++++++ resultteststest/Cargo.toml | 6 +++++ resultteststest/README.md | 3 +++ resultteststest/src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 resultteststest/Cargo.lock create mode 100644 resultteststest/Cargo.toml create mode 100644 resultteststest/README.md create mode 100644 resultteststest/src/lib.rs diff --git a/resultteststest/Cargo.lock b/resultteststest/Cargo.lock new file mode 100644 index 0000000..d1f8dd4 --- /dev/null +++ b/resultteststest/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "resultteststest" +version = "0.1.0" diff --git a/resultteststest/Cargo.toml b/resultteststest/Cargo.toml new file mode 100644 index 0000000..942deef --- /dev/null +++ b/resultteststest/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "resultteststest" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/resultteststest/README.md b/resultteststest/README.md new file mode 100644 index 0000000..5b8fa7a --- /dev/null +++ b/resultteststest/README.md @@ -0,0 +1,3 @@ +# ResultTestsTest + +Checking how Cargo deals with Results in tests diff --git a/resultteststest/src/lib.rs b/resultteststest/src/lib.rs new file mode 100644 index 0000000..ff5ca99 --- /dev/null +++ b/resultteststest/src/lib.rs @@ -0,0 +1,46 @@ +fn not_zero(num: usize) -> Result<(), ()> { + if num == 0 { + Err(()) + } else { + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn with_result_ok() -> Result<(), ()> { + Ok(()) + } + + #[test] + fn with_result_err() -> Result<(), ()> { + Err(()) + } + + #[test] + fn nested() -> Result<(), ()> { + not_zero(1)?; + Ok(()) + } + + #[test] + fn nested2() -> Result<(), ()> { + not_zero(0)?; + Ok(()) + } + + #[test] + fn nested3() -> Result<(), ()> { + let result = (|| { + not_zero(2)?; + Ok(()) + })(); + + // do something else, like cleaning up the testing workspace + + result + } +}