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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Before and after examples
//!
//! The Assertables macros can be useful for runtime validation, verification,
//! sanitization, reliability engineering, chaos engineering, and the like.
//!
//! This file has some very simple demonstrations of "before" code that uses
//! just the build-in Rust assert macros, then of "after" code that shows the
//! better Assertables macros.
use *;
/// Is a number greater than another number?
///
/// This example shows how to get better error messages for infix comparisons.
///
/// Compare the first assert which is Rust standard versus As
///
/// ```ignore
/// let a = 1;
/// let b = 2;
///
/// assert!(a > b);
/// // assertion failed: a > b
///
/// assert_infix!(a > b);
/// // assertion failed: `assert_infix!(a > b)`
/// // https://docs.rs/assertables/8.12.0/assertables/macro.assert_infix.html
/// // a label: `a`,
/// // a debug: `1`,
/// // b label: `b`,
/// // b debug: `2`
/// ```
///
/// Are two numbers near each other?
///
/// This example shows two numbers and some basic math for nearness.
///
/// ```ignore
/// let a = 10;
/// let b = 12;
/// let delta = 1;
///
/// assert!(f64::abs(a - b) <= delta);
/// // assertion failed: i32::abs(a - b) <= delta
///
/// assert_infix!(a, b, delta);
/// // assertion failed: `assert_in_delta!(a, b, delta)`
/// // https://docs.rs/assertables/8.12.0/assertables/macro.assert_in_delta.html
/// // a label: `a`,
/// // a debug: `10`,
/// // b label: `b`,
/// // b debug: `12`,
/// // delta label: `delta`,
/// // delta: `1`,
/// // | a - b |: `2`,\n",
/// // | a - b | ≤ delta: false"
/// ```
///
/// Verify a Result is Ok.
///
/// This example shows how to verify that a Result enumeration is Ok.
///
/// ```ignore
/// pub fn positive(i: i8) -> Result<i8, i8> {
/// if i > 0 { Ok(i) } else { Err(i) }
/// }
///
/// assert_eq!(positive(-1).is_ok());
/// // assertion failed: positive(-1).is_ok()
///
/// assert_fs_read_to_string_eq!(a_path, b_path);
/// // assertion failed: `assert_ok!(a)`
/// // https://docs.rs/assertables/8.12.0/assertables/macro.assert_ok.html
/// // a label: `positive(-1)`,
/// // a debug: `Err(-1)`
/// ```
///
/// Compare text file strings.
///
/// This example shows two file system paths, and how to read them into strings.
///
/// ```ignore
/// let a_path = "alfa.txt";
/// let b_path = "bravo.txt";
///
/// assert_eq!(std::fs::read_to_string(a_path).unwrap(), std::fs::read_to_string(b_path).unwrap());
/// // assertion `left == right` failed
/// // left: "alfa\n"
/// // right: "bravo\n"
///
/// assert_fs_read_to_string_eq!(a_path, b_path);
/// // assertion failed: `assert_fs_read_to_string_eq!(a_path, b_path)`
/// // https://docs.rs/assertables/8.12.0/assertables/macro.assert_fs_read_to_string_eq.html
/// // a_path label: `a_path`,
/// // a_path debug: `"alfa.txt"`,
/// // b_path label: `b_path`,
/// // b_path debug: `"bravo.txt"`,
/// // a string: `"alfa\n"`,
/// // b string: `"bravo\n"`
/// ```
///
/// Verify a command standard output stream contains a target string.
///
/// This example shows how to create a command, run it, and capture the output.
///
/// ```ignore
/// use std::process::Command;
/// let mut command = Command::new("echo");
/// command.args(["alfa"]);
/// let containee = "zz";
///
/// assert!(String::from_utf8(command.output().unwrap().stdout).unwrap().contains(&containee));
/// // assertion failed: String::from_utf8(command.output().unwrap().stdout).unwrap().contains(containee)
///
/// assert_command_stdout_contains!(command, containee);
/// // assertion failed: `assert_command_stdout_contains!(command, containee)`
/// // https://docs.rs/assertables/8.12.0/assertables/macro.assert_command_stdout_contains.html
/// // command label: `command`,
/// // command debug: `"echo" "alfa"`,
/// // containee label: `containee`,
/// // containee debug: `"zz"`,
/// // stdout: `"alfa\n"`
/// ```
///