#assertions #struct-fields #testing #macro #assert #test-macro

selective_assertions

Macros for asserting equality of structs with the flexibility to exclude or focus on specific fields

3 releases (breaking)

0.3.0 Aug 7, 2024
0.2.0 Jul 28, 2024
0.1.0 Jul 28, 2024

#2002 in Rust patterns

Download history 16/week @ 2024-08-09 1/week @ 2024-08-16 34/week @ 2024-09-13 20/week @ 2024-09-20 37/week @ 2024-09-27 10/week @ 2024-10-04

111 downloads per month

MIT license

10KB
73 lines

selective_assertions

selective_assertions is a Rust crate that provides macros for flexible assertions in tests. It includes assert_eq_excluding and assert_eq_selected, which allow you to compare complex structures while ignoring specified fields or only comparing specified fields.

Features

  • assert_eq_excluding: Asserts that two values are equal, while ignoring specified fields.
  • assert_eq_selected: Asserts that specific fields of two values are equal.

Getting Started

Installation

Add selective_assertions to your Cargo.toml:

[dependencies]
selective_assertions = "0.3.0"

Usage

assert_eq_excluding

The assert_eq_excluding macro allows you to assert that two values are equal while ignoring specified fields. This is useful for comparing complex structures where certain fields may differ but are not relevant to the equality check.

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

// Debug and PartialEq are required for assert_eq!
// Clone, Getters, and Setters are required for assert_eq_excluding!
#[derive(Debug, PartialEq, Clone, Getters, Setters)]
#[set = "pub"]
struct User {
    id: u32,
    name: String,
    age: u32,
}

impl User {
    fn new(id: u32, name: &str, age: u32) -> Self {
        User { id, name: name.to_string(), age }
    }
}

fn main() {
    let user1 = User { id: 1, name: "Alice".to_string(), age: 10 };
    let user2 = User { id: 1, name: "Alice".to_string(), age: 20 };

    // This assertion will pass because the `age` field is excluded in the comparison.
    assert_eq_excluding!(user1, user2, age);
}

assert_eq_selected

The assert_eq_selected macro allows you to assert that specific fields of two values are equal. This is useful for focusing on comparing only the specified fields without needing to compare the entire struct or object.

use derive_getters::Getters;
use selective_assertions::*;

// Debug and PartialEq are required for assert_eq!
// Getters is required for assert_eq_excluding!
#[derive(Debug, PartialEq, Getters)]
struct User {
    id: u32,
    name: String,
    age: u8,
}

fn main() {
    let user1 = User { id: 1, name: String::from("Alice"), age: 7 };
    let user2 = User { id: 1, name: String::from("Alice"), age: 8 };

    // Compare user1 and user2, focusing only on the `id` and `name` fields
    assert_eq_selected!(user1, user2, id, name);
}

License

This project is licensed under the MIT License. For more information on the license, please see the license.

Dependencies