mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tests: colorize failing tests, improve layout (#8066)
This commit is contained in:
@@ -3,6 +3,17 @@ module main
|
||||
import os
|
||||
import term
|
||||
|
||||
const use_color = term.can_show_color_on_stderr()
|
||||
|
||||
const use_relative_paths = can_use_relative_paths()
|
||||
|
||||
fn can_use_relative_paths() bool {
|
||||
return match os.getenv('VERROR_PATHS') {
|
||||
'absolute' { false }
|
||||
else { true }
|
||||
}
|
||||
}
|
||||
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// / This file will get compiled as part of the main program,
|
||||
// / for a _test.v file.
|
||||
@@ -13,32 +24,65 @@ import term
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// TODO copy pasta builtin.v fn ___print_assert_failure
|
||||
fn cb_assertion_failed(i &VAssertMetaInfo) {
|
||||
filepath := if use_relative_paths { i.fpath } else { os.real_path(i.fpath) }
|
||||
mut final_filepath := filepath + ':${i.line_nr + 1}:'
|
||||
if use_color {
|
||||
final_filepath = term.gray(final_filepath)
|
||||
}
|
||||
mut final_funcname := 'fn ' + i.fn_name.replace('main.', '').replace('__', '.')
|
||||
if use_color {
|
||||
final_funcname = term.red('✗ ' + final_funcname)
|
||||
}
|
||||
final_src := if use_color { term.dim('assert ${term.bold(i.src)}') } else { 'assert ' + i.src }
|
||||
eprintln('$final_filepath $final_funcname')
|
||||
if i.op.len > 0 && i.op != 'call' {
|
||||
mut lvtitle := ' Left value:'
|
||||
mut rvtitle := ' Right value:'
|
||||
mut slvalue := '$i.lvalue'
|
||||
mut srvalue := '$i.rvalue'
|
||||
if use_color {
|
||||
slvalue = term.yellow(slvalue)
|
||||
srvalue = term.yellow(srvalue)
|
||||
lvtitle = term.gray(lvtitle)
|
||||
rvtitle = term.gray(rvtitle)
|
||||
}
|
||||
cutoff_limit := 30
|
||||
if slvalue.len > cutoff_limit || srvalue.len > cutoff_limit {
|
||||
eprintln(' > $final_src')
|
||||
eprintln(lvtitle)
|
||||
eprintln(' $slvalue')
|
||||
eprintln(rvtitle)
|
||||
eprintln(' $srvalue')
|
||||
} else {
|
||||
eprintln(' > $final_src')
|
||||
eprintln(' $lvtitle $slvalue')
|
||||
eprintln('$rvtitle $srvalue')
|
||||
}
|
||||
} else {
|
||||
eprintln(' $final_src')
|
||||
}
|
||||
eprintln('')
|
||||
}
|
||||
|
||||
fn cb_assertion_ok(i &VAssertMetaInfo) {
|
||||
// prints for every assertion instead of per test function
|
||||
// TODO: needs to be changed
|
||||
/*
|
||||
use_color := term.can_show_color_on_stderr()
|
||||
use_relative_paths := match os.getenv('VERROR_PATHS') {
|
||||
'absolute' { false }
|
||||
else { true }
|
||||
}
|
||||
final_filename := if use_relative_paths { i.fpath } else { os.real_path(i.fpath) }
|
||||
final_funcname := i.fn_name.replace('main.', '').replace('__', '.')
|
||||
final_src := if use_color { term.bold(i.src) } else { i.src }
|
||||
eprintln('')
|
||||
eprintln('$final_filename:${i.line_nr+1}: failed assert in function $final_funcname')
|
||||
eprintln('Source : `$final_src`')
|
||||
if i.op.len > 0 && i.op != 'call' {
|
||||
mut slvalue := '$i.lvalue'
|
||||
mut srvalue := '$i.rvalue'
|
||||
// lpostfix := if slvalue == i.llabel { '.' } else { '<= `$i.llabel`' }
|
||||
// rpostfix := if srvalue == i.rlabel { '.' } else { '<= `$i.rlabel`' }
|
||||
if use_color {
|
||||
slvalue = term.bold(term.yellow(slvalue))
|
||||
srvalue = term.bold(term.yellow(srvalue))
|
||||
}
|
||||
eprintln(' left value: $slvalue')
|
||||
eprintln(' right value: $srvalue')
|
||||
filepath := if use_relative_paths { i.fpath } else { os.real_path(i.fpath) }
|
||||
final_filepath := if use_color {
|
||||
term.gray(filepath + ':${i.line_nr+1}')
|
||||
} else {
|
||||
filepath + ':${i.line_nr+1}'
|
||||
}
|
||||
}
|
||||
|
||||
fn cb_assertion_ok(i &VAssertMetaInfo) {
|
||||
// do nothing for now on an OK assertion
|
||||
// println('OK ${(i.line_nr+1):5d}|${i.src}')
|
||||
mut final_funcname := i.fn_name.replace('main.', '').replace('__', '.')
|
||||
if use_color {
|
||||
final_funcname = term.green('✓ ' + final_funcname)
|
||||
}
|
||||
println('$final_funcname ($final_filepath)')
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user