Macro assert_eq_excluding

Source
macro_rules! assert_eq_excluding {
    ($actual:expr, $expect:expr, $($field:ident),+) => { ... };
}
Expand description

Asserts that two values are equal, excluding the specified fields.

The fields listed in the macro are set to the values copied from the expected value before performing the equality check.

This allows you to assert that all other fields are equal while excluding the specified ones.

§Syntax

assert_eq_excluding!(actual_value, expected_value, field1, field2, ...);

  • actual_value: The actual value to compare.
  • expected_value: The expected value to compare against.
  • field1, field2, ...: The names of the fields to exclude during the comparison.

§Example

If you want to compare two User instances while excluding the age as follows:

assert_eq_excluding!(user1, user2, age);

use derive_getters::Getters;
use getset::Setters;
use selective_assertions::*;

#[derive(Debug, PartialEq, Clone, Getters, Setters)]
#[set = "pub"]
pub struct User {
    id: u32,
    name: String,
    age: u8,
}

let user1 = User { id: 1, name: "Alice".to_string(), age: 7 };
let user2 = User { id: 1, name: "Alice".to_string(), age: 8 };

// Compare user1 and user2, excluding the `age` field
assert_eq_excluding!(user1, user2, age); // This will pass

§Panics

This macro will panic if the actual and expected values are not equal when excluding the specified fields.

§Note

The macro requires getter and setter methods.

It is recommended to use derive_getters::Getters and getset::Setters crates.