asserting 0.14.0

Fluent assertions for tests in Rust that are convenient to write and easy to extend.
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
use crate::prelude::*;
use crate::spec::{AssertFailure, Expression, OwnedLocation};
use crate::std::{
    format,
    string::{String, ToString},
    vec,
};

#[test]
fn default_of_expression_is_subject() {
    assert_that!(&*Expression::default()).is_equal_to("subject");
}

#[test]
fn location_display_format() {
    let location = Location::new("src/my_module/my_test.rs", 54, 13);

    assert_that!(format!("{}", location)).is_equal_to("src/my_module/my_test.rs:54:13");
}

#[test]
fn owned_location_display_format() {
    let location = OwnedLocation::new("src/my_module/my_test.rs", 54, 13);

    assert_that!(format!("{}", location)).is_equal_to("src/my_module/my_test.rs:54:13");
}

#[test]
fn construct_owned_location_from_location() {
    let location = Location::new("src/my_module/my_test.rs", 54, 13);
    let owned_location = OwnedLocation::from(location);

    assert_that!(owned_location).is_equal_to(OwnedLocation {
        file: "src/my_module/my_test.rs".to_string(),
        line: 54,
        column: 13,
    });
}

#[test]
fn owned_location_can_be_referenced_as_location() {
    let owned_location = OwnedLocation::new("src/my_module/my_test.rs", 54, 13);

    let location = owned_location.as_location();

    assert_that!(location).is_equal_to(Location {
        file: "src/my_module/my_test.rs",
        line: 54,
        column: 13,
    });
}

#[test]
fn assert_failure_display_format() {
    let failure = AssertFailure {
        description: Some("this thing is the best".to_string()),
        message: "but this thing is the worst\ninstead it should be the best".to_string(),
        location: Some(OwnedLocation::new("src/thing_module/thing_test.rs", 54, 13)),
    };

    assert_that!(format!("{}", failure)).is_equal_to(
        "this thing is the best\nbut this thing is the worst\ninstead it should be the best\n",
    );
}

#[test]
fn mapping_subject_in_spec() {
    struct Point {
        x: i64,
        y: i64,
    }

    let target = Point { x: 12, y: -64 };

    assert_that(target)
        .mapping(|s| (s.x, s.y))
        .is_equal_to((12, -64));
}

#[cfg(feature = "float-cmp")]
#[test]
fn extracting_from_subject_in_spec() {
    struct Foo {
        lorem: String,
        ipsum: f64,
    }

    let foo = Foo {
        lorem: "clita aute consequat dolor".into(),
        ipsum: 0.4519,
    };

    assert_that(&foo)
        .extracting(|s| &s.lorem)
        .is_equal_to("clita aute consequat dolor");

    assert_that(&foo)
        .extracting(|s| s.ipsum)
        .is_close_to(0.4519);
}

#[test]
fn assert_that_macro_with_owned_string_subject() {
    let input_string = String::from("erat esse sit aliqua");

    assert_that!(input_string).is_equal_to("erat esse sit aliqua");
}

#[test]
fn assert_that_macro_with_borrowed_string_subject() {
    let input_string = String::from("erat esse sit aliqua");

    assert_that!(&input_string).is_equal_to("erat esse sit aliqua");
}

#[test]
fn assert_that_macro_with_borrowed_str_subject() {
    let input_string = "adipiscing rebum amet iusto";

    assert_that!(input_string).is_equal_to("adipiscing rebum amet iusto");
}

#[test]
#[should_panic(
    expected = "expected ultimate_answer to be equal to 42\n   but was: 51\n  expected: 42\n"
)]
fn assert_that_macro_is_equal_to_with_integers_fails() {
    let ultimate_answer = 51;

    assert_that!(ultimate_answer)
        .with_diff_format(DIFF_FORMAT_NO_HIGHLIGHT)
        .is_equal_to(42);
}

#[test]
fn assert_that_option_is_some_chained_with_has_value() {
    let subject = Some(42);

    assert_that!(subject).is_some().has_value(42);
}

#[test]
fn verify_that_option_is_some_chained_with_has_value_fails_as_none() {
    let my_variable = None::<i32>;

    let failures = verify_that!(my_variable)
        .is_some()
        .has_value(42)
        .display_failures();

    assert_eq!(
        failures,
        &[
            r"expected my_variable to be Some(_)
   but was: None
  expected: Some(_)
",
            r"expected my_variable to be some containing 42
   but was: None
  expected: Some(42)
",
        ]
    );
}

