#![doc = include_str!("../README.md")]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![allow(clippy::test_attr_in_doctest)]
#[derive(Debug)]
pub enum TestError {}
impl<T: std::fmt::Display> From<T> for TestError {
#[track_caller] fn from(error: T) -> Self {
panic!("error: {} - {:#}", std::any::type_name::<T>(), error);
}
}
pub type TestResult<T = ()> = std::result::Result<T, TestError>;
pub fn ok<T>(value: T) -> TestResult<T> {
Ok(value)
}
#[cfg(test)]
mod tests {
use anyhow::Context as _;
use super::*;
#[test]
#[ignore] fn compilation_works() -> TestResult {
std::fs::File::open("this-file-does-not-exist")?;
Ok(())
}
fn test_fn() -> TestResult<String> {
let string = String::from_utf8(vec![0, 159, 146, 150])?;
Ok(string)
}
#[test]
fn check_if_panics() -> TestResult {
let result = std::panic::catch_unwind(|| {
let _ = test_fn();
});
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(
Some(
&"error: alloc::string::FromUtf8Error - invalid utf-8 sequence of 1 bytes from index 1"
.to_string()
),
err.downcast_ref::<String>()
);
Ok(())
}
fn anyhow_a() -> anyhow::Result<String> {
let string = String::from_utf8(vec![0, 159, 146, 150])?;
Ok(string)
}
fn anyhow_b() -> anyhow::Result<String> {
let file = anyhow_a().context("Parsing a string")?;
Ok(file)
}
fn anyhow_c() -> TestResult<String> {
let file = anyhow_b()?;
Ok(file)
}
#[test]
fn check_if_anyhow_panics() -> TestResult {
let result = std::panic::catch_unwind(|| {
let _ = anyhow_c();
});
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(
Some(
&"error: anyhow::Error - Parsing a string: invalid utf-8 sequence of 1 bytes from index 1"
.to_string()
),
err.downcast_ref::<String>()
);
Ok(())
}
}