1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! This example demonstrates that writing a helper function for asserting a
//! custom type is often easier than writing custom assertions.
//!
//! The helper function `assert_snake_body` is used to assert the state of a
//! snake. It reuses built-in assertions on each field. In case of failing
//! assertions, the names of the fields are printed with the failure message.
//!
//! Running the example will print a failed assertion to the console:
//!
//! ```console
//! thread 'main' (5388) panicked at examples\assertion_function.rs:107:5:
//! assertion of snake body failed:
//!
//! expected snake.length to be equal to 3
//! but was: 2
//! expected: 3
//!
//! expected snake.body to contain exactly in order [Coord { x: 2, y: 1 }, Coord { x: 1, y: 1 }, Coord { x: 1, y: 2 }]
//! but was: [Coord { x: 2, y: 1 }, Coord { x: 1, y: 2 }, Coord { x: -1, y: 1 }]
//! expected: [Coord { x: 2, y: 1 }, Coord { x: 1, y: 1 }, Coord { x: 1, y: 2 }]
//! missing: [Coord { x: 1, y: 1 }]
//! extra: [Coord { x: -1, y: 1 }]
//! out-of-order: [Coord { x: 1, y: 2 }]
//!
//! expected snake.head to be equal to Coord { x: 2, y: 1 }
//! but was: Coord { x: 3, y: 1 }
//! expected: Coord { x: 2, y: 1 }
//! ```
//!
//! [image of colored output in the console](assertion_function.png)
//!
//! Since version 0.13.0 of `asserting` it is possible to write a custom
//! assertion method with reusing the built-in assertions. This is demonstrated
//! in the example
//! [custom_assertion_reusing_existing.rs](custom_assertion_reusing_existing.rs)
//! which uses the same `Snake` struct and same assertions as this example.
// just to prevent some linter warnings
use *;
/// Helper function for asserting a snake's state.
///
/// It takes a snake and an expected body and asserts that the snake's
/// length, body, and head are equal to the expected body.
///
/// It first does assertions on all fields of the snake without panicking so
/// that all found failures are printed at once and not the first one only.
///
/// The failure messages contain names of the fields like "snake.length",
/// "snake.body" and "snake.head".