Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test of Display impl nested inside display attribute #200

Merged
merged 1 commit into from Oct 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion tests/test_display.rs
@@ -1,4 +1,4 @@
use std::fmt::Display;
use std::fmt::{self, Display};
use thiserror::Error;

fn assert<T: Display>(expected: &str, value: T) {
Expand Down Expand Up @@ -141,6 +141,35 @@ fn test_match() {
);
}

#[test]
fn test_nested_display() {
// Same behavior as the one in `test_match`, but without String allocations.
#[derive(Error, Debug)]
#[error("{}", {
struct Msg<'a>(&'a String, &'a Option<usize>);
impl<'a> Display for Msg<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.1 {
Some(n) => write!(formatter, "error occurred with {}", n),
None => write!(formatter, "there was an empty error"),
}?;
write!(formatter, ": {}", self.0)
}
}
Msg(.0, .1)
})]
struct Error(String, Option<usize>);

assert(
"error occurred with 1: ...",
Error("...".to_owned(), Some(1)),
);
assert(
"there was an empty error: ...",
Error("...".to_owned(), None),
);
}

#[test]
fn test_void() {
#[allow(clippy::empty_enum)]
Expand Down