tests: add vlib/v/gen/js/program_test.v to ease regression testing `-b js` programs

pull/10824/head
Delyan Angelov 2021-07-16 09:29:19 +03:00
parent ebc34c237b
commit 0723daf788
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 117 additions and 1 deletions

View File

@ -2,7 +2,7 @@ import os
const (
test_dir = os.join_path('vlib', 'v', 'gen', 'js', 'tests')
output_dir = '_js_tests/'
output_dir = os.join_path(os.temp_dir(), '_js_tests/')
v_options = '-b js -w'
node_options = ''
)

View File

@ -0,0 +1,98 @@
import os
import term
import rand
import v.util.vtest
import v.util.diff
const vexe = @VEXE
const vroot = @VMODROOT
const diff_cmd = find_diff_cmd()
fn find_diff_cmd() string {
return diff.find_working_diff_command() or { '' }
}
[noreturn]
fn exit_because(msg string) {
eprintln('$msg, tests will not run')
exit(0)
}
fn test_node_exists() {
res := os.execute('node --version')
if res.exit_code != 0 {
exit_because('node does not exist')
}
if !res.output.starts_with('v') {
exit_because('invalid node version')
}
version := res.output.trim_left('v').int()
if version < 10 {
exit_because('node should be at least version 10, but is currently version: $version')
}
println('Using node version: $version')
}
fn test_running_programs_compiled_with_the_js_backend() ? {
os.setenv('VCOLORS', 'never', true)
os.chdir(vroot)
test_dir := 'vlib/v/gen/js/tests/testdata'
main_files := get_main_files_in_dir(test_dir)
fails := check_path(test_dir, main_files) ?
assert fails == 0
}
fn get_main_files_in_dir(dir string) []string {
mut mfiles := os.walk_ext(dir, '.v')
mfiles.sort()
return mfiles
}
fn check_path(dir string, tests []string) ?int {
mut nb_fail := 0
paths := vtest.filter_vtest_only(tests, basepath: vroot)
for path in paths {
program := path.replace(vroot + os.path_separator, '')
program_out := program.replace('.v', '.out')
if !os.exists(program_out) {
os.write_file(program_out, '') ?
}
print(program + ' ')
res := os.execute('$vexe -b js run $program')
if res.exit_code < 0 {
panic(res.output)
}
mut expected := os.read_file(program_out) ?
expected = clean_line_endings(expected)
found := clean_line_endings(res.output)
if expected != found {
println(term.red('FAIL'))
println('============')
println('expected $program_out content:')
println(expected)
println('============')
println('found:')
println(found)
println('============\n')
println('diff:')
println(diff.color_compare_strings(diff_cmd, rand.ulid(), found, expected))
println('============\n')
nb_fail++
} else {
println(term.green('OK'))
assert true
}
}
return nb_fail
}
fn clean_line_endings(s string) string {
mut res := s.trim_space()
res = res.replace(' \n', '\n')
res = res.replace(' \r\n', '\n')
res = res.replace('\r\n', '\n')
res = res.trim('\n')
return res
}

View File

@ -0,0 +1 @@
2 > 1

View File

@ -0,0 +1,15 @@
if 2 > 1 {
println('2 > 1')
}
if 2 < 1 {
println('2 < 1')
}
if 2 == 1 {
println('2 == 1')
}
if 2 == 0 {
println('2 == 0')
}

View File

@ -0,0 +1 @@
hello world

View File

@ -0,0 +1 @@
println('hello world')