v4 / vlib / v3 / tests / string_int_markused_codegen_test.v
114 lines · 96 sloc · 3.19 KB · 288feee4702b35beadb4037d02b96b5c8674156b
Raw
1import os
2
3const string_int_vexe = @VEXE
4const string_int_tests_dir = os.dir(@FILE)
5const string_int_v3_dir = os.dir(string_int_tests_dir)
6const string_int_vlib_dir = os.dir(string_int_v3_dir)
7const string_int_v3_src = os.join_path(string_int_v3_dir, 'v3.v')
8
9fn string_int_build_v3() string {
10 v3_bin := os.join_path(os.temp_dir(), 'v3_string_int_markused_test_${os.getpid()}')
11 os.rm(v3_bin) or {}
12 build :=
13 os.execute('${string_int_vexe} -gc none -path "${string_int_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${string_int_v3_src}')
14 assert build.exit_code == 0, build.output
15 return v3_bin
16}
17
18fn test_top_level_string_int_roots_exact_receiver_method() {
19 v3_bin := string_int_build_v3()
20 src := os.join_path(os.temp_dir(), 'v3_string_int_markused_${os.getpid()}.v')
21 os.write_file(src, "module main
22
23struct S {
24 value int
25}
26
27struct U {
28 value int
29}
30
31fn (s S) int() int {
32 return s.value + 1
33}
34
35fn (u U) int() int {
36 return u.value + 100
37}
38
39fn use_local_receiver() {
40 s := S{
41 value: 41
42 }
43 println(s.int())
44}
45
46println('42'.int())
47use_local_receiver()
48") or {
49 panic(err)
50 }
51 bin := os.join_path(os.temp_dir(), 'v3_string_int_markused_${os.getpid()}')
52 compile := os.execute('${v3_bin} ${src} -b c -o ${bin}')
53 assert compile.exit_code == 0, compile.output
54 assert !compile.output.contains('implicit declaration'), compile.output
55 run := os.execute(bin)
56 assert run.exit_code == 0, run.output
57 assert run.output.trim_space() == '42\n42', run.output
58
59 generated := os.read_file(bin + '.c') or { panic(err) }
60 string_int_refs := generated.count('string__int(')
61 assert string_int_refs >= 2, generated
62 assert generated.contains('S__int('), generated
63 assert !generated.contains('U__int('), generated
64}
65
66fn test_top_level_or_block_root_receiver_call_roots_string_int() {
67 v3_bin := string_int_build_v3()
68 src := os.join_path(os.temp_dir(), 'v3_string_int_or_root_${os.getpid()}.v')
69 os.write_file(src, "module main
70
71n := arguments()[1] or { '10' }.int()
72println(n)
73") or {
74 panic(err)
75 }
76 bin := os.join_path(os.temp_dir(), 'v3_string_int_or_root_${os.getpid()}')
77 compile := os.execute('${v3_bin} ${src} -b c -o ${bin}')
78 assert compile.exit_code == 0, compile.output
79 assert !compile.output.contains('implicit declaration'), compile.output
80 run := os.execute(bin)
81 assert run.exit_code == 0, run.output
82 assert run.output.trim_space() == '10', run.output
83
84 generated := os.read_file(bin + '.c') or { panic(err) }
85 assert generated.contains('string__int('), generated
86}
87
88fn test_top_level_calls_are_ignored_when_explicit_main_exists() {
89 v3_bin := string_int_build_v3()
90 src := os.join_path(os.temp_dir(), 'v3_explicit_main_ignores_top_level_${os.getpid()}.v')
91 os.write_file(src, "module main
92
93fn ignored_top_level_only() {
94 println('ignored')
95}
96
97ignored_top_level_only()
98
99fn main() {
100 println('entry')
101}
102") or {
103 panic(err)
104 }
105 bin := os.join_path(os.temp_dir(), 'v3_explicit_main_ignores_top_level_${os.getpid()}')
106 compile := os.execute('${v3_bin} ${src} -b c -o ${bin}')
107 assert compile.exit_code == 0, compile.output
108 run := os.execute(bin)
109 assert run.exit_code == 0, run.output
110 assert run.output.trim_space() == 'entry', run.output
111
112 generated := os.read_file(bin + '.c') or { panic(err) }
113 assert !generated.contains('ignored_top_level_only('), generated
114}
115