assertables 8.5.0

Assertables: Rust crate of macros `assert…!` for better tests, quality assurance, debug testing, and runtime reliability.
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# Rust assert macros: how to use assertables to improve your code

Work in progress...


### Macros for values

* `assert_eq!`

* `assert_ne!`

* `assert_gt!`

* `assert_ge!`

* `assert_lt!`

* `assert_le!`


### Macros for strings

* `assert_starts_with!` and `assert_not_starts_with!`

* `assert_ends_with!` and `assert_not_ends_with!`

* `assert_contains!` and `assert_not_contains!`

* `assert_is_match!` and `assert_not_match!`


### Macros for functions

A **function** is a typical Rust function.

Compare two functions with inputs:

```rust
let output1 = function1(input1);
let output2 = function2(input2);
assert_eq!(output1, output2);
```

Rust `assertables` provides the macro `assert_fn_eq!` that does the same kind of processing, by automatically calling functions with inputs, then comparing the outputs:

```rust
assert_fn_eq!(function1, input1, function2, input2);
```

The `assertables` message looks like:

```text
assertion failed: `assert_fn_eq!(left_function, left_input, right_function, right_input)`,
  left_function label: `function1`,
     left_input label: `input1`,
     left_input debug: `…`,
 right_function label: `function2`,
     right_expr label: `input2`,
     right_expr debug: `…`,
                 left: `…`,
                right: `…`
```

Rust `assertables` provides these macros for functions:

* `assert_fn_eq!`

* `assert_fn_eq_other!`

* `assert_fn_ge!`

* `assert_fn_ge_other!`

* `assert_fn_gt!`

* `assert_fn_gt_other!`

* `assert_fn_le!`

* `assert_fn_le_other!`

* `assert_fn_lt!`

* `assert_fn_lt_other!`

* `assert_fn_ne!`

* `assert_fn_ne_other!`


### Macros for functions that return a Result

A **Result** is a Rust standard that can be either `Ok`, `Err`.

TODO

let a = 1;
let b = String::from("1");
assert_fn_ok_eq!(example_digit_to_string, a, b);
//-> ()

let a = 1;
let b = String::from("2");
// Panic with error message
let result = panic::catch_unwind(|| {
assert_fn_ok_eq!(example_digit_to_string, a, b);
//-> panic!
});
assert!(result.is_err());

```text
assertion failed: `assert_fn_ok_eq!(left_function, left_input, right_function, right_input)`
  left_function label: `example_digit_to_string`,
     left_input label: `a`,
     left_input debug: `1`,
 right_function label: `example_digit_to_string`,
    right_input label: `a`,
    right_input debug: `1`,
     right_expr label: `b`,
     right_expr debug: `"2"`,
                 left: `"1"`,
                right: `"2"`
```


Rust `assertables` provides these macros for functions that return a Result of `Ok`, `Err`:

* `assert_fn_ok_eq!`

* `assert_fn_ok_eq_other!`

* `assert_fn_ok_ge!`

* `assert_fn_ok_ge_other!`

* `assert_fn_ok_gt!`

* `assert_fn_ok_gt_other!`

* `assert_fn_ok_le!`

* `assert_fn_ok_le_other!`

* `assert_fn_ok_lt!`

* `assert_fn_ok_lt_other!`

* `assert_fn_ok_ne!`

* `assert_fn_ok_ne_other!`

* `assert_fn_err_eq!`

* `assert_fn_err_eq_other!`

* `assert_fn_err_ge!`

* `assert_fn_err_ge_other!`

* `assert_fn_err_gt!`

* `assert_fn_err_gt_other!`

* `assert_fn_err_le!`

* `assert_fn_err_le_other!`

* `assert_fn_err_lt!`

* `assert_fn_err_lt_other!`

* `assert_fn_err_ne!`

* `assert_fn_err_ne_other!`


### Macros for sets

A **set** means a collection of elements, without any ordering, and without duplicate elements.

A set is sometimes written by using mathematical notation, which looks like this:

```text
set = {1, 2, 3}
```

These sets are equal, because ordering does not matter, and the left set contains the same elements as the right set:

```
{1, 2, 3} = {3, 2, 1}
```

