mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: implement -stats option for running a _test.v file
* Draft implementation of `v -stats file_test.v` . * compiler: call stuff in vlib/benchmark/tests/always_imported.v, when doing `v -stats file_test.v` * Nicer looking output from 'v -stats file_test.v' . * Tweak colors and layout of -stats file_test.v . * Fix a hardcoded path in compiler/main.v . * Show colorized OK/FAIL for the examples in 'v test v' too. * Add some comments about the purpose of the methods inside vlib/benchmark/tests/always_imported.v . * when fails are 0, do not colorize their number at all.
This commit is contained in:
committed by
Alexander Medvednikov
parent
f1923d454c
commit
ac5241b5bd
@@ -1,6 +1,7 @@
|
||||
module benchmark
|
||||
|
||||
import time
|
||||
import term
|
||||
|
||||
/*
|
||||
Example usage of this module:
|
||||
@@ -64,18 +65,40 @@ pub fn (b mut Benchmark) ok() {
|
||||
b.nok++
|
||||
}
|
||||
|
||||
pub fn (b mut Benchmark) fail_many(n int) {
|
||||
b.step_end_time = benchmark.now()
|
||||
b.ntotal+=n
|
||||
b.nfail+=n
|
||||
}
|
||||
|
||||
pub fn (b mut Benchmark) ok_many(n int) {
|
||||
b.step_end_time = benchmark.now()
|
||||
b.ntotal+=n
|
||||
b.nok+=n
|
||||
}
|
||||
|
||||
pub fn (b mut Benchmark) neither_fail_nor_ok() {
|
||||
b.step_end_time = benchmark.now()
|
||||
}
|
||||
|
||||
pub fn (b mut Benchmark) step_message(msg string) string {
|
||||
return b.tdiff_in_ms(msg, b.step_start_time, b.step_end_time)
|
||||
}
|
||||
|
||||
pub fn (b mut Benchmark) total_message(msg string) string {
|
||||
mut tmsg := '$msg : ok, fail, total = ${b.nok:5d}, ${b.nfail:5d}, ${b.ntotal:5d}'
|
||||
mut tmsg := '$msg \n ok, fail, total = ' +
|
||||
term.ok_message('${b.nok:5d}') + ', ' +
|
||||
if b.nfail > 0 { term.fail_message('${b.nfail:5d}') } else { '${b.nfail:5d}' } + ', ' +
|
||||
'${b.ntotal:5d}'
|
||||
if b.verbose {
|
||||
tmsg = '<=== total time spent $tmsg'
|
||||
}
|
||||
return b.tdiff_in_ms(tmsg, b.bench_start_time, b.bench_end_time)
|
||||
}
|
||||
|
||||
pub fn (b mut Benchmark) total_duration() i64 {
|
||||
return (b.bench_end_time - b.bench_start_time)
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn (b mut Benchmark) tdiff_in_ms(s string, sticks i64, eticks i64) string {
|
||||
@@ -89,4 +112,3 @@ fn (b mut Benchmark) tdiff_in_ms(s string, sticks i64, eticks i64) string {
|
||||
fn now() i64 {
|
||||
return time.ticks()
|
||||
}
|
||||
|
||||
|
||||
96
vlib/benchmark/tests/always_imported.v
Normal file
96
vlib/benchmark/tests/always_imported.v
Normal file
@@ -0,0 +1,96 @@
|
||||
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 ...
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
import os
|
||||
import benchmark
|
||||
import term
|
||||
|
||||
struct BenchedTests {
|
||||
mut:
|
||||
oks int
|
||||
fails int
|
||||
test_suit_file string
|
||||
step_func_name string
|
||||
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'
|
||||
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__','')
|
||||
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
|
||||
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() )
|
||||
return
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////
|
||||
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() )
|
||||
return
|
||||
}
|
||||
if fail_diff > 0 {
|
||||
println(fail_text('FAIL') + b.bench.step_message(nasserts(fail_diff)) + b.fn_name() )
|
||||
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`
|
||||
fn (b mut BenchedTests) end_testing() {
|
||||
b.bench.stop()
|
||||
println( ' ' + b.bench.total_message('running V tests in "' + os.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 | '
|
||||
}
|
||||
|
||||
fn ok_text(s string) string {
|
||||
return term.ok_message('${s:5s}')
|
||||
}
|
||||
|
||||
fn fail_text(s string) string {
|
||||
return term.fail_message('${s:5s}')
|
||||
}
|
||||
|
||||
@@ -7,3 +7,21 @@ pub fn can_show_color_on_stdout() bool {
|
||||
pub fn can_show_color_on_stderr() bool {
|
||||
return can_show_color_on_fd(2)
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
|
||||
pub fn ok_message(s string) string {
|
||||
return if can_show_color_on_stdout() {
|
||||
green( s )
|
||||
}else{
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fail_message(s string) string {
|
||||
return if can_show_color_on_stdout() {
|
||||
red( s )
|
||||
}else{
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user