3 个版本 (破坏性更新)
0.3.0 | 2024 年 8 月 7 日 |
---|---|
0.2.0 | 2024 年 7 月 28 日 |
0.1.0 | 2024 年 7 月 28 日 |
#1044 in Rust 模式
每月 331 次下载
10KB
73 行
selective_assertions
selective_assertions
是一个 Rust 包,提供了测试中的灵活断言宏。它包括 assert_eq_excluding
和 assert_eq_selected
,允许你在忽略指定字段或仅比较指定字段的情况下比较复杂结构。
特性
- assert_eq_excluding: 断言两个值相等,同时忽略指定字段。
- assert_eq_selected: 断言两个值的特定字段相等。
入门指南
安装
将 selective_assertions
添加到你的 Cargo.toml
[dependencies]
selective_assertions = "0.3.0"
用法
assert_eq_excluding
assert_eq_excluding 宏允许你在忽略指定字段的情况下断言两个值相等。这在比较复杂结构时很有用,其中某些字段可能不同,但与相等性检查无关。
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
assert_eq_selected 宏允许你断言两个值的特定字段相等。这在只需要比较指定字段,而不需要比较整个结构体或对象时很有用。
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);
}
许可证
本项目采用 MIT 许可证。有关许可证的更多信息,请参阅 许可证。