Is there a way to construct tests in Rust to throw a warning when not exhaustive? Certainly, I don't expect a solution for this in general, but I'm looking for a solution that would work when the arguments to a function are enumerated types. I'd like to check that all combinations are used in a way a match statement checks that all combinations are covered. For example, consider the code:
// Terrible numerical type
#[derive(Debug,PartialEq)]
pub enum Num {
Int(i32),
Float(f32),
}
// Mathematical operation on this terrible type
pub fn myadd(x : crate::Num, y :Num) -> Num {
match (x,y) {
(Num::Int(x),Num::Int(y)) => Num::Int(x+y),
(Num::Int(x),Num::Float(y)) => Num::Float((x as f32) + y),
(Num::Float(x),Num::Int(y)) => Num::Float(x+(y as f32)),
(Num::Float(x),Num::Float(y)) => Num::Float(x+y),
}
}
// Add testing
#[cfg(test)]
mod test{
use super::*;
#[test]
fn int_int() {
assert_eq!(myadd(Num::Int(1),Num::Int(2)),Num::Int(3));
}
#[test]
fn float_int() {
assert_eq!(myadd(Num::Float(1.),Num::Int(2)),Num::Float(3.));
}
#[test]
fn int_float() {
assert_eq!(myadd(Num::Int(1),Num::Float(2.)),Num::Float(3.));
}
}
Here, we're missing the test float_float. I'd like a way to throw a warning to denote this test is missing. If we forgot the Float,Float case in pattern matching, we'd get the error:
error[E0004]: non-exhaustive patterns: `(Float(_), Float(_))` not covered
--> src/lib.rs:10:11
|
10 | match (x,y) {
| ^^^^^ pattern `(Float(_), Float(_))` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
I'm trying to get something similar for the testing combinations. In case it matters, I don't care if all of the tests are combined to a single function rather than split into four different tests. I didn't know if there was a trick using pattern matching to achieve this or some other mechanism.