These sets are not equal, because the sets do not contain all of the same elements, i.e. the left set contains the element "3" but not "4", and the right set contains the element "4" but not "3":

```text
{1, 2, 3} = {1, 2, 4}
```

This example is not a set, because sets cannot have duplicate elements, i.e. the element "3" occurs twice:

```text
{1, 2, 3, 3}
```

Rust can create a list of elements by using the standard array syntax:

```
let array = [1, 2, 3];
```

Rust arrays care about their order, so cannot be directly compared as sets:

```rust
let array1 = [1, 2, 3];
let array2 = [3, 2, 1];
assert_eq!(array1, array2); //=> panic
```

Convert an array into a set, by using `std::collections::BTreeSet`:

```rust
let set1: std::collections::BTreeSet<_> = array1.into_iter().collect();
```

Convert two arrays into sets, then use `assert_eq!` to compare them as sets:

```rust
let array1 = [1, 2];
let array2 = [3, 4];
let set1: std::collections::BTreeSet<_> = array1.into_iter().collect();
let set2: std::collections::BTreeSet<_> = array2.into_iter().collect();
assert_eq!(set1, set2);
```

Rust `assertables` provides the macro `assert_set_eq!` that does the same kind of processing, by automatically converting inputs into sets, then comparing them as sets:

```rust
assert_set_eq!(array1, array2);
```

The `assertables` message looks like:

```text
assertion failed: `assert_set_eq!(left_set, right_set)`
  left_set label: `&array1`,
  left_set debug: `[1, 2]`,
 right_set label: `&array2`,
 right_set debug: `[3, 4]`,
            left: `{1, 2}`,
           right: `{3, 4}`
```

Rust `assertables` provides these macros for sets:

* `assert_set_eq`

* `assert_set_ne`

* `assert_set_subset`

* `assert_set_superset`

* `assert_set_joint`

* `assert_set_disjoint`


### Macros for bags

A **bag** means a collection of elements, without any ordering, and allowing duplicate elements.

A bag is sometimes written by using mathematical notation, which looks like this:

```text
bag = {1, 1, 1, 2, 3}
```

These bags are equal, because ordering does not matter, and the bags contain all the same elements in the same numer:

```text
{1, 1, 1, 2, 3} = {1, 3, 1, 2, 1}
```

These bags are not equal, because the bags contain an element in a different number, i.e. the left bag has the element "3" once, and right bag has element "3" twice:

```text
{1, 2, 3} = {1, 2, 3, 3}
```

Rust can create a list of elements by using the standard array syntax:

```rust
let array = [1, 1, 1, 2, 3];
```

Rust arrays care about their order, so cannot be directly compared as bags:

```rust
let array1 = [1, 1, 1, 2, 3];
let array2 = [1, 3, 1, 2, 1];
assert_eq!(array1, array2); //=> panic
```

Convert an array into a bag, by using `std::collections::BTreeMap`, and tracking each element's count:

```rust
let mut bag1: ::std::collections::BTreeMap<_, usize> = ::std::collections::BTreeMap::new();
for x in array1.into_iter() {
    let n = bag1.entry(x).or_insert(0);
    *n += 1;
}
```

Convert two arrays into bags, then use `assert_eq!` to compare them as bags:

```rust
let mut bag1: ::std::collections::BTreeMap<_, usize> = ::std::collections::BTreeMap::new();
for x in array1.into_iter() {
    let n = bag1.entry(x).or_insert(0);
    *n += 1;
}
let mut bag1: ::std::collections::BTreeMap<_, usize> = ::std::collections::BTreeMap::new();
for x in array2.into_iter() {
    let n = bag2.entry(x).or_insert(0);
    *n += 1;
}
assert_eq!(bag1, bag2);
```

Rust `assertables` provides the macro `assert_bag_eq!` that does the same kind of processing, by automatically converting inputs into sets, then comparing them as bags:

```rust
assert_bag_eq!(array1, array2);
```

The `aasertables` message looks like:

```text
assertion failed: `assert_bag_eq!(left_bag, right_bag)`
  left_bag label: `&array1`,
  left_bag debug: `[1, 1]`,
 right_bag label: `&array2`,
 right_bag debug: `[1, 1, 1, 2, 2, 2, 2]`
            left: `{1: 2}`,
           right: `{1: 3, 2: 4}`
```

