FluentFieldAssertions is a library that allows you to write tests in a natural language-like syntax.
With this library, you can perform field assertions in an intuitive and readable way.
FluentFieldAssertions generated the following methods for each field.
fn {field}_eq(&self, expected: {field_type}) -> &Self
fn {field}_ne(&self, expected: {field_type}) -> &Self
fn {field}_satisfies(&self, pred: impl FnOnce(&{field_type}) -> bool) -> &Self
```rust use fluentfieldassertions::FluentFieldAssertions;
struct User { id: usize, name: String, age: usize, }
let user = User { id: 1, name: "Alice".to_string(), age: 17, };
// You can write tests in a natural language-like syntax. user.ideq(1) .nameeq("Alice".tostring()) .agesatisfies(|age| age < &18);
// Same as above. asserteq!(user.id, 1); asserteq!(user.name, "Alice".to_string()); assert!(user.age < 18); ```
You can use it in the basic struct.
Each field must implement the traits Eq
and Display
.
```rust use fluentfieldassertions::FluentFieldAssertions;
struct User { id: usize, name: String, age: usize, }
let user = User { id: 1, name: "Alice".to_string(), age: 17, };
// You can use {field}_eq
to asserteq!.
user.nameeq("Alice".tostring());
asserteq!(user.name, "Alice".to_string()); // Same as above.
// You can use {field}_ne
to assertne!.
user.namene("Bob".tostring());
assertne!(user.name, "Bob".to_string()); // Same as above.
// You can use {field}_satisfies
to assert!.
user.namesatisfies(|name| name.startswith('A'));
assert!(user.name.starts_with('A')); // Same as above.
// You can chain assertions as method calls. user.ideq(1) .nameeq("Alice".tostring()) .agesatisfies(|age| age < &18);
// Same as above. asserteq!(user.id, 1); asserteq!(user.name, "Alice".to_string()); assert!(user.age < 18); ```
You can also use it in the generic struct.
In that case, Generics type T
must implement the traits Eq
and Display
.
```rust use core::fmt::Debug; use fluentfieldassertions::FluentFieldAssertions;
struct PointT
must implement trait Eq
and Display
.
where
T: Eq + Debug,
{
x: T,
y: T,
z: T,
}
let point = Point { x: 1, y: 2, z: 3 };
point.xeq(1).yne(9).z_satisfies(|z| z % 3 == 0); ```