v2 / vlib / v / preludes / tests_with_stats.v
100 lines · 91 sloc · 3.15 KB · b593f6199a0646a567fa03827523b9f038d5230d
Raw
1module main
2
3// /////////////////////////////////////////////////////////////////////
4// / This file will get compiled as a part of the same module,
5// / in which a given _test.v file is, when v is given -stats argument
6// / The methods defined here are called back by the test program's
7// / main function, so that customizing the look & feel of the results
8// / is easy, since it is done in normal V code, instead of in embedded C ...
9// /////////////////////////////////////////////////////////////////////
10import os
11import benchmark
12
13const inner_indent = ' '
14
15struct BenchedTests {
16mut:
17 bench benchmark.Benchmark
18 oks u64
19 fails u64
20 fn_fails u64
21 test_suit_file string
22 step_func_name string
23}
24
25// ///////////////////////////////////////////////////////////////////
26// Called at the start of the test program produced by `v -stats file_test.v`
27@[markused]
28fn start_testing(total_number_of_tests int, vfilename string) BenchedTests {
29 mut benched_tests_res := BenchedTests{
30 bench: benchmark.new_benchmark()
31 }
32 benched_tests_res.bench.set_total_expected_steps(total_number_of_tests)
33 benched_tests_res.test_suit_file = vfilename
34 println('running tests in: ${benched_tests_res.test_suit_file}')
35 return benched_tests_res
36}
37
38// Called before each test_ function, defined in file_test.v
39@[markused]
40fn (mut b BenchedTests) testing_step_start(stepfunc string) {
41 b.step_func_name = stepfunc.replace('main.', '').replace('__', '.')
42 b.oks = test_runner.total_assert_passes
43 b.fails = test_runner.total_assert_fails
44 b.fn_fails = test_runner.fn_fails
45 b.bench.step()
46}
47
48// Called after each test_ function, defined in file_test.v
49@[markused]
50fn (mut b BenchedTests) testing_step_end() {
51 ok_diff := int(test_runner.total_assert_passes - b.oks)
52 fail_diff := int(test_runner.total_assert_fails - b.fails)
53 fn_fail_diff := int(test_runner.fn_fails - b.fn_fails)
54 // ////////////////////////////////////////////////////////////////
55 if ok_diff == 0 && fn_fail_diff == 0 {
56 b.bench.neither_fail_nor_ok()
57 println(inner_indent + b.bench.step_message_ok(' NO asserts | ') + b.fn_name())
58 return
59 }
60 // ////////////////////////////////////////////////////////////////
61 if ok_diff > 0 {
62 b.bench.ok_many(ok_diff)
63 }
64 if fn_fail_diff > 0 {
65 b.bench.fail_many(fn_fail_diff)
66 }
67 // ////////////////////////////////////////////////////////////////
68 if fn_fail_diff > 0 {
69 sfail_diff := nasserts(ok_diff + fail_diff)
70 println(inner_indent + b.bench.step_message_fail(sfail_diff) + b.fn_name())
71 return
72 }
73 if ok_diff > 0 {
74 sok_diff := nasserts(ok_diff)
75 println(inner_indent + b.bench.step_message_ok(sok_diff) + b.fn_name())
76 return
77 }
78}
79
80fn (b &BenchedTests) fn_name() string {
81 return b.step_func_name + '()'
82}
83
84// Called at the end of the test program produced by `v -stats file_test.v`
85@[markused]
86fn (mut b BenchedTests) end_testing() {
87 b.bench.stop()
88 fname := os.file_name(b.test_suit_file)
89 msg := 'running V tests in "${fname}"'
90 final := inner_indent + b.bench.total_message(msg)
91 println(final)
92}
93
94// ///////////////////////////////////////////////////////////////////
95fn nasserts(n int) string {
96 if n == 1 {
97 return '${n:5d} assert | '
98 }
99 return '${n:5d} asserts | '
100}
101