Rust `assertables` provides these macros for bags:

* `assert_bag_eq`

* `assert_bag_ne`

* `assert_bag_subbag`

* `assert_bag_superbag`

* `assert_bag_joint`

* `assert_bag_disjoint`


### Macros for readers

Rust has a concept of a "reader", such as using `std::io::Read` and its function `read_to_string()`. The concept can work with a variety of unpinnings, such as files, bytes, strings, and more.

Example reader of bytes:

```rust
use std::io::Read;
let mut reader = "hello world".as_bytes();
```

The Rust function `read_to_string()` needs an input string which will receive the text output. The function returns a result that is the number of bytes that the function read:

```rust
let mut string = String::new();
let result = reader.read_to_string(&mut string);
let number_of_bytes_read = result.unwrap();
```

Rust can compare a reader's string output to another reader's string output:

```rust
let mut string1 = String::new();
let mut string2 = String::new();
let result1 = reader1.read_to_string(&mut string1);
let result2 = reader2.read_to_string(&mut string2);
assert_eq!(string1, string2);
```

Rust `assertables` provides the macro `assert_io_read_to_string_eq!` that does the same kind of processing, by automatically calling `read_to_string()`, then comparing the outputs as strings:

```rust
assert_io_read_to_string_eq!(reader1, reader2);
```

Rust `assertables` provides these macros for readers:

* `assert_io_read_to_string_eq!`

* `assert_io_read_to_string_eq_other!`

* `assert_io_read_to_string_ne!`

* `assert_io_read_to_string_ne_other!`

* `assert_io_read_to_string_lt!`

* `assert_io_read_to_string_lt_other!`

* `assert_io_read_to_string_le!`

* `assert_io_read_to_string_le_other!`

* `assert_io_read_to_string_gt!`

* `assert_io_read_to_string_gt_other!`

* `assert_io_read_to_string_ge!`

* `assert_io_read_to_string_ge_other!`

* `assert_io_read_to_string_contains!`

* `assert_io_read_to_string_matches!`


### Macros for commands

Rust programs can call shell commands.

For example, consider this shell command, which prints the word hello:

```sh
printf %s hello
```

Rust can create the shell command, by using this code:

```rust
use std::process::Command;
let mut command = Command::new("printf");
command.args(["%s", "hello"]);
```

Run the command and capture its output:

```rust
let output = command.output();
```

Capture the standard output text, which is encoded with a system-specific encoding, not necessarily UTF-8 encoding:

```rust
let stdout = output.unwrap().stdout;
```

Convert the standard output text to UTF-8 encoding:

```rust
let string = String::from_utf8(stdout).unwrap();
```

Rust can compare a command's standard output string to another command's standard output string, by using:

```rust
let string1 = String::from_utf8(command1.output().unwrap().stdout).unwrap();
let string2 = String::from_utf8(command2.output().unwrap().stdout).unwrap();
assert_eq!(string1, string2);
```

Rust `assertables` provides the macro `assert_command_stdout_eq!` that does the same kind of processing, by automatically converting commands into standard outputs, then into UTF-8 strings, then comparing them as strings:

```rust
assert_command_eq!(command1, command2);
```

Rust `assertables` provides these macros for commands and standard output:

* assert_command_stdout_eq.rs
* assert_command_stdout_eq_other.rs
* assert_command_stdout_contains.rs
* assert_command_stdout_is_match.rs

Rust `assertables` provides these macros for commands and standard error:

* assert_command_stderr_eq.rs
* assert_command_stderr_eq_other.rs
* assert_command_stderr_contains.rs
* assert_command_stderr_is_match.rs


### Macros for commands created via program and args

The previous section showed how Rust can create a shell command, by using this code:

```rust
use std::process::Command;
let mut command = Command::new("printf");
command.args(["%s", "hello"]);
```

The previous section showed that Rust `assertables` provides the macro `assert_command_stdout_eq!` such as:

```rust
use std::process::Command;
let mut command1 = Command::new("printf");
command1.args(["%s", "hello"]);
let mut command2 = Command::new("printf");
command2.args(["%s", "hello"]);
assert_command_eq!(string1, string2);
```

