googletest 0.14.2

A rich assertion and matcher library inspired by GoogleTest for C++
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
mod verify_pred {
    use googletest::prelude::*;
    use googletest::Result;
    use indoc::indoc;

    #[test]
    fn supports_function_call_with_non_debug_types() -> Result<()> {
        // Non-Debug - cannot be printed.
        struct Apple;
        fn f(_a: &Apple, _b: u32, _c: u32) -> bool {
            false
        }
        fn g(_a: u32) -> u32 {
            5
        }

        let a = &Apple;
        let res = verify_pred!(f(a, g(g(3)), 1 + 2));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                f(a, g(g(3)), 1 + 2) was false with
                  a does not implement Debug,
                  g(g(3)) = 5,
                  1 + 2 = 3,
                  at"
            })))
        )
    }

    #[test]
    fn supports_trailing_comma() -> Result<()> {
        verify_that!(verify_pred!(false,), err(anything()))
    }

    #[test]
    fn supports_non_function() -> Result<()> {
        verify_pred!(true)?;
        verify_that!(verify_pred!(false), err(anything()))
    }

    #[test]
    fn does_not_print_literals() -> Result<()> {
        trait Foo {
            fn f(&self, _a: u32, _b: i32, _c: u32, _d: &str) -> bool {
                false
            }
        }
        impl Foo for i32 {}

        let res = verify_pred!(0.f(1, 2_i32.abs(), 1 + 2, "hello"));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {r#"
                0.f(1, 2_i32.abs(), 1 + 2, "hello") was false with
                  2_i32.abs() = 2,
                  1 + 2 = 3,
                  at"#
            })))
        )
    }

    #[test]
    fn supports_chained_field_access_and_method_calls_with_non_debug_types() -> Result<()> {
        // Non-Debug
        struct Apple {
            b: Banana,
        }
        #[derive(Debug)]
        struct Banana;
        impl Banana {
            fn c(&self, _c: &Cherry, _d: u32) -> bool {
                false
            }
        }
        // Non-Debug - cannot be printed.
        struct Cherry;

        let a = Apple { b: Banana };
        let c = &Cherry;
        let d = 3;
        let res = verify_pred!(a.b.c(c, d));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                a.b.c(c, d) was false with
                  a does not implement Debug,
                  a.b = Banana,
                  c does not implement Debug,
                  d = 3,
                  at"
            })))
        )
    }

    #[test]
    fn evaluates_functions_and_arguments_exactly_once() -> Result<()> {
        let mut a = 0;
        let mut foo = |_b: u32| {
            a += 1;
            false
        };
        let mut b = 0;
        let mut bar = || {
            b += 10;
            b
        };

        let res = verify_pred!(foo(bar()));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                foo(bar()) was false with
                  bar() = 10,
                  at"
            })))
        )?;

        verify_that!((a, b), eq((1, 10)))
    }

    #[test]
    fn evaluates_methods_and_arguments_exactly_once() -> Result<()> {
        struct Apple(u32);
        impl Apple {
            fn c(&mut self, _b: bool) -> bool {
                self.0 += 1;
                false
            }
        }
        let mut a = Apple(0);
        let mut b = Apple(10);

        let res = verify_pred!(a.c(b.c(false)));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                a.c(b.c(false)) was false with
                  a does not implement Debug,
                  b.c(false) = false,
                  at"
            })))
        )?;

        verify_that!((a.0, b.0), eq((1, 11)))
    }

    #[test]
    fn supports_chained_method_calls() -> Result<()> {
        #[derive(Debug)]
        struct Apple;
        impl Apple {
            fn b(&self, _b: u32) -> Banana {
                Banana
            }
        }
        // Non-Debug: not printed on error.
        struct Banana;
        impl Banana {
            fn c(&self, _c0: u32, _c1: Cherry) -> bool {
                false
            }
        }
        // Non-Debug: not printed on error.
        #[derive(Copy, Clone)]
        struct Cherry;

        let a = Apple;
        let v = 10;
        let res = verify_pred!(a.b(v).c(11, Cherry));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                a.b(v).c(11, Cherry) was false with
                  a = Apple,
                  v = 10,
                  a.b(v) does not implement Debug,
                  Cherry does not implement Debug,
                  at"
            })))
        )
    }

    #[test]
    fn prints_consumed_values() -> Result<()> {
        // Non-Debug
        struct Apple;
        impl Apple {
            fn b(self) -> Banana {
                Banana
            }
        }
        #[derive(Debug)]
        struct Banana;
        impl Banana {
            fn c(self) -> bool {
                false
            }
        }

        let a = Apple;
        let res = verify_pred!(a.b().c());
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                a.b().c() was false with
                  a does not implement Debug,
                  a.b() = Banana,
                  at"
            })))
        )
    }

    #[test]
    fn works_with_realistic_example_with_consumed_intermediate_values() -> Result<()> {
        let res =
            verify_pred!(vec![1, 2].into_iter().map(|x| x * 2).collect::<Vec<_>>().is_empty());
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                vec! [1, 2].into_iter().map(| x | x * 2).collect :: < Vec < _ > >
                ().is_empty() was false with
                  vec! [1, 2] = [1, 2],
                  vec! [1, 2].into_iter() = IntoIter([1, 2]),
                  | x | x * 2 does not implement Debug,
                  vec! [1, 2].into_iter().map(| x | x * 2) = Map { iter: IntoIter([1, 2]) },
                  vec! [1, 2].into_iter().map(| x | x * 2).collect :: < Vec < _ > > () = [2, 4],
                  at"
            })))
        )
    }

    #[test]
    fn values_should_be_accessible_after_test() -> Result<()> {
        // Not `Copy` and should not be consumed by the generated test code.
        #[derive(Debug)]
        struct Apple;
        impl Apple {
            fn b(&self, _c: &mut u32) -> bool {
                false
            }
        }

        let mut c = 0;
        let a = Apple;
        let res = verify_pred!(a.b(&mut c));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                a.b(& mut c) was false with
                  a = Apple,
                  & mut c = 0,
                  at"
            })))
        )?;

        // `a` and `&mut c` should still be accessible after the test despite not being
        // `Copy`.
        let _ = a.b(&mut c);

        Ok(())
    }

    #[test]
    fn prints_values_for_mutating_expressions() -> Result<()> {
        let mut a = 1;
        let mut b = 2;
        let mut c = 0;
        trait Mutator {
            fn mutate_and_false(&mut self, b: &mut u32) -> bool;
        }
        impl Mutator for u32 {
            fn mutate_and_false(&mut self, b: &mut u32) -> bool {
                *self += 10;
                *b += 20;
                false
            }
        }

        // Macro to to avoid the inconsistency in how `;` and `&mut` are printed between
        // Rust versions when printing out the stringified version of the block.
        macro_rules! block_a {
            () => {{
                c += 10;
                &mut a
            }};
        }
        macro_rules! block_b {
            () => {{
                c += 100;
                &mut b
            }};
        }
        let res = verify_pred! { block_a!().mutate_and_false(block_b!()) };

        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                block_a! ().mutate_and_false(block_b! ()) was false with
                  block_a! () = 1,
                  block_b! () = 2,
                  at"
            })))
        )?;

        verify_that!((a, b, c), eq((11, 22, 110)))
    }

    #[test]
    fn values_can_be_insulated_with_parens() -> Result<()> {
        // Not `Copy` and has a consuming method.
        struct Apple;
        impl Apple {
            fn b(self) -> Banana {
                Banana
            }
        }
        #[derive(Debug)]
        struct Banana;
        impl Banana {
            fn c(&self) -> bool {
                false
            }
        }

        let a = Apple;
        let res = verify_pred!({ a.b() }.c());
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                { a.b() }.c() was false with
                  { a.b() } = Banana,
                  at"
            })))
        )
    }

    #[test]
    fn binary_operator() -> Result<()> {
        // Add chaining and function calls.
        fn f(x: u32) -> u32 {
            x + 1
        }
        #[derive(Debug)]
        struct Apple;
        impl Apple {
            fn b(&self, y: u32) -> u32 {
                y + 10
            }
        }
        let a = Apple;
        let x = 1;
        let y = 2;
        let res = verify_pred!(f(x) - 1 == a.b(y + 1) + f(y));
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                f(x) - 1 == a.b(y + 1) + f(y) was false with
                  x = 1,
                  f(x) = 2,
                  f(x) - 1 = 1,
                  a = Apple,
                  y + 1 = 3,
                  a.b(y + 1) = 13,
                  y = 2,
                  f(y) = 3,
                  a.b(y + 1) + f(y) = 16,
                  at"
            })))
        )
    }

    #[rustversion::before(1.77)]
    #[test]
    fn unary_operator() -> Result<()> {
        #[derive(Debug)]
        struct Apple;
        impl Apple {
            fn b(&self, _b: u32, _c: i32) -> i32 {
                0
            }
        }

        let a = Apple;
        let b = 1;
        let res = verify_pred!(!a.b(b, -1) == !-2);
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                ! a.b(b, - 1) ==! - 2 was false with
                  a = Apple,
                  b = 1,
                  a.b(b, - 1) = 0,
                  ! a.b(b, - 1) = -1,
                  ! - 2 = 1,
                  at"
            })))
        )
    }

    #[rustversion::since(1.77)]
    #[test]
    fn unary_operator() -> Result<()> {
        #[derive(Debug)]
        struct Apple;
        impl Apple {
            fn b(&self, _b: u32, _c: i32) -> i32 {
                0
            }
        }

        let a = Apple;
        let b = 1;
        let res = verify_pred!(!a.b(b, -1) == !-2);
        verify_that!(
            res,
            err(displays_as(contains_substring(indoc! {"
                ! a.b(b, - 1) == ! - 2 was false with
                  a = Apple,
                  b = 1,
                  a.b(b, - 1) = 0,
                  ! a.b(b, - 1) = -1,
                  ! - 2 = 1,
                  at"
            })))
        )
    }
}