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

Added support for using => to do extra assertions on destructured data #3

Merged
merged 1 commit into from
Mar 6, 2018

Conversation

jpastuszek
Copy link
Contributor

When working with complex nested data structures I often have a need to do custom assertions on the data itself. Also in Rust some data can only be matched (no Eq) and some can only be compared (e.g. float values).
So I added support for => in the macro to allow any number of extra custom assertions on data itself leaving the main pattern to assert on data shape only.

Here are some of examples from my code base.

See how assert_matches! asserts on the shape of data while assert_eq! or assert! on particular values. As added bonus if some data is wrong you get precise location of the failed assert_eq! rather than generic "does not match" error.
Also it is possible to do data extraction and nesting of assert_matches!:

        assert_matches!(client_record.transaction, ClientAccessTransaction::Full { 
                backend_record: Some(ref backend_record), 
                .. 
            } => {
                let backend_record = backend_record.get_resolved().unwrap();

                assert_matches!(backend_record, &BackendAccessRecord {
                        ident,
                        start: Some(start),
                        end: Some(end),
                        ref reason,
                        ..
                    } =>
                    assert_eq!(reason, "fetch"),
                    assert_eq!(ident, 1000),
                    assert_eq!(start, parse!("1469180762.484544")),
                    assert_eq!(end, parse!("1469180764.484544"))
                );
                assert_matches!(backend_record.transaction, BackendAccessTransaction::Failed {
                        request: HttpRequest {
                            ref method,
                            ref url,
                            ref protocol,
                            ref headers
                        },
                        ..
                    } =>
                    assert_eq!(method, "GET"),
                    assert_eq!(url, "/foobar"),
                    assert_eq!(protocol, "HTTP/1.1"),
                    assert_eq!(headers, &[
                        ("Host".to_string(), "localhost:8080".to_string()),
                        ("User-Agent".to_string(), "curl/7.40.0".to_string())])
                );
                assert_matches!(backend_record.transaction, BackendAccessTransaction::Failed {
                        synth_response: HttpResponse {
                            ref protocol,
                            status,
                            ref reason,
                            ref headers
                        },
                        ..
                    } =>
                    assert_eq!(protocol, "HTTP/1.1"),
                    assert_eq!(status, 503),
                    assert_eq!(reason, "Backend fetch failed"),
                    assert_eq!(headers, &[
                        ("Date".to_string(), "Fri, 22 Jul 2016 09:46:02 GMT".to_string()),
                        ("Server".to_string(), "Varnish".to_string()),
                        ("Content-Type".to_string(), "text/html; charset=utf-8".to_string())])
                );
            }
        );
        assert_matches!(client_record.transaction, ClientAccessTransaction::Full { 
                backend_record: Some(ref backend_record), 
                .. 
            } => {
                let backend_record = backend_record.get_resolved().unwrap();

                assert_matches!(backend_record.transaction, BackendAccessTransaction::Abandoned {
                        request: HttpRequest {
                            ref url,
                            ..
                        },
                        retry_record: Some(ref retry_record), 
                        ..
                    } => 
                    assert_eq!(url, "/retry"),
                    {
                        let backend_record = retry_record.get_resolved().unwrap();

                        assert_eq!(backend_record.reason, "retry".to_string());
                        assert_matches!(backend_record.transaction, BackendAccessTransaction::Full {
                                request: HttpRequest {
                                    ref url,
                                    ..
                                },
                                ..
                            } => 
                            assert_eq!(url, "/iss/v2/thumbnails/foo/4006450256177f4a/bar.jpg")
                        );
                    }
                )
            }
        );

src/lib.rs Outdated
}
};
( $e:expr , $pat:pat if $cond:expr , $($arg:tt)* ) => {
( $e:expr , $pat:pat if $cond:expr , $($arg:expr),* ) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing this? This is not backward compatible and it would prevent users from using the following valid construct: assert_matches!(foo, _, "{value:?}", value=foo);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid error: local ambiguity: multiple parsing options: built-in NTs tt ('arg') or expr ('arm').. I did not think about this use case... I hoped this would be backwards compatible but looks like it can't be. I will experiment more with this - perhaps there is a way.

@jpastuszek
Copy link
Contributor Author

jpastuszek commented Mar 6, 2018

OK, with this changes one can no longer use comma delimited assertions but you can still use block which loos more natural in fact.

        assert_matches!(client_record.transaction, ClientAccessTransaction::Full {
                request: HttpRequest {
                    ref method,
                    ref url,
                    ref protocol,
                    ref headers
                },
                ..
            } => {
                assert_eq!(method, "GET");
                assert_eq!(url, "/foobar");
                assert_eq!(protocol, "HTTP/1.1");
                assert_eq!(headers, &[
                    ("Host".to_string(), "localhost:8080".to_string()),
                    ("User-Agent".to_string(), "curl/7.40.0".to_string())]);
            }
        );

Also message arguments need to be last since tt* will match everything.

@murarth
Copy link
Owner

murarth commented Mar 6, 2018

Okay. Looks good.

Just a little nitpick: Your pull request includes a merge with this repo's master branch; if you rebase on top of this repo's master branch, that should be resolved (and if you'd also squash your two remaining commits into one, that would be great, too).

@jpastuszek
Copy link
Contributor Author

Rebased and squashed as requested.

@murarth murarth merged commit 5131e4c into murarth:master Mar 6, 2018
@murarth
Copy link
Owner

murarth commented Mar 6, 2018

Thanks for your contribution. I've published the changes in version 1.2.0.

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

Successfully merging this pull request may close these issues.

None yet

2 participants