From 968c83983aebe60f4c2931262186641a59bb6434 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Fri, 28 Jul 2023 15:04:29 +0200 Subject: [PATCH] fix: re-add the basic doc test It got lost with the `README.md` conversion. Signed-off-by: Harald Hoyer --- tests/test_basic.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/test_basic.rs diff --git a/tests/test_basic.rs b/tests/test_basic.rs new file mode 100644 index 0000000..ac5412c --- /dev/null +++ b/tests/test_basic.rs @@ -0,0 +1,34 @@ +use chainerror::prelude::v1::*; + +#[test] +fn test_basic() { + use std::path::PathBuf; + type BoxedError = Box; + fn read_config_file(path: PathBuf) -> Result<(), BoxedError> { + // do stuff, return other errors + let _buf = std::fs::read_to_string(&path).context(format!("Reading file: {:?}", &path))?; + // do stuff, return other errors + Ok(()) + } + fn process_config_file() -> Result<(), BoxedError> { + // do stuff, return other errors + read_config_file("_non_existent.txt".into()).context("read the config file")?; + // do stuff, return other errors + Ok(()) + } + + if let Err(e) = process_config_file() { + let os_notfound_error = std::io::Error::from_raw_os_error(2); + eprintln!("Error:\n{:?}", e); + let s = format!("{:?}", e); + let lines = s.lines().collect::>(); + assert_eq!(lines.len(), 5); + assert!(lines[0].starts_with("tests/test_basic.rs:")); + assert_eq!(lines[1], "Caused by:"); + assert!(lines[2].starts_with("tests/test_basic.rs:")); + assert_eq!(lines[3], "Caused by:"); + assert_eq!(lines[4], format!("{:?}", os_notfound_error)); + } else { + panic!(); + } +}