NUnit is a C # unit-test framework that allows you to write code as follows:
Assert.That(someInt, Is.EqualTo(42));
Assert.That(someList, Has.Member(someMember));
I like this code because it is easy to read, like English.
I play with Rust to find out if I can create a library that gives the same feelings:
use std::fmt::Debug;
struct Is;
enum Verb<T> {
EqualTo(T),
}
impl Is {
fn equal_to<T>(&self, obj: T) -> Verb<T> {
Verb::EqualTo(obj)
}
}
const is: Is = Is{};
fn assert_that<T: Eq + Debug>(obj: T, verb: Verb<T>) {
match verb {
Verb::EqualTo(rhs) => assert_eq!(obj, rhs),
}
}
fn main() {
assert_that(42, is.equal_to(42));
assert_that(42, is.equal_to(0));
}
This is good, but on the one hand: when the panic code is in assert_that(42, is.equal_to(0)), the line specified by the panic is a line assert_eq!(obj, rhs)(i.e. in the library instead of the user code). I know this is normal, but I would have a more helpful post.
How to indicate the number of the right line in a panic?
source
share