| 1 | import os |
| 2 | |
| 3 | const vexe = @VEXE |
| 4 | const tests_dir = os.dir(@FILE) |
| 5 | const v3_dir = os.dir(tests_dir) |
| 6 | const v3_src = os.join_path(v3_dir, 'v3.v') |
| 7 | |
| 8 | // build_v3 builds v3 data for v3 tests. |
| 9 | fn build_v3() string { |
| 10 | v3_bin := os.join_path(os.temp_dir(), 'v3_if_guard_selector_test') |
| 11 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 12 | assert build.exit_code == 0 |
| 13 | return v3_bin |
| 14 | } |
| 15 | |
| 16 | // run_good supports run good handling for v3 tests. |
| 17 | fn run_good(v3_bin string, name string, source string) string { |
| 18 | src := os.join_path(os.temp_dir(), 'v3_${name}.v') |
| 19 | os.write_file(src, source) or { panic(err) } |
| 20 | bin := os.join_path(os.temp_dir(), 'v3_${name}') |
| 21 | compile := os.execute('${v3_bin} ${src} -b c -o ${bin}') |
| 22 | assert compile.exit_code == 0 |
| 23 | assert !compile.output.contains('C compilation failed') |
| 24 | |
| 25 | run := os.execute(bin) |
| 26 | assert run.exit_code == 0 |
| 27 | return run.output.trim_space() |
| 28 | } |
| 29 | |
| 30 | // test_if_guard_selector_keeps_payload_type validates this v3 regression case. |
| 31 | fn test_if_guard_selector_keeps_payload_type() { |
| 32 | v3_bin := build_v3() |
| 33 | if_guard_out := run_good(v3_bin, 'if_guard_selector_input', |
| 34 | "struct Info {\n\tname string\n}\n\nfn maybe_info() ?Info {\n\treturn Info{name: 'abc'}\n}\n\nfn main() {\n\tif info := maybe_info() {\n\t\tname := info.name\n\t\tprintln(name)\n\t}\n}\n") |
| 35 | assert if_guard_out == 'abc' |
| 36 | |
| 37 | map_for_out := run_good(v3_bin, 'map_for_in_input', |
| 38 | "fn main() {\n\tmut m := map[string]int{}\n\tm['a'] = 1\n\tfor name, _ in m {\n\t\tprintln(name)\n\t}\n}\n") |
| 39 | assert map_for_out == 'a' |
| 40 | } |
| 41 | |