#[test]
fn verify_that_a_subject_with_custom_description_is_equal_to_fails() {
    let an_answer = 51;

    let failures = verify_that(an_answer)
        .described_as("the answer to all important questions is 42")
        .is_equal_to(42)
        .display_failures();

    assert_eq!(
        failures,
        &[r"the answer to all important questions is 42
expected subject to be equal to 42
   but was: 51
  expected: 42
"]
    );
}

#[test]
fn verify_that_a_subject_with_custom_name_and_custom_description_is_equal_to_fails() {
    let subject = 51;

    let failures = verify_that(subject)
        .named("answer")
        .described_as("the answer to all important questions is 42")
        .is_equal_to(42)
        .display_failures();

    assert_eq!(
        failures,
        &[r"the answer to all important questions is 42
expected answer to be equal to 42
   but was: 51
  expected: 42
"]
    );
}

#[test]
fn soft_assertions_with_chained_assertion_methods() {
    let subject = "the answer to all important questions is 42".to_string();

    verify_that(subject)
        .contains("important")
        .has_at_most_length(43)
        .soft_panic();
}

#[test]
#[should_panic = "expected subject to contain \"unimportant\"\n   \
       but was: \"the answer to all important questions is 42\"\n  \
      expected: \"unimportant\"\n\
    \n\
    expected subject to have at most a length of 41\n   \
       but was: 43\n  \
      expected: <= 41\n\
"]
fn soft_assertions_panic_once_with_multiple_failure_messages() {
    let subject = "the answer to all important questions is 42".to_string();

    verify_that(subject)
        .contains("unimportant")
        .has_at_most_length(41)
        .soft_panic();
}

#[derive(Debug)]
struct TestPerson {
    name: String,
    age: u8,
}

#[test]
fn assert_each_element_of_an_iterator_of_integer() {
    let subject = [2, 4, 6, 8, 10];

    assert_that(subject)
        .is_not_empty()
        .each_element(|e| e.is_positive().is_at_most(20));
}

#[test]
fn assert_each_element_of_an_iterator_of_person() {
    let subject = vec![
        TestPerson {
            name: "John".into(),
            age: 42,
        },
        TestPerson {
            name: "Jane".into(),
            age: 20,
        },
    ];

    assert_that(subject)
        .is_not_empty()
        .each_element(|person| person.extracting(|p| p.age).is_at_most(42));
}

#[test]
fn assert_each_element_of_a_borrowed_iterator_of_integer() {
    let subject = vec![2, 4, 6, 8, 10];

    assert_that(&subject)
        .is_not_empty()
        .each_element(|e| e.is_positive().is_at_most(&20));
}

#[test]
fn assert_each_element_of_a_borrowed_iterator_of_person() {
    let subject = vec![
        TestPerson {
            name: "John".into(),
            age: 42,
        },
        TestPerson {
            name: "Jane".into(),
            age: 20,
        },
    ];

    assert_that(&subject)
        .is_not_empty()
        .each_element(|person| person.extracting(|p| &p.name).starts_with('J'));
}

#[test]
#[should_panic = "expected numbers [1] to be not equal to 4\n   but was: 4\n  expected: not 4\n"]
fn assert_each_element_of_an_iterator_panics_if_one_assertion_fails() {
    let subject = [2, 4, 6, 8, 10];

    assert_that(subject)
        .named("numbers")
        .is_not_empty()
        .each_element(|e| e.is_not_equal_to(4));
}

#[test]
fn verify_assert_each_element_of_an_iterator_fails() {
    let subject = [2, 4, 6, 8, 10];

    let failures = verify_that(&subject)
        .named("numbers")
        .each_element(|e| e.is_greater_than(&2).is_at_most(&7))
        .display_failures();

    assert_eq!(
        failures,
        &[
            r"expected numbers [0] to be greater than 2
   but was: 2
  expected: > 2
",
            r"expected numbers [3] to be at most 7
   but was: 8
  expected: <= 7
",
            r"expected numbers [4] to be at most 7
   but was: 10
  expected: <= 7
",
        ]
    );
}

#[test]
fn assert_any_element_of_an_iterator_of_str() {
    let subject = ["one", "two", "three", "four", "five"];

    assert_that(subject)
        .is_not_empty()
        .any_element(|e| e.contains("ee"));
}

#[test]
fn assert_any_element_of_an_iterator_of_person() {
    let subject = vec![
        TestPerson {
            name: "John".into(),
            age: 42,
        },
        TestPerson {
            name: "Jane".into(),
            age: 20,
        },
    ];

    assert_that(subject)
        .is_not_empty()
        .any_element(|person| person.extracting(|p| p.age).is_at_most(20));
}

#[test]
fn assert_any_element_of_a_borrowed_iterator_of_str() {
    let subject = vec!["one", "two", "three", "four", "five"];

    assert_that(&subject)
        .is_not_empty()
        .any_element(|e| e.starts_with("fi"));
}

#[test]
fn assert_any_element_of_a_borrowed_iterator_of_person() {
    let subject = vec![
        TestPerson {
            name: "John".into(),
            age: 42,
        },
        TestPerson {
            name: "Jane".into(),
            age: 20,
        },
    ];

    assert_that(&subject)
        .is_not_empty()
        .any_element(|person| person.extracting(|p| &p.name).ends_with('n'));
}

#[test]
fn verify_assert_any_element_of_an_iterator_fails() {
    let subject = ["one", "two", "three", "four", "five"];

    let failures = verify_that(subject)
        .named("words")
        .any_element(|e| e.starts_with("fu"))
        .display_failures();

    assert_eq!(
        failures,
        &[
            r#"expected words [0] to start with "fu"
   but was: "one"
  expected: "fu"
"#,
            r#"expected words [1] to start with "fu"
   but was: "two"
  expected: "fu"
"#,
            r#"expected words [2] to start with "fu"
   but was: "three"
  expected: "fu"
"#,
            r#"expected words [3] to start with "fu"
   but was: "four"
  expected: "fu"
"#,
            r#"expected words [4] to start with "fu"
   but was: "five"
  expected: "fu"
"#,
        ]
    );
}

#[cfg(feature = "colored")]
mod colored {
    use crate::prelude::*;

    #[test]
    #[should_panic = "expected subject to contain \"unimportant\"\n   \
       but was: \"\u{1b}[31mthe answer to all important questions is 42\u{1b}[0m\"\n  \
      expected: \"\u{1b}[32munimportant\u{1b}[0m\"\n\
    \n\
    expected subject to have at most a length of 41\n   \
       but was: \u{1b}[31m43\u{1b}[0m\n  \
      expected: <= \u{1b}[32m41\u{1b}[0m\n\
"]
    fn soft_assertions_panic_message_contains_highlighted_diffs() {
        let subject = "the answer to all important questions is 42";

        verify_that(subject)
            .with_configured_diff_format()
            .contains("unimportant")
            .has_at_most_length(41)
            .soft_panic();
    }
}