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

A better panic message for assert_body_matches #14

Open
scrabsha opened this issue Jan 6, 2022 · 0 comments
Open

A better panic message for assert_body_matches #14

scrabsha opened this issue Jan 6, 2022 · 0 comments

Comments

@scrabsha
Copy link
Contributor

scrabsha commented Jan 6, 2022

The following code snippet shows is an obvious incorrect call to assert_body_matches:

use restest::assert_body_matches;

struct User {
    first_name: String,
    last_name: String,
}

#[test]
fn basic_message() {
    assert_body_matches! {
        User {
            first_name: "Grace".to_string(),
            last_name: "Hopper".to_string(),
        },
        User {
            first_name: _,
            last_name: "Lovelace",
        },
    };
}

As of 24fc843, it generates the following panic message:

thread 'basic_message' panicked at 'Matching failed', tests/error_message.rs:10:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This is... less than ideal.

What could be done

What i'd love to see is a way to see is something like this:

thread 'basic_message' panicked at 'Failed to match the request output with the provided pattern:
               Value returned:           |           Pattern expected:
                                         |
   |   {                                 |  {
   |       first_name: "Grace",          |      first_name: _,
!! |       last_name: "Hopper",          |      last_name: "Lovelace",
   |   }                                 |  }
', tests/error_message.rs:5:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Note that this output JSON-ifies the pattern. We could Rust-ify the value instead:

               Value returned:           |           Pattern expected:
                                         |
   |   User {                            |  User {
   |       first_name: "Grace",          |      first_name: _,
!! |       last_name: "Hopper",          |      last_name: "Lovelace",
   |   }                                 |  }

Implementation details

We can gather a set of "constraints" of the value returned by the server by using a custom Serializer, which could, from the previous example, return us the following constraints:

.first_name == "Grace"
.last_name == "Hopper"

We could gather the constraints from the pattern by defining a custom [Visitor], which could extract similar constraints from the pattern:

.first_name: _
.last_name: "Lovelace"

We could then visit each constraint from the pattern, get the corresponding constraint in the value-side, compare them (write the !! if required), then pretty-print both on each side.

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

1 participant