From f531ada80f143b018a198f3f8b429c7d9aedbdf6 Mon Sep 17 00:00:00 2001 From: David Drysdale Date: Fri, 5 Apr 2024 11:46:21 +0100 Subject: [PATCH] util: test expect_err failures --- src/util/tests.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/util/tests.rs b/src/util/tests.rs index b75d248..bd1ca98 100644 --- a/src/util/tests.rs +++ b/src/util/tests.rs @@ -38,3 +38,35 @@ fn test_cbor_type_error() { expect_err(e, want); } } + +#[test] +#[should_panic] +fn test_expect_err_but_ok() { + let result: Result = Ok(42); + expect_err(result, "absent text"); +} + +#[test] +#[should_panic] +fn test_expect_err_wrong_msg() { + let err = cbor_type_error::<()>(&Value::Bool(true), "a"); + expect_err(err, "incorrect text"); +} + +#[test] +#[should_panic] +fn test_expect_err_wrong_display_msg() { + // Error type where `Debug` shows the message but `Display` doesn't + #[allow(dead_code)] + #[derive(Debug)] + struct Error(&'static str); + impl core::fmt::Display for Error { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "other") + } + } + + let err: Result = Err(Error("text")); + // The expected text appears in the `Debug` output but not the `Display` output. + expect_err(err, "text"); +}