1 use pretty_assertions::assert_eq;
2 use libtest_mimic::{Trial, Conclusion, Measurement};
3 use crate::common::{args, check, do_run};
4 
5 #[macro_use]
6 mod common;
7 
8 
tests() -> Vec<Trial>9 fn tests() -> Vec<Trial> {
10     fn meas(avg: u64, variance: u64) -> Option<Measurement> {
11         Some(Measurement { avg, variance })
12     }
13 
14     vec![
15         Trial::test("cat", || Ok(())),
16         Trial::test("dog", || Err("was not a good boy".into())),
17         Trial::test("fox", || Ok(())).with_kind("apple"),
18         Trial::test("bunny", || Err("jumped too high".into())).with_kind("apple"),
19         Trial::test("frog", || Ok(())).with_ignored_flag(true),
20         Trial::test("owl", || Err("broke neck".into())).with_ignored_flag(true),
21         Trial::test("fly", || Ok(())).with_ignored_flag(true).with_kind("banana"),
22         Trial::test("bear", || Err("no honey".into())).with_ignored_flag(true).with_kind("banana"),
23 
24         Trial::bench("red", |_| Ok(meas(32, 3))),
25         Trial::bench("blue", |_| Err("sky fell down".into())),
26         Trial::bench("yellow", |_| Ok(meas(64, 4))).with_kind("kiwi"),
27         Trial::bench("green", |_| Err("was poisoned".into())).with_kind("kiwi"),
28         Trial::bench("purple", |_| Ok(meas(100, 5))).with_ignored_flag(true),
29         Trial::bench("cyan", |_| Err("not creative enough".into())).with_ignored_flag(true),
30         Trial::bench("orange", |_| Ok(meas(17, 6))).with_ignored_flag(true).with_kind("banana"),
31         Trial::bench("pink", |_| Err("bad".into())).with_ignored_flag(true).with_kind("banana"),
32     ]
33 }
34 
35 #[test]
normal()36 fn normal() {
37     check(args([]), tests, 16,
38         Conclusion {
39             num_filtered_out: 0,
40             num_passed: 4,
41             num_failed: 4,
42             num_ignored: 8,
43             num_measured: 0,
44         },
45         "
46             test          cat    ... ok
47             test          dog    ... FAILED
48             test [apple]  fox    ... ok
49             test [apple]  bunny  ... FAILED
50             test          frog   ... ignored
51             test          owl    ... ignored
52             test [banana] fly    ... ignored
53             test [banana] bear   ... ignored
54             test          red    ... ok
55             test          blue   ... FAILED
56             test [kiwi]   yellow ... ok
57             test [kiwi]   green  ... FAILED
58             test          purple ... ignored
59             test          cyan   ... ignored
60             test [banana] orange ... ignored
61             test [banana] pink   ... ignored
62 
63             failures:
64 
65             ---- dog ----
66             was not a good boy
67 
68             ---- bunny ----
69             jumped too high
70 
71             ---- blue ----
72             sky fell down
73 
74             ---- green ----
75             was poisoned
76 
77 
78             failures:
79                 dog
80                 bunny
81                 blue
82                 green
83         ",
84     );
85 }
86 
87 #[test]
test_mode()88 fn test_mode() {
89     check(args(["--test"]), tests, 16,
90         Conclusion {
91             num_filtered_out: 0,
92             num_passed: 2,
93             num_failed: 2,
94             num_ignored: 12,
95             num_measured: 0,
96         },
97         "
98             test          cat    ... ok
99             test          dog    ... FAILED
100             test [apple]  fox    ... ok
101             test [apple]  bunny  ... FAILED
102             test          frog   ... ignored
103             test          owl    ... ignored
104             test [banana] fly    ... ignored
105             test [banana] bear   ... ignored
106             test          red    ... ignored
107             test          blue   ... ignored
108             test [kiwi]   yellow ... ignored
109             test [kiwi]   green  ... ignored
110             test          purple ... ignored
111             test          cyan   ... ignored
112             test [banana] orange ... ignored
113             test [banana] pink   ... ignored
114 
115             failures:
116 
117             ---- dog ----
118             was not a good boy
119 
120             ---- bunny ----
121             jumped too high
122 
123 
124             failures:
125                 dog
126                 bunny
127         ",
128     );
129 }
130 
131 #[test]
bench_mode()132 fn bench_mode() {
133     check(args(["--bench"]), tests, 16,
134         Conclusion {
135             num_filtered_out: 0,
136             num_passed: 0,
137             num_failed: 2,
138             num_ignored: 12,
139             num_measured: 2,
140         },
141         "
142             test          cat    ... ignored
143             test          dog    ... ignored
144             test [apple]  fox    ... ignored
145             test [apple]  bunny  ... ignored
146             test          frog   ... ignored
147             test          owl    ... ignored
148             test [banana] fly    ... ignored
149             test [banana] bear   ... ignored
150             test          red    ... bench:          32 ns/iter (+/- 3)
151             test          blue   ... FAILED
152             test [kiwi]   yellow ... bench:          64 ns/iter (+/- 4)
153             test [kiwi]   green  ... FAILED
154             test          purple ... ignored
155             test          cyan   ... ignored
156             test [banana] orange ... ignored
157             test [banana] pink   ... ignored
158 
159             failures:
160 
161             ---- blue ----
162             sky fell down
163 
164             ---- green ----
165             was poisoned
166 
167 
168             failures:
169                 blue
170                 green
171         ",
172     );
173 }
174 
175 #[test]
list()176 fn list() {
177     let (c, out) = common::do_run(args(["--list"]), tests());
178     assert_log!(out, "
179         cat: test
180         dog: test
181         [apple] fox: test
182         [apple] bunny: test
183         frog: test
184         owl: test
185         [banana] fly: test
186         [banana] bear: test
187         red: bench
188         blue: bench
189         [kiwi] yellow: bench
190         [kiwi] green: bench
191         purple: bench
192         cyan: bench
193         [banana] orange: bench
194         [banana] pink: bench
195     ");
196     assert_eq!(c, Conclusion {
197         num_filtered_out: 0,
198         num_passed: 0,
199         num_failed: 0,
200         num_ignored: 0,
201         num_measured: 0,
202      });
203 }
204 
205 #[test]
list_ignored()206 fn list_ignored() {
207     let (c, out) = common::do_run(args(["--list", "--ignored"]), tests());
208     assert_log!(out, "
209         frog: test
210         owl: test
211         [banana] fly: test
212         [banana] bear: test
213         purple: bench
214         cyan: bench
215         [banana] orange: bench
216         [banana] pink: bench
217     ");
218     assert_eq!(c, Conclusion {
219         num_filtered_out: 0,
220         num_passed: 0,
221         num_failed: 0,
222         num_ignored: 0,
223         num_measured: 0,
224      });
225 }
226 
227 #[test]
list_with_filter()228 fn list_with_filter() {
229     let (c, out) = common::do_run(args(["--list", "a"]), tests());
230     assert_log!(out, "
231         cat: test
232         [banana] bear: test
233         cyan: bench
234         [banana] orange: bench
235     ");
236     assert_eq!(c, Conclusion {
237         num_filtered_out: 0,
238         num_passed: 0,
239         num_failed: 0,
240         num_ignored: 0,
241         num_measured: 0,
242      });
243 }
244 
245 #[test]
filter_c()246 fn filter_c() {
247     check(args(["c"]), tests, 2,
248         Conclusion {
249             num_filtered_out: 14,
250             num_passed: 1,
251             num_failed: 0,
252             num_ignored: 1,
253             num_measured: 0,
254         },
255         "
256             test cat  ... ok
257             test cyan ... ignored
258         ",
259     );
260 }
261 
262 #[test]
filter_o_test()263 fn filter_o_test() {
264     check(args(["--test", "o"]), tests, 6,
265         Conclusion {
266             num_filtered_out: 10,
267             num_passed: 1,
268             num_failed: 1,
269             num_ignored: 4,
270             num_measured: 0,
271         },
272         "
273             test          dog    ... FAILED
274             test [apple]  fox    ... ok
275             test          frog   ... ignored
276             test          owl    ... ignored
277             test [kiwi]   yellow ... ignored
278             test [banana] orange ... ignored
279 
280             failures:
281 
282             ---- dog ----
283             was not a good boy
284 
285 
286             failures:
287                 dog
288         ",
289     );
290 }
291 
292 #[test]
filter_o_test_include_ignored()293 fn filter_o_test_include_ignored() {
294     check(args(["--test", "--include-ignored", "o"]), tests, 6,
295         Conclusion {
296             num_filtered_out: 10,
297             num_passed: 2,
298             num_failed: 2,
299             num_ignored: 2,
300             num_measured: 0,
301         },
302         "
303             test          dog    ... FAILED
304             test [apple]  fox    ... ok
305             test          frog   ... ok
306             test          owl    ... FAILED
307             test [kiwi]   yellow ... ignored
308             test [banana] orange ... ignored
309 
310             failures:
311 
312             ---- dog ----
313             was not a good boy
314 
315             ---- owl ----
316             broke neck
317 
318 
319             failures:
320                 dog
321                 owl
322         ",
323     );
324 }
325 
326 #[test]
filter_o_test_ignored()327 fn filter_o_test_ignored() {
328     check(args(["--test", "--ignored", "o"]), tests, 3,
329         Conclusion {
330             num_filtered_out: 13,
331             num_passed: 1,
332             num_failed: 1,
333             num_ignored: 1,
334             num_measured: 0,
335         },
336         "
337             test          frog   ... ok
338             test          owl    ... FAILED
339             test [banana] orange ... ignored
340 
341             failures:
342 
343             ---- owl ----
344             broke neck
345 
346 
347             failures:
348                 owl
349         ",
350     );
351 }
352 
353 #[test]
normal_include_ignored()354 fn normal_include_ignored() {
355     check(args(["--include-ignored"]), tests, 16,
356         Conclusion {
357             num_filtered_out: 0,
358             num_passed: 8,
359             num_failed: 8,
360             num_ignored: 0,
361             num_measured: 0,
362         },
363         "
364             test          cat    ... ok
365             test          dog    ... FAILED
366             test [apple]  fox    ... ok
367             test [apple]  bunny  ... FAILED
368             test          frog   ... ok
369             test          owl    ... FAILED
370             test [banana] fly    ... ok
371             test [banana] bear   ... FAILED
372             test          red    ... ok
373             test          blue   ... FAILED
374             test [kiwi]   yellow ... ok
375             test [kiwi]   green  ... FAILED
376             test          purple ... ok
377             test          cyan   ... FAILED
378             test [banana] orange ... ok
379             test [banana] pink   ... FAILED
380 
381             failures:
382 
383             ---- dog ----
384             was not a good boy
385 
386             ---- bunny ----
387             jumped too high
388 
389             ---- owl ----
390             broke neck
391 
392             ---- bear ----
393             no honey
394 
395             ---- blue ----
396             sky fell down
397 
398             ---- green ----
399             was poisoned
400 
401             ---- cyan ----
402             not creative enough
403 
404             ---- pink ----
405             bad
406 
407 
408             failures:
409                 dog
410                 bunny
411                 owl
412                 bear
413                 blue
414                 green
415                 cyan
416                 pink
417         ",
418     );
419 }
420 
421 #[test]
normal_ignored()422 fn normal_ignored() {
423     check(args(["--ignored"]), tests, 8,
424         Conclusion {
425             num_filtered_out: 8,
426             num_passed: 4,
427             num_failed: 4,
428             num_ignored: 0,
429             num_measured: 0,
430         },
431         "
432             test          frog   ... ok
433             test          owl    ... FAILED
434             test [banana] fly    ... ok
435             test [banana] bear   ... FAILED
436             test          purple ... ok
437             test          cyan   ... FAILED
438             test [banana] orange ... ok
439             test [banana] pink   ... FAILED
440 
441             failures:
442 
443             ---- owl ----
444             broke neck
445 
446             ---- bear ----
447             no honey
448 
449             ---- cyan ----
450             not creative enough
451 
452             ---- pink ----
453             bad
454 
455 
456             failures:
457                 owl
458                 bear
459                 cyan
460                 pink
461         ",
462     );
463 }
464 
465 #[test]
lots_of_flags()466 fn lots_of_flags() {
467     check(args(["--include-ignored", "--skip", "g", "--test", "o"]), tests, 3,
468         Conclusion {
469             num_filtered_out: 13,
470             num_passed: 1,
471             num_failed: 1,
472             num_ignored: 1,
473             num_measured: 0,
474         },
475         "
476             test [apple] fox    ... ok
477             test         owl    ... FAILED
478             test [kiwi]  yellow ... ignored
479 
480             failures:
481 
482             ---- owl ----
483             broke neck
484 
485 
486             failures:
487                 owl
488         ",
489     );
490 }
491 
492 #[test]
terse_output()493 fn terse_output() {
494     let (c, out) = do_run(args(["--format", "terse", "--test-threads", "1"]), tests());
495     assert_eq!(c, Conclusion {
496         num_filtered_out: 0,
497         num_passed: 4,
498         num_failed: 4,
499         num_ignored: 8,
500         num_measured: 0,
501     });
502     assert_log!(out, "
503         running 16 tests
504         .F.Fiiii.F.Fiiii
505         failures:
506 
507         ---- dog ----
508         was not a good boy
509 
510         ---- bunny ----
511         jumped too high
512 
513         ---- blue ----
514         sky fell down
515 
516         ---- green ----
517         was poisoned
518 
519 
520         failures:
521             dog
522             bunny
523             blue
524             green
525 
526         test result: FAILED. 4 passed; 4 failed; 8 ignored; 0 measured; 0 filtered out; \
527             finished in 0.00s
528     ");
529 }
530