diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/failure/tests | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/failure/tests')
-rw-r--r-- | third_party/rust/failure/tests/basic_fail.rs | 21 | ||||
-rw-r--r-- | third_party/rust/failure/tests/fail_compat.rs | 35 | ||||
-rw-r--r-- | third_party/rust/failure/tests/macro_trailing_comma.rs | 67 |
3 files changed, 123 insertions, 0 deletions
diff --git a/third_party/rust/failure/tests/basic_fail.rs b/third_party/rust/failure/tests/basic_fail.rs new file mode 100644 index 0000000000..574886db7e --- /dev/null +++ b/third_party/rust/failure/tests/basic_fail.rs @@ -0,0 +1,21 @@ +#[macro_use] +extern crate failure; + +use failure::Fail; + +#[test] +fn test_name() { + #[derive(Fail, Debug)] + #[fail(display = "my error")] + struct MyError; + + let err = MyError; + + assert_eq!(err.to_string(), "my error"); + assert_eq!(err.name(), Some("basic_fail::MyError")); + + let ctx = err.context("whatever"); + + assert_eq!(ctx.to_string(), "whatever"); + assert_eq!(ctx.name(), Some("basic_fail::MyError")); +}
\ No newline at end of file diff --git a/third_party/rust/failure/tests/fail_compat.rs b/third_party/rust/failure/tests/fail_compat.rs new file mode 100644 index 0000000000..81f84be76a --- /dev/null +++ b/third_party/rust/failure/tests/fail_compat.rs @@ -0,0 +1,35 @@ +#[macro_use] +extern crate failure; + +use failure::Fail; + +fn return_failure() -> Result<(), failure::Error> { + #[derive(Fail, Debug)] + #[fail(display = "my error")] + struct MyError; + + let err = MyError; + Err(err.into()) +} + +fn return_error() -> Result<(), Box<dyn std::error::Error>> { + return_failure()?; + Ok(()) +} + +fn return_error_send_sync() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { + return_failure()?; + Ok(()) +} + +#[test] +fn smoke_default_compat() { + let err = return_error(); + assert!(err.is_err()); +} + +#[test] +fn smoke_compat_send_sync() { + let err = return_error_send_sync(); + assert!(err.is_err()); +} diff --git a/third_party/rust/failure/tests/macro_trailing_comma.rs b/third_party/rust/failure/tests/macro_trailing_comma.rs new file mode 100644 index 0000000000..012d006170 --- /dev/null +++ b/third_party/rust/failure/tests/macro_trailing_comma.rs @@ -0,0 +1,67 @@ +#[macro_use] +extern crate failure; + +// NOTE: +// +// This test is in a separate file due to the fact that ensure! cannot be used +// from within failure. +// +// (you get: 'macro-expanded `macro_export` macros from the current crate cannot +// be referred to by absolute paths') + +// Encloses an early-returning macro in an IIFE so that we +// can treat it as a Result-returning function. +macro_rules! wrap_early_return { + ($expr:expr) => {{ + fn func() -> Result<(), failure::Error> { + let _ = $expr; + + #[allow(unreachable_code)] + Ok(()) + } + func().map_err(|e| e.to_string()) + }}; +} + +#[test] +fn bail() { + assert_eq!( + wrap_early_return!(bail!("test")), + wrap_early_return!(bail!("test",))); + assert_eq!( + wrap_early_return!(bail!("test {}", 4)), + wrap_early_return!(bail!("test {}", 4,))); +} + +#[test] +fn ensure() { + assert_eq!( + wrap_early_return!(ensure!(false, "test")), + wrap_early_return!(ensure!(false, "test",))); + assert_eq!( + wrap_early_return!(ensure!(false, "test {}", 4)), + wrap_early_return!(ensure!(false, "test {}", 4,))); +} + +#[test] +fn single_arg_ensure() { + assert_eq!( + wrap_early_return!(ensure!(false)), + Err("false".to_string())); + assert_eq!( + wrap_early_return!(ensure!(true == false)), + Err("true == false".to_string())); + assert_eq!( + wrap_early_return!(ensure!(4 == 5)), + Err("4 == 5".to_string())); +} + +#[test] +fn format_err() { + assert_eq!( + format_err!("test").to_string(), + format_err!("test",).to_string()); + assert_eq!( + format_err!("test {}", 4).to_string(), + format_err!("test {}", 4,).to_string()); +} |