v4 / vlib / v3 / tests / post_merge_review_fixes_test.v
88 lines · 80 sloc · 3.67 KB · 4ca9619315e7615b101419fbcf72e923f4f9b36d
Raw
1import os
2
3const vexe = @VEXE
4const tests_dir = os.dir(@FILE)
5const v3_dir = os.dir(tests_dir)
6const vlib_dir = os.dir(v3_dir)
7const repo_dir = os.dir(vlib_dir)
8const v3_src = os.join_path(v3_dir, 'v3.v')
9const compiler_src_dir = os.join_path(repo_dir, 'cmd', 'v')
10
11fn build_v3() string {
12 v3_bin := os.join_path(os.temp_dir(), 'v3_post_merge_review_fixes_test')
13 build :=
14 os.execute('${vexe} -gc none -path "${vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${v3_src}')
15 assert build.exit_code == 0, build.output
16 return v3_bin
17}
18
19fn run_good(v3_bin string, name string, src string) string {
20 good_src := os.join_path(os.temp_dir(), 'v3_${name}.v')
21 os.write_file(good_src, src) or { panic(err) }
22 good_bin := os.join_path(os.temp_dir(), 'v3_${name}')
23 compile := os.execute('${v3_bin} ${good_src} -b c -o ${good_bin}')
24 assert compile.exit_code == 0, compile.output
25 assert !compile.output.contains('C compilation failed'), compile.output
26 run := os.execute(good_bin)
27 assert run.exit_code == 0, run.output
28 return run.output.trim_space()
29}
30
31fn write_project_file(root string, rel string, src string) {
32 path := os.join_path(root, rel)
33 os.mkdir_all(os.dir(path)) or { panic(err) }
34 os.write_file(path, src) or { panic(err) }
35}
36
37fn run_good_project(v3_bin string, name string, files map[string]string, input string) string {
38 root := os.join_path(os.temp_dir(), 'v3_${name}_project')
39 if os.exists(root) {
40 os.rmdir_all(root) or { panic(err) }
41 }
42 os.mkdir_all(root) or { panic(err) }
43 for rel, src in files {
44 write_project_file(root, rel, src)
45 }
46 input_path := if input.len == 0 { root } else { os.join_path(root, input) }
47 good_bin := os.join_path(os.temp_dir(), 'v3_${name}')
48 compile := os.execute('${v3_bin} ${input_path} -b c -o ${good_bin}')
49 assert compile.exit_code == 0, compile.output
50 assert !compile.output.contains('C compilation failed'), compile.output
51 run := os.execute(good_bin)
52 assert run.exit_code == 0, run.output
53 return run.output.trim_space()
54}
55
56fn test_compiler_vexe_env_uses_running_executable() {
57 v3_bin := build_v3()
58 c_out := os.join_path(os.temp_dir(), 'v3_review_vexe.c')
59 os.rm(c_out) or {}
60 gen := os.execute('${v3_bin} -o ${c_out} ${compiler_src_dir}')
61 assert gen.exit_code == 0, gen.output
62 assert os.exists(c_out)
63 c_source := os.read_file(c_out) or { panic(err) }
64 assert !c_source.contains('v3_vexe_target')
65 assert !c_source.contains('fopen(v3_src')
66 assert c_source.contains('snprintf(v3_checkout_vexe, sizeof(v3_checkout_vexe),')
67 assert c_source.contains('if (access(v3_checkout_vexe, F_OK) == 0) v3_vexe = v3_checkout_vexe;')
68 assert c_source.contains('const char* v3_vexe = v3_src_real_result != NULL ? v3_src_real : v3_arg0;')
69 assert c_source.contains('_putenv_s("VEXE", v3_vexe);')
70 assert c_source.contains('setenv("VEXE", v3_vexe, 1);')
71}
72
73fn test_assoc_return_runs_defers() {
74 v3_bin := build_v3()
75 out := run_good(v3_bin, 'assoc_return_runs_defers',
76 'struct Point {\n\tx int\n\ty int\n}\n\n__global hit int\n\nfn make_point() Point {\n\tbase := Point{\n\t\tx: 1\n\t\ty: 2\n\t}\n\tdefer {\n\t\thit = 7\n\t}\n\treturn Point{\n\t\t...base\n\t\tx: 5\n\t}\n}\n\nfn main() {\n\tp := make_point()\n\tprintln(int_str(p.x))\n\tprintln(int_str(hit))\n}\n')
77 assert out == '5\n7'
78}
79
80fn test_qualified_enum_str_requires_exact_receiver() {
81 v3_bin := build_v3()
82 out := run_good_project(v3_bin, 'qualified_enum_str_exact_receiver', {
83 'main.v': 'module main\n\nimport moda\nimport modb\n\nfn main() {\n\tprintln(moda.Color.red.str())\n\tprintln(modb.Color.blue.str())\n}\n'
84 'moda/moda.v': 'module moda\n\npub enum Color {\n\tred\n}\n'
85 'modb/modb.v': "module modb\n\npub enum Color {\n\tblue\n}\n\npub fn (c Color) str() string {\n\treturn 'custom'\n}\n"
86 }, 'main.v')
87 assert out == 'red\ncustom'
88}
89