From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/failure/tests/basic_fail.rs | 21 +++++++ third_party/rust/failure/tests/fail_compat.rs | 35 +++++++++++ .../rust/failure/tests/macro_trailing_comma.rs | 67 ++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 third_party/rust/failure/tests/basic_fail.rs create mode 100644 third_party/rust/failure/tests/fail_compat.rs create mode 100644 third_party/rust/failure/tests/macro_trailing_comma.rs (limited to 'third_party/rust/failure/tests') 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> { + return_failure()?; + Ok(()) +} + +fn return_error_send_sync() -> Result<(), Box> { + 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()); +} -- cgit v1.2.3