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

Allow error message like standard library assertions #25

Open
FSMaxB opened this issue Jan 24, 2022 · 4 comments
Open

Allow error message like standard library assertions #25

FSMaxB opened this issue Jan 24, 2022 · 4 comments

Comments

@FSMaxB
Copy link

FSMaxB commented Jan 24, 2022

It would be great if the assertion macros were to support specifying an error message with format parameters as the standard library assertions do.

Example:

assert_eq!(1, 2, "Numbers in test case {} didn't match", case);
@davidpdrsn
Copy link
Owner

Whats your use case? The error messages are quite involved and contain newlines and stuff. I'm thinking it would be quite cumbersome for a user to have to type all that.

@FSMaxB
Copy link
Author

FSMaxB commented Jan 24, 2022

I'm not saying that the user should be able to replace the error messages that are already produced by something like assert_json_eq!, but that they should be able to add their own message to it to contextualize the error without having to look at the line that the error was happening in. This is especially useful when comparing multiple objects in a loop because it allows to add the contextual information in which loop iteration the assertion error was happening.

Also from the perspective of writing readable tests, this allows you to specify the intention of the assertion with an error message that is immediately visible on failure instead of writing a comment.

Let's take this somewhat contrived example:

let tests = [
	(Value::Number(1u8.into()), json!(1u8), "Numbers should be compared."),
	(Value::String("hell".into()), json!("hello"), "Strings should be compared."),
];

for (expected, actual, test_name) in tests {
	assert_json_eq!(expected, actual);
}

This would currently print something like:

json atoms at path "(root)" are not equal:
    lhs:
        "hell"
    rhs:
        "hello"

But this could instead be:

for (expected, actual, test_name) in tests {
	assert_json_eq!(expected, actual, "Error: {test_name}");
}

With a message like:

Error: "Strings should be compared."
json atoms at path "(root)" are not equal:
    lhs:
        "hell"
    rhs:
        "hello"

@FSMaxB
Copy link
Author

FSMaxB commented Jan 24, 2022

The concrete use case that came up for me was when writing an integration test for a REST API. I want to compare both what's stored in the database and what's returned from the create endpoint. So it would look something like this:

let expected_entity = json!(...);
assert_json_eq!(&expected_entity, entity_from_database, "Didn't store created entity to database correctly");
assert_json_eq!(&expected_entity, response_json, "Entity from HTTP response was incorrect");

Without the context, it's easy to mix up the two.

@davidpdrsn
Copy link
Owner

Ah okay sorry! I misunderstood. I see it now. You just wanna print something extra with the error message thats already displayed. Yeah that sounds like a good idea!

@davidpdrsn davidpdrsn changed the title Feature request: Allow error message like standard library assertions. Allow error message like standard library assertions Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants