I’m writing a test case for a function which returns a tuple including an enum. I want to verify that the returned tuple matches what I am expecting.
How I do compare tuples composed of enums in Rust? The following fails to compile:
enum Test {
Foo,
Bar
}
assert_eq!((1, Test::Foo), (1, Test::Foo));
(Of course, one of those tuples is generated at runtime by a method.)
>Solution :
Tuples already implement PartialEq and Debug if all their elements do, but you’re missing both a Debug and PartialEq implementation for your custom enum:
#[derive(Debug, PartialEq)]
enum Test {
Foo,
Bar
}
With that your expected assert_eq!((1, Test::Foo), (1, Test::Foo)); works.
If you can’t implement either trait you can use matches!(<value>, <pattern>):
let got = (1, Test::Foo); // calculate your result
assert!(matches!(got, (1, Test::Foo)))