| 1 | import os |
| 2 | |
| 3 | const string_int_vexe = @VEXE |
| 4 | const string_int_tests_dir = os.dir(@FILE) |
| 5 | const string_int_v3_dir = os.dir(string_int_tests_dir) |
| 6 | const string_int_vlib_dir = os.dir(string_int_v3_dir) |
| 7 | const string_int_v3_src = os.join_path(string_int_v3_dir, 'v3.v') |
| 8 | |
| 9 | fn 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 | |
| 18 | fn 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 | |
| 23 | struct S { |
| 24 | value int |
| 25 | } |
| 26 | |
| 27 | struct U { |
| 28 | value int |
| 29 | } |
| 30 | |
| 31 | fn (s S) int() int { |
| 32 | return s.value + 1 |
| 33 | } |
| 34 | |
| 35 | fn (u U) int() int { |
| 36 | return u.value + 100 |
| 37 | } |
| 38 | |
| 39 | fn use_local_receiver() { |
| 40 | s := S{ |
| 41 | value: 41 |
| 42 | } |
| 43 | println(s.int()) |
| 44 | } |
| 45 | |
| 46 | println('42'.int()) |
| 47 | use_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 | |
| 66 | fn 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 | |
| 71 | n := arguments()[1] or { '10' }.int() |
| 72 | println(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 | |
| 88 | fn 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 | |
| 93 | fn ignored_top_level_only() { |
| 94 | println('ignored') |
| 95 | } |
| 96 | |
| 97 | ignored_top_level_only() |
| 98 | |
| 99 | fn 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 | |