From a8bf78eb241e2c8770e3ab7fb5dc579766a4ea11 Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Fri, 12 Apr 2024 23:52:22 +0700 Subject: [PATCH] Adds tests --- .github/workflows/ci.yml | 2 ++ Cargo.toml | 3 +++ src/lib.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54073e9..03889c3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,3 +17,5 @@ jobs: run: cargo fmt --check - name: Lint run: cargo clippy -- -D warnings + - name: Run tests + run: cargo test --workspace diff --git a/Cargo.toml b/Cargo.toml index eb52397..b75acb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ repository = "https://github.com/ultimaweapon/erdp" edition = "2021" [dependencies] + +[dev-dependencies] +thiserror = "1.0.58" diff --git a/src/lib.rs b/src/lib.rs index 00ac262..d153ac2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,3 +35,33 @@ impl<'a> std::fmt::Display for Display<'a> { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use thiserror::Error; + + #[test] + fn single() { + let e = TestError::Single; + + assert_eq!(e.display().to_string(), "an error without nested errors"); + } + + #[test] + fn nested() { + let e = std::io::Error::from(std::io::ErrorKind::NotFound); + let e = TestError::Nested(e); + + assert_eq!(e.display().to_string(), "nested error -> entity not found"); + } + + #[derive(Debug, Error)] + enum TestError { + #[error("an error without nested errors")] + Single, + + #[error("nested error")] + Nested(#[source] std::io::Error), + } +}