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