v/vlib/v/fmt/fmt_test.v

75 lines
2.1 KiB
V
Raw Normal View History

2020-04-25 15:49:16 +00:00
import os
import term
import benchmark
import v.ast
import v.fmt
import v.parser
import v.table
import v.pref
import v.util
2020-02-17 21:50:04 +00:00
const (
error_missing_vexe = 1
error_failed_tests = 2
2020-02-17 21:50:04 +00:00
)
fn test_fmt() {
fmt_message := 'vfmt tests'
2020-02-29 16:51:35 +00:00
eprintln(term.header(fmt_message, '-'))
2020-02-17 21:50:04 +00:00
vexe := os.getenv('VEXE')
if vexe.len == 0 || !os.exists(vexe) {
eprintln('VEXE must be set')
exit(error_missing_vexe)
}
2020-03-07 21:26:26 +00:00
vroot := os.dir(vexe)
2020-03-10 14:02:09 +00:00
tmpfolder := os.temp_dir()
diff_cmd := util.find_working_diff_command() or { '' }
mut fmt_bench := benchmark.new_benchmark()
// Lookup the existing test _input.vv files:
input_files := os.walk_ext('$vroot/vlib/v/fmt/tests', '_input.vv')
2020-02-29 16:51:35 +00:00
fmt_bench.set_total_expected_steps(input_files.len)
for istep, ipath in input_files {
fmt_bench.cstep = istep
fmt_bench.step()
2020-03-19 14:49:07 +00:00
ifilename := os.file_name(ipath)
opath := ipath.replace('_input.vv', '_expected.vv')
if !os.exists(opath) {
fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('missing file ${opath}'))
continue
}
expected_ocontent := os.read_file(opath) or {
2020-02-29 16:51:35 +00:00
fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('cannot read from ${opath}'))
continue
}
table := table.new_table()
file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{
is_fmt: true
}, &ast.Scope{
parent: 0
2020-04-25 15:49:16 +00:00
})
2020-05-04 14:22:41 +00:00
result_ocontent := fmt.fmt(file_ast, table, false)
if expected_ocontent != result_ocontent {
fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))
if diff_cmd == '' {
eprintln('>> sorry, but no working "diff" CLI command can be found')
continue
}
2020-03-09 01:23:34 +00:00
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}')
os.write_file(vfmt_result_file, result_ocontent)
eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file))
continue
}
fmt_bench.ok()
eprintln(fmt_bench.step_message_ok('${ipath}'))
}
fmt_bench.stop()
eprintln(term.h_divider('-'))
eprintln(fmt_bench.total_message(fmt_message))
if fmt_bench.nfail > 0 {
exit(error_failed_tests)
2020-02-17 21:50:04 +00:00
}
}