2019-10-07 07:51:26 +02:00
|
|
|
module main
|
2019-12-30 05:23:54 +01:00
|
|
|
// /////////////////////////////////////////////////////////////////////
|
|
|
|
// / This file will get compiled as a part of the same module,
|
|
|
|
// / in which a given _test.v file is, when v is given -stats argument
|
|
|
|
// / The methods defined here are called back by the test program's
|
|
|
|
// / main function, generated by compiler/main.v so that customizing the
|
|
|
|
// / look & feel of the results is easy, since it is done in normal V
|
|
|
|
// / code, instead of in embedded C ...
|
|
|
|
// /////////////////////////////////////////////////////////////////////
|
2020-04-26 08:32:05 +02:00
|
|
|
import os
|
|
|
|
import benchmark
|
2019-10-07 07:51:26 +02:00
|
|
|
|
2019-12-30 05:23:54 +01:00
|
|
|
const (
|
|
|
|
INNER_INDENT = ' '
|
|
|
|
)
|
|
|
|
|
2019-10-07 07:51:26 +02:00
|
|
|
struct BenchedTests {
|
|
|
|
mut:
|
2019-12-30 05:23:54 +01:00
|
|
|
oks int
|
|
|
|
fails int
|
2019-10-07 07:51:26 +02:00
|
|
|
test_suit_file string
|
|
|
|
step_func_name string
|
2019-12-30 05:23:54 +01:00
|
|
|
bench benchmark.Benchmark
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:23:54 +01:00
|
|
|
// ///////////////////////////////////////////////////////////////////
|
2019-10-07 07:51:26 +02:00
|
|
|
// Called at the start of the test program produced by `v -stats file_test.v`
|
2019-12-30 05:23:54 +01:00
|
|
|
fn start_testing(total_number_of_tests int, vfilename string) BenchedTests {
|
|
|
|
mut b := BenchedTests{
|
|
|
|
bench: benchmark.new_benchmark()
|
|
|
|
}
|
|
|
|
b.bench.set_total_expected_steps(total_number_of_tests)
|
|
|
|
b.test_suit_file = vfilename
|
2019-10-07 07:51:26 +02:00
|
|
|
println('running tests in: $b.test_suit_file')
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called before each test_ function, defined in file_test.v
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut b BenchedTests) testing_step_start(stepfunc string) {
|
2019-12-30 05:23:54 +01:00
|
|
|
b.step_func_name = stepfunc.replace('main__', '').replace('__', '.')
|
|
|
|
b.oks = C.g_test_oks
|
2019-10-07 07:51:26 +02:00
|
|
|
b.fails = C.g_test_fails
|
|
|
|
b.bench.step()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called after each test_ function, defined in file_test.v
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut b BenchedTests) testing_step_end() {
|
2019-12-30 05:23:54 +01:00
|
|
|
ok_diff := C.g_test_oks - b.oks
|
2019-10-07 07:51:26 +02:00
|
|
|
fail_diff := C.g_test_fails - b.fails
|
2019-12-30 05:23:54 +01:00
|
|
|
// ////////////////////////////////////////////////////////////////
|
2019-10-07 07:51:26 +02:00
|
|
|
if ok_diff == 0 && fail_diff == 0 {
|
|
|
|
b.bench.neither_fail_nor_ok()
|
2020-04-08 02:45:49 +02:00
|
|
|
println(INNER_INDENT + b.bench.step_message_ok(' NO asserts | ') + b.fn_name())
|
2019-10-07 07:51:26 +02:00
|
|
|
return
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
|
|
|
// ////////////////////////////////////////////////////////////////
|
|
|
|
if ok_diff > 0 {
|
2019-10-07 07:51:26 +02:00
|
|
|
b.bench.ok_many(ok_diff)
|
|
|
|
}
|
|
|
|
if fail_diff > 0 {
|
|
|
|
b.bench.fail_many(fail_diff)
|
|
|
|
}
|
2019-12-30 05:23:54 +01:00
|
|
|
// ////////////////////////////////////////////////////////////////
|
|
|
|
if ok_diff > 0 && fail_diff == 0 {
|
|
|
|
println(INNER_INDENT + b.bench.step_message_ok(nasserts(ok_diff)) + b.fn_name())
|
2019-10-07 07:51:26 +02:00
|
|
|
return
|
|
|
|
}
|
2019-12-30 05:23:54 +01:00
|
|
|
if fail_diff > 0 {
|
|
|
|
println(INNER_INDENT + b.bench.step_message_fail(nasserts(fail_diff)) + b.fn_name())
|
2019-10-07 07:51:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (b &BenchedTests) fn_name() string {
|
|
|
|
return b.step_func_name + '()'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called at the end of the test program produced by `v -stats file_test.v`
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut b BenchedTests) end_testing() {
|
2019-10-07 07:51:26 +02:00
|
|
|
b.bench.stop()
|
2020-03-19 15:49:07 +01:00
|
|
|
println(INNER_INDENT + b.bench.total_message('running V tests in "' + os.file_name(b.test_suit_file) + '"'))
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:23:54 +01:00
|
|
|
// ///////////////////////////////////////////////////////////////////
|
2019-10-07 07:51:26 +02:00
|
|
|
fn nasserts(n int) string {
|
2019-12-30 05:23:54 +01:00
|
|
|
if n == 1 {
|
2020-03-30 17:21:32 +02:00
|
|
|
return '${n:5d} assert | '
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
|
|
|
return '${n:5d} asserts | '
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|