vxx / vlib / v3 / tests / test_v3.vsh
93 lines · 82 sloc · 2.29 KB · fdf1d4b8b182d77187ff4e0c02d98d935c6dec76
Raw
1#!/usr/local/bin/v
2
3import os
4
5const vexe = @VEXE
6const tests_dir = os.dir(@FILE)
7const v3_dir = os.dir(tests_dir)
8const test_v = os.join_path(tests_dir, 'test_all_lang_features.v')
9const test_out = os.join_path(tests_dir, 'test_all_lang_features.out')
10const v3_src = os.join_path(v3_dir, 'v3.v')
11
12fn run(cmd string) os.Result {
13 println('> ${cmd}')
14 return os.execute(cmd)
15}
16
17fn build_v3() string {
18 v3_bin := os.join_path(os.temp_dir(), 'v3_test_runner')
19 cmd := '${vexe} -o ${v3_bin} ${v3_src}'
20 r := run(cmd)
21 if r.exit_code != 0 {
22 eprintln('FAIL: could not build v3')
23 eprintln(r.output)
24 exit(1)
25 }
26 return v3_bin
27}
28
29fn run_v3_c(v3_bin string) string {
30 v3c_bin := '${os.temp_dir()}/v3c_test'
31 r := run('${v3_bin} ${test_v} -b c -o ${v3c_bin}')
32 if r.exit_code != 0 {
33 eprintln('FAIL: v3 C backend compilation failed')
34 eprintln(r.output)
35 exit(1)
36 }
37 return run_stdout(v3c_bin)
38}
39
40fn run_stdout(cmd string) string {
41 stdout_path := '${os.temp_dir()}/v3c_test_stdout'
42 os.rm(stdout_path) or {}
43 println('> ${cmd}')
44 code := os.system('${cmd} > ${os.quoted_path(stdout_path)}')
45 if code != 0 {
46 eprintln('FAIL: command failed (exit ${code})')
47 exit(code)
48 }
49 output := read_text_file(stdout_path)
50 os.rm(stdout_path) or {}
51 return output
52}
53
54fn read_text_file(path string) string {
55 content := os.read_file(path) or {
56 eprintln('FAIL: failed to read ${path}: ${err}')
57 exit(1)
58 }
59 return content
60}
61
62fn assert_same_text(label string, actual string, expected string) {
63 if actual == expected {
64 return
65 }
66 actual_lines := actual.split_into_lines()
67 expected_lines := expected.split_into_lines()
68 min_lines := if actual_lines.len < expected_lines.len {
69 actual_lines.len
70 } else {
71 expected_lines.len
72 }
73 for i in 0 .. min_lines {
74 if actual_lines[i] != expected_lines[i] {
75 eprintln('FAIL: ${label} differs at line ${i + 1}: expected `${expected_lines[i]}`, got `${actual_lines[i]}`')
76 exit(1)
77 }
78 }
79 eprintln('FAIL: ${label} line count differs: expected ${expected_lines.len}, got ${actual_lines.len}')
80 exit(1)
81}
82
83// Build v3
84v3_bin := build_v3()
85println('built v3: ${v3_bin}')
86
87// Run V3 backends
88v3c_out := run_v3_c(v3_bin)
89expected_out := read_text_file(test_out)
90assert_same_text('v3 C fixture output', v3c_out, expected_out)
91println('v3 C: OK')
92
93println('=== V3 C OK (${v3c_out.split_into_lines().len} lines) ===')
94