murarth / assert_matches Goto Github PK
View Code? Open in Web Editor NEWProvides macro `assert_matches` for testing pattern matching
License: Apache License 2.0
Provides macro `assert_matches` for testing pattern matching
License: Apache License 2.0
Consider this example(I would do a playground one if assert_matches were accesible there):
#[derive(Debug)]
pub enum Error {
Lowercase { name: String },
}
fn do_something(n: String) -> Result<(), Error> {
match n {
name if name.chars().any(|c| !c.is_lowercase()) => Err(Error::Lowercase { name }),
_ => panic!("All good"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;
#[test]
fn example() {
let name = "BazinGa";
let e: Result<(), Error> = Err(Error::Lowercase {
name: "".to_owned(),
});
assert_matches!(do_something(name.to_owned()), e);
}
}
Here are a few issues:
assert_matches
directly it will not compile.e
variable the compiler will complain that it is not used both in place of declaration and usagename
field name is not mathed correctly.Your assert_matches macro is more expressive than that in the matches crate.
My project should probably just switch to this crate, though. We do not use matches macro anywhere.
Hi!
I found myself implementing a very similar macro a few times. I called it assert_pat
, but it did virtually the same thing. Today I thought I should create an RFC proposing that my macro is added to the standard library. I find it very useful and ergonomic whenever you need to assert equality on something that does not implement Eq
, but that can be matched by a destructure. For example, when using error-chain
this is very needed, since the errors generated by that library does not implement Eq
.
However. In my research I found this crate, which is awesome! But have you thought about suggesting it be included in the standard library?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.