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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! All-purpose [`assert!(...)`](macro.assert.html) and [`check!(...)`](macro.check.html) macros, inspired by [Catch2](https://github.com/catchorg/Catch2).
//! There is also a [`debug_assert!(...)`](macro.debug_assert.html) macro that is disabled on optimized builds by default.
//!
//! # Why these macros?
//!
//! These macros offer some benefits over the assertions from the standard library:
//! * Use comparison operators inside the assertion instead of specialized macros: `assert!(1 + 1 == 2)`.
//! * Test pattern matches: `assert!(let Err(e) = File::open("/non/existing/file"))`.
//! * Use [let chains](https://blog.rust-lang.org/2025/06/26/Rust-1.88.0/#let-chains) (even with compilers older than Rust 1.88).
//! * See which part of a `&&` chain failed.
//! * Re-use captured variables from pattern matches in later code with `assert!(...)`.
//! * Perform multiple checks before panicking with `check!(...)`.
//! * Colored failure messages!
//! * Highlighted diffs between the Debug form of the expected and actual values!
//!
//! The macros also accept additional arguments for a custom message, so it is fully compatible with `std::assert!(...)`.
//! This means that you can import the macro as a drop in replacement:
//! ```
//! use assert2::assert;
//! ```
//!
//! # Examples
//!
//! See the expression and their evaluation:
//!
//! ```should_panic
//! # use assert2::check;
//! check!(6 + 1 <= 2 * 3);
//! ```
//!
//! 
//!
//! ----------
//!
//! See multiline diffs between the expected and actual value:
//!
//! ```should_panic
//! # use assert2::check;
//! # use std::fs::File;
//! # use std::io::ErrorKind;
//! # #[derive(Debug, Eq, PartialEq)]
//! # struct Pet {
//! # name: String,
//! # age: u32,
//! # kind: String,
//! # shaved: bool,
//! # }
//! # let scrappy = Pet {
//! # name: "Scrappy".into(),
//! # age: 7,
//! # kind: "Bearded Collie".into(),
//! # shaved: false,
//! # };
//! # let coco = Pet {
//! # name: "Coco".into(),
//! # age: 7,
//! # kind: "Bearded Collie".into(),
//! # shaved: true,
//! # };
//! check!(scrappy == coco);
//! ```
//!
//! 
//!
//! ----------
//!
//! Or just in-line highlighting for short values:
//!
//! ```should_panic
//! # use assert2::check;
//! check!((3, Some(4)) == [1, 2, 3].iter().size_hint());
//! ```
//!
//! 
//!
//! ----------
//!
//! Test pattern matches:
//!
//! ```should_panic
//! # use assert2::check;
//! # use std::fs::File;
//! check!(let Ok(_) = File::open("/non/existing/file"));
//! ```
//!
//! 
//!
//! ----------
//!
//! Capture variables from the pattern for further use:
//!
//! ```should_panic
//! # use assert2::check;
//! # use assert2::assert;
//! # use std::fs::File;
//! # use std::io::ErrorKind;
//! assert!(let Err(e) = File::open("/non/existing/file"));
//! check!(e.kind() == ErrorKind::PermissionDenied);
//! ```
//!
//! 
//!
//! ----------
//!
//! Run multiple checks in one macro using `&&` chains (also supports `let`-chains):
//!
//! ```should_panic
//! # use assert2::assert;
//! # use std::fs::File;
//! # use std::io::ErrorKind;
//! assert!(
//! let Err(e) = File::open("/non/existing/file")
//! && e.kind() == ErrorKind::PermissionDenied
//! );
//! ```
//!
//! 
//!
//! # `assert` vs `check`
//! The crate provides two macros: `check!(...)` and `assert!(...)`.
//! The main difference is that `check` is really intended for test cases and doesn't immediately panic.
//! Instead, it will print the assertion error and fail the test.
//! This allows you to run multiple checks and can help to determine the reason of a test failure more easily.
//! The `assert` macro on the other hand simply prints the error and panics,
//! and can be used outside of tests just as well.
//!
//! Currently, `check` uses a scope guard to delay the panic until the current scope ends.
//! Ideally, `check` doesn't panic at all, but only signals that a test case has failed.
//! If this becomes possible in the future, the `check` macro will change, so **you should not rely on `check` to panic**.
//!
//! # Difference between stable and nightly.
//! If available, the crate uses the `proc_macro_span` feature to get the original source code.
//! On stable and beta, it falls back to stringifying the expression.
//! This makes the output a bit more readable on nightly.
//!
//! # Capturing variables
//! When you use the [`assert!(...)`](macro.assert.html) macro, any placeholders in `let` patterns are captured.
//! They will be made available in the calling scope as if they were a regular `let` binding.
//! This allows you to run additional checks on the captured variables.
//!
//! For example:
//!
//! ```
//! # fn main() {
//! # use assert2::assert;
//! # use assert2::check;
//! # struct Foo {
//! # name: &'static str,
//! # }
//! # enum Error {
//! # InvalidName(InvalidNameError),
//! # }
//! # struct InvalidNameError {
//! # name: &'static str,
//! # }
//! # impl Foo {
//! # fn try_new(name: &'static str) -> Result<Self, Error> {
//! # if name == "bar" {
//! # Ok(Self { name })
//! # } else {
//! # Err(Error::InvalidName(InvalidNameError { name }))
//! # }
//! # }
//! # fn name(&self) -> &'static str {
//! # self.name
//! # }
//! # }
//! # impl InvalidNameError {
//! # fn name(&self) -> &'static str {
//! # self.name
//! # }
//! # fn to_string(&self) -> String {
//! # format!("invalid name: {}", self.name)
//! # }
//! # }
//! assert!(let Ok(foo) = Foo::try_new("bar"));
//! check!(foo.name() == "bar");
//!
//! assert!(let Err(Error::InvalidName(e)) = Foo::try_new("bogus name"));
//! check!(e.name() == "bogus name");
//! check!(e.to_string() == "invalid name: bogus name");
//! # }
//! ```
//!
//! The [`check!(...)`](macro.check.html) can not do this, as code following the macro can still be executed, even if the check failed.
//! However, you can run multiple checks inside the same macro call using `let` chains:
//!
//! ```
//! # fn main() {
//! # use assert2::check;
//! # struct Foo {
//! # name: &'static str,
//! # }
//! # enum Error {
//! # InvalidName(InvalidNameError),
//! # }
//! # struct InvalidNameError {
//! # name: &'static str,
//! # }
//! # impl Foo {
//! # fn try_new(name: &'static str) -> Result<Self, Error> {
//! # if name == "bar" {
//! # Ok(Self { name })
//! # } else {
//! # Err(Error::InvalidName(InvalidNameError { name }))
//! # }
//! # }
//! # fn name(&self) -> &'static str {
//! # self.name
//! # }
//! # }
//! # impl InvalidNameError {
//! # fn name(&self) -> &'static str {
//! # self.name
//! # }
//! # fn to_string(&self) -> String {
//! # format!("invalid name: {}", self.name)
//! # }
//! # }
//! check!(let Ok(foo) = Foo::try_new("bar") && foo.name() == "bar");
//!
//! check!(
//! let Err(Error::InvalidName(e)) = Foo::try_new("bogus name")
//! && e.name() == "bogus name"
//! && e.to_string() == "invalid name: bogus name"
//! );
//! # }
//! ```
//!
//! # Controlling the output format.
//!
//! As an end-user, you can influence the way that `assert2` formats failed assertions by changing the `ASSERT2` environment variable.
//! You can specify any combination of options, separated by a comma.
//! The supported options are:
//! * `auto`: Automatically select the compact or pretty `Debug` format for an assertion based on the length (default).
//! * `pretty`: Always use the pretty `Debug` format for assertion messages (`{:#?}`).
//! * `compact`: Always use the compact `Debug` format for assertion messages (`{:?}`).
//! * `no-color`: Disable colored output, even when the output is going to a terminal.
//! * `color`: Enable colored output, even when the output is not going to a terminal.
//!
//! For example, you can run the following command to force the use of the compact `Debug` format with colored output:
//! ```shell
//! ASSERT2=compact,color cargo test
//! ```
//!
//! If neither the `color` or the `no-color` options are set,
//! then `assert2` follows the [clicolors specification](https://bixense.com/clicolors/):
//!
//! * `NO_COLOR != 0` or `CLICOLOR == 0`: Write plain output without color codes.
//! * `CLICOLOR != 0`: Write colored output when the output is going to a terminal.
//! * `CLICOLOR_FORCE != 0`: Write colored output even when it is not going to a terminal.
/// Assert that an expression evaluates to true or matches a pattern.
///
/// Use a `let` expression to test an expression against a pattern: `assert!(let pattern = expr)`.
/// For other tests, just give a boolean expression to the macro: `assert!(1 + 2 == 2)`.
///
/// If the expression evaluates to false or if the pattern doesn't match,
/// an assertion failure is printed and the macro panics instantly.
///
/// Use [`check!`](macro.check.html) if you still want further checks to be executed.
///
/// All placeholders in `let` patterns are made available in the calling scope.
/// Additionally, the macro supports `let` chains (regardless of your compiler version):
/// ```
/// # use assert2::assert;
/// assert!(
/// let Err(e) = std::fs::File::open("/non/existing/file")
/// && e.kind() == std::io::ErrorKind::NotFound
/// && let Some(os_code) = e.raw_os_error()
/// );
/// println!("OS error code: {os_code}");
/// ```
///
/// # Custom messages
/// You can pass additional arguments to the macro.
/// These will be used to print a custom message in addition to the normal message.
///
/// ```
/// # use assert2::assert;
/// assert!(3 * 4 == 12, "Oh no, math is broken! 1 + 1 == {}", 1 + 1);
/// ```
/// Check if an expression evaluates to true or matches a pattern.
///
/// Use a `let` expression to test an expression against a pattern: `check!(let pattern = expr)`.
/// For other tests, just give a boolean expression to the macro: `check!(1 + 2 == 2)`.
///
/// If the expression evaluates to false or if the pattern doesn't match,
/// an assertion failure is printed but the macro does not panic immediately.
/// The check macro will cause the running test to fail eventually.
///
/// Use [`assert!`](macro.assert.html) if you want the test to panic instantly.
///
/// Currently, this macro uses a scope guard to delay the panic.
/// However, this may change in the future if there is a way to signal a test failure without panicking.
/// **Do not rely on `check!()` to panic**.
///
/// Unlike the [`assert!(...)`][assert] macro, placeholders inside a `check!(...)` call are not made available in the calling scope.
/// However, you can use the placeholders for further testing with a `let` chain in the same macro call:
///
/// ```
/// # use assert2::check;
/// check!(
/// let Err(e) = std::fs::File::open("/non/existing/file")
/// && e.kind() == std::io::ErrorKind::NotFound
/// && let Some(_) = e.raw_os_error()
/// );
/// ```
///
/// # Custom messages
/// You can pass additional arguments to the macro.
/// These will be used to print a custom message in addition to the normal message.
///
/// ```
/// # use assert2::check;
/// check!(3 * 4 == 12, "Oh no, math is broken! 1 + 1 == {}", 1 + 1);
/// ```
/// Assert that an expression evaluates to true or matches a pattern.
///
/// This macro supports the same checks as [`assert`](macro.assert.html), but they are only executed if debug assertions are enabled.
///
/// As with [`std::debug_assert`](https://doc.rust-lang.org/stable/std/macro.debug_assert.html),
/// the expression is still type checked if debug assertions are disabled.
///
/// Assert that an expression matches a pattern and make all captured variables available in the calling scope.
///
/// Since version 0.3.17 this is is equivalent to `assert!(let pattern = expression)`, and this macro is now deprecated.
pub use stringify as __assert2_core_stringify;