|
5 | 5 | //! particularly as provided by this crate: |
6 | 6 | //! |
7 | 7 | //! * express the intent of an assertion |
8 | | -//! * an assertion reads more like natural english |
| 8 | +//! * an assertion reads more like natural English |
9 | 9 | //! * concise and expressive assertions for more complex types like collections |
10 | 10 | //! * distinct and more helpful error messages for specific assertions |
11 | 11 | //! * easy spotting the difference between the expected and the actual value |
|
320 | 320 | //! # } |
321 | 321 | //! ``` |
322 | 322 | //! |
| 323 | +//! ## Field-by-field recursive comparison |
| 324 | +//! |
| 325 | +//! Requires crate feature `recursive`. |
| 326 | +//! |
| 327 | +//! The recursive comparison mode compares the subject and the expected value |
| 328 | +//! field-by-field recursively. The recursive comparison mode is available for |
| 329 | +//! all types that implement [`serde::Serialize`]. |
| 330 | +//! |
| 331 | +//! Basic usage: |
| 332 | +//! |
| 333 | +//! ``` |
| 334 | +//! # #[cfg(not(feature = "recursive"))] |
| 335 | +//! # fn main() {} |
| 336 | +//! # #[cfg(feature = "recursive")] |
| 337 | +//! # fn main() { |
| 338 | +//! use asserting::prelude::*; |
| 339 | +//! use serde::Serialize; |
| 340 | +//! |
| 341 | +//! #[derive(Serialize)] |
| 342 | +//! struct Address { |
| 343 | +//! id: u64, |
| 344 | +//! street: String, |
| 345 | +//! city: String, |
| 346 | +//! zip: u16, |
| 347 | +//! } |
| 348 | +//! |
| 349 | +//! #[derive(Serialize)] |
| 350 | +//! struct Person { |
| 351 | +//! id: u64, |
| 352 | +//! name: String, |
| 353 | +//! age: u8, |
| 354 | +//! address: Address, |
| 355 | +//! } |
| 356 | +//! |
| 357 | +//! let person = Person { |
| 358 | +//! id: 123, |
| 359 | +//! name: "Silvia".into(), |
| 360 | +//! age: 25, |
| 361 | +//! address: Address { |
| 362 | +//! id: 92, |
| 363 | +//! street: "Second Street".into(), |
| 364 | +//! city: "New York".into(), |
| 365 | +//! zip: 12345, |
| 366 | +//! } |
| 367 | +//! }; |
| 368 | +//! |
| 369 | +//! // ignore some fields |
| 370 | +//! assert_that!(&person) |
| 371 | +//! .using_recursive_comparison() |
| 372 | +//! .ignoring_fields(["id", "address.id", "address.street"]) |
| 373 | +//! .is_equal_to(Person { |
| 374 | +//! id: 0, |
| 375 | +//! name: "Silvia".into(), |
| 376 | +//! age: 25, |
| 377 | +//! address: Address { |
| 378 | +//! id: 0, |
| 379 | +//! street: "Main Street".into(), |
| 380 | +//! city: "New York".into(), |
| 381 | +//! zip: 12345, |
| 382 | +//! } |
| 383 | +//! }); |
| 384 | +//! |
| 385 | +//! // assert only fields relevant for a testcase |
| 386 | +//! assert_that!(person) |
| 387 | +//! .using_recursive_comparison() |
| 388 | +//! .ignoring_not_expected_fields() |
| 389 | +//! .is_equivalent_to(value!({ |
| 390 | +//! name: "Silvia", |
| 391 | +//! age: 25_u8, |
| 392 | +//! address: { |
| 393 | +//! zip: 12345_u16, |
| 394 | +//! } |
| 395 | +//! })); |
| 396 | +//! # } |
| 397 | +//! ``` |
| 398 | +//! |
| 399 | +//! A more in-depth description of the recursive comparison mode is given in the |
| 400 | +//! [`recursive_comparison`] module. |
| 401 | +//! |
323 | 402 | //! # The `assert_that` and `verify_that` functions and macros |
324 | 403 | //! |
325 | 404 | //! Assertions can be written in two ways. The standard way that panics when |
|
341 | 420 | //! failures from assertions, which can be read later. |
342 | 421 | //! |
343 | 422 | //! The [`Spec`] can hold additional information about the subject, such as the |
344 | | -//! expression we are asserting, the code location of the assert statement and |
| 423 | +//! expression we are asserting, the code location of the assert statement, and |
345 | 424 | //! an optional description of what we are going to assert. These attributes are |
346 | 425 | //! all optional and must be set explicitly by the user. |
347 | 426 | //! |
|
760 | 839 | //! [`failures()`]: spec::GetFailures::failures |
761 | 840 | //! [`named()`]: spec::Spec::named |
762 | 841 | //! [`located_at()`]: spec::Spec::located_at |
| 842 | +//! [`serde::Serialize`]: serde_core::Serialize |
763 | 843 |
|
764 | 844 | #![doc(html_root_url = "https://docs.rs/asserting/0.13.1")] |
765 | 845 | #![cfg_attr(not(feature = "std"), no_std)] |
|
0 commit comments