Rust `assertables` provides the macro `assert_program_args_eq` that does the same kind of processing, by automatically converting programs and args into commands, then to standard outputs, then into UTF-8 stringss, then comparing them as strings:

```rust
assert_program_args_eq!("printf", ["%s", "hello"], "printf", ["%s", "hello"]);
```

Rust `assertables` provides these macros for program args and standard output:

* `assert_program_args_stdout_eq!`

* `assert_program_args_stdout_eq_other!`

* `assert_program_args_stdout_contains!`

* `assert_program_args_stdout_is_match!`

Rust `assertables` provides these macros for program args and standard error:

* `assert_program_args_stderr_eq!`

* `assert_program_args_stderr_eq_other!`

* `assert_program_args_stderr_contains!`

* `assert_program_args_stderr_is_match!`


## Developers: how we write our test macros

We write each our test macros in three flavors:

* `assert_gt_as_result` returns a `Result` as `Ok`, `Err`. This macro contains all the logic, all the error formatting, etc. This macro is called by the other flavors below. This macros is also useful for runtime checks, such as when you want to know success or failure, yet you don't want to panic.

* `assert_gt` returns `()` or panics. This is the typical macro that most developers will use for testing. This macro wraps `assert_gt_as_result`. This macro provides two arms: one arm is for returning the error messsage as is, and one arm is for returning a developer's custom error message.

* `debug_assert_gt` return `()` or panics. This macro's statements are only enabled in non-optimized builds by default. An optimized build will not execute this macro's statements unless `-C debug-assertions` is passed to the compiler.

The sections below show each of the three flavors, using our simplest macro group: `assert_gt_as_result`, `assert_gt`, `debug_assert_gt`.


### assert_gt_as_result

The macro `assert_gt_as_result` returns a `Result` as `Ok`, `Err`.

The macro contains all the logic and all the error formatting.

The macro is called by the other flavors: directly by `assert_gt` and indirectly by `debug_assert_gt`.

The macros is useful for runtime checks, such as when you want to know success or failure, yet you don't want to panic.

Code:

```rust
#[macro_export]
macro_rules! assert_gt_as_result {
    ($a:expr, $b:expr $(,)?) => ({
        match (&$a, &$b) {
            (a_val, b_val) => {
                if a_val > b_val {
                    Ok(())
                } else {
                    Err(format!(
                        concat!(
                            "assertion failed: `assert_gt!(left, right)`\n",
                            "  left label: `{}`,\n",
                            "  left debug: `{:?}`,\n",
                            " right label: `{}`,\n",
                            " right debug: `{:?}`,\n",
                            "        left: `{:?}`,\n",
                            "       right: `{:?}`"
                        ),
                        stringify!($a), $a,
                        stringify!($b), $b,
                        a_val,
                        b_val
                    ))
                }
            }
        }
    });
}
```


### assert_gt

The macro `assert_gt` returns `()` or panics.

The macro is the typical macro that most developers will use for testing.

The macro wraps `assert_gt_as_result`.

The macro provides two arms: one arm is for returning the error messsage as is, and one arm is for returning a developer's custom error message.

Code:

```rust
#[macro_export]
macro_rules! assert_gt {
    ($a:expr, $b:expr $(,)?) => ({
        match assert_gt_result!($a, $b) {
            Ok(()) => (),
            Err(err) => panic!("{}", err),
        }
    });
    ($a:expr, $b:expr, $($message:tt)+) => ({
        match assert_gt!($a, $b) {
            Ok(()) => (),
            Err(_err) => panic!("{}", $($message)+),
        }
    });
}
```


### debug_assert_gt

The macro `debug_assert_gt` return `()` or panics.

The macro is the typical macro that most developers will use for runtime debugging during development, and possibly also for runtime debugging during production.

The macro's statements are only enabled in non-optimized builds by default. An optimized build will not execute this macro's statements unless `-C debug-assertions` is passed to the compiler.

The macro wraps `assert_gt`.

Code:

```rust
#[macro_export]
macro_rules! debug_assert_gt {
    ($($arg:tt)*) => {
        if $crate::cfg!(debug_assertions) {
            $crate::assert_gt!($($arg)*);
        }
    };
}
```