mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tests: improved test output formatting
This commit is contained in:

committed by
Alexander Medvednikov

parent
4f173c8900
commit
a0f32f5c29
@ -1,72 +1,75 @@
|
||||
module main
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
/// 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 ...
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
// /////////////////////////////////////////////////////////////////////
|
||||
// / 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 ...
|
||||
// /////////////////////////////////////////////////////////////////////
|
||||
import (
|
||||
os
|
||||
term
|
||||
filepath
|
||||
benchmark
|
||||
)
|
||||
|
||||
const (
|
||||
INNER_INDENT = ' '
|
||||
)
|
||||
|
||||
struct BenchedTests {
|
||||
mut:
|
||||
oks int
|
||||
fails int
|
||||
oks int
|
||||
fails int
|
||||
test_suit_file string
|
||||
step_func_name string
|
||||
bench benchmark.Benchmark
|
||||
bench benchmark.Benchmark
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ///////////////////////////////////////////////////////////////////
|
||||
// Called at the start of the test program produced by `v -stats file_test.v`
|
||||
fn start_testing() BenchedTests {
|
||||
mut b := BenchedTests{ bench: benchmark.new_benchmark() }
|
||||
b.test_suit_file = os.executable() + '.v'
|
||||
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
|
||||
println('running tests in: $b.test_suit_file')
|
||||
return b
|
||||
}
|
||||
|
||||
// Called before each test_ function, defined in file_test.v
|
||||
fn (b mut BenchedTests) testing_step_start(stepfunc string) {
|
||||
b.step_func_name = stepfunc.replace('main__','').replace('__','.')
|
||||
b.oks = C.g_test_oks
|
||||
b.step_func_name = stepfunc.replace('main__', '').replace('__', '.')
|
||||
b.oks = C.g_test_oks
|
||||
b.fails = C.g_test_fails
|
||||
b.bench.step()
|
||||
}
|
||||
|
||||
// Called after each test_ function, defined in file_test.v
|
||||
fn (b mut BenchedTests) testing_step_end() {
|
||||
ok_diff := C.g_test_oks - b.oks
|
||||
ok_diff := C.g_test_oks - b.oks
|
||||
fail_diff := C.g_test_fails - b.fails
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// ////////////////////////////////////////////////////////////////
|
||||
if ok_diff == 0 && fail_diff == 0 {
|
||||
b.bench.neither_fail_nor_ok()
|
||||
println(' ' + b.bench.step_message('NO asserts | ') + b.fn_name() )
|
||||
println(INNER_INDENT + b.bench.step_message_ok('NO asserts | ') + b.fn_name())
|
||||
return
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////
|
||||
if ok_diff > 0 {
|
||||
}
|
||||
// ////////////////////////////////////////////////////////////////
|
||||
if ok_diff > 0 {
|
||||
b.bench.ok_many(ok_diff)
|
||||
}
|
||||
if fail_diff > 0 {
|
||||
b.bench.fail_many(fail_diff)
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////
|
||||
if ok_diff > 0 && fail_diff == 0 {
|
||||
println(ok_text('OK') + b.bench.step_message(nasserts(ok_diff)) + b.fn_name() )
|
||||
// ////////////////////////////////////////////////////////////////
|
||||
if ok_diff > 0 && fail_diff == 0 {
|
||||
println(INNER_INDENT + b.bench.step_message_ok(nasserts(ok_diff)) + b.fn_name())
|
||||
return
|
||||
}
|
||||
if fail_diff > 0 {
|
||||
println(fail_text('FAIL') + b.bench.step_message(nasserts(fail_diff)) + b.fn_name() )
|
||||
if fail_diff > 0 {
|
||||
println(INNER_INDENT + b.bench.step_message_fail(nasserts(fail_diff)) + b.fn_name())
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -78,22 +81,25 @@ fn (b &BenchedTests) fn_name() string {
|
||||
// Called at the end of the test program produced by `v -stats file_test.v`
|
||||
fn (b mut BenchedTests) end_testing() {
|
||||
b.bench.stop()
|
||||
println( ' ' + b.bench.total_message('running V tests in "' + filepath.filename(b.test_suit_file) + '"' ) )
|
||||
println(INNER_INDENT + b.bench.total_message('running V tests in "' + filepath.filename(b.test_suit_file) + '"'))
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ///////////////////////////////////////////////////////////////////
|
||||
fn nasserts(n int) string {
|
||||
if n==0 { return '${n:2d} asserts | ' }
|
||||
if n==1 { return '${n:2d} assert | ' }
|
||||
return '${n:2d} asserts | '
|
||||
if n == 0 {
|
||||
return '${n:2d} asserts | '
|
||||
}
|
||||
if n == 1 {
|
||||
return '${n:2d} assert | '
|
||||
}
|
||||
if n < 10 {
|
||||
return '${n:2d} asserts | '
|
||||
}
|
||||
if n < 100 {
|
||||
return '${n:3d} asserts | '
|
||||
}
|
||||
if n < 1000 {
|
||||
return '${n:4d} asserts | '
|
||||
}
|
||||
return '${n:5d} asserts | '
|
||||
}
|
||||
|
||||
fn ok_text(s string) string {
|
||||
return term.ok_message('${s:5s}')
|
||||
}
|
||||
|
||||
fn fail_text(s string) string {
|
||||
return term.fail_message('${s:5s}')
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user