| 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_option_arg_codegen_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_optional_argument_codegen_wraps_values_and_none validates this v3 regression case. |
| 31 | fn test_optional_argument_codegen_wraps_values_and_none() { |
| 32 | v3_bin := build_v3() |
| 33 | out := run_good(v3_bin, 'option_arg_codegen_input', |
| 34 | "struct Info {\n\tvalue int\n}\n\nfn pass_int(x ?int) int {\n\treturn 1\n}\n\nfn pass_info(x ?Info) int {\n\treturn 1\n}\n\nfn main() {\n\tassert pass_int(3) == 1\n\tassert pass_int(none) == 1\n\tassert pass_info(Info{value: 4}) == 1\n\tassert pass_info(none) == 1\n\tprintln('ok')\n}\n") |
| 35 | assert out == 'ok' |
| 36 | } |
| 37 | |
| 38 | // test_optional_if_expr_codegen_initializes_optional_temp validates this v3 regression case. |
| 39 | fn test_optional_if_expr_codegen_initializes_optional_temp() { |
| 40 | v3_bin := build_v3() |
| 41 | out := run_good(v3_bin, 'optional_if_expr_codegen_input', |
| 42 | "fn none_int() ?int {\n\treturn none\n}\n\nfn some_int() ?int {\n\treturn 7\n}\n\nfn choose(flag bool) ?int {\n\tvalue := if flag {\n\t\tnone_int()\n\t} else {\n\t\tsome_int()\n\t}\n\treturn value\n}\n\nfn main() {\n\tgood := choose(false) or { 0 }\n\tnone_value := choose(true) or { -1 }\n\tassert good == 7\n\tassert none_value == -1\n\tprintln('ok')\n}\n") |
| 43 | assert out == 'ok' |
| 44 | } |
| 45 | |
| 46 | // test_nested_local_optional_primitive_typedef validates this v3 regression case. |
| 47 | fn test_nested_local_optional_primitive_typedef() { |
| 48 | v3_bin := build_v3() |
| 49 | out := run_good(v3_bin, 'nested_local_optional_primitive_typedef_input', |
| 50 | "fn main() {\n\txs := [?f32(1.5)]\n\tassert xs.len == 1\n\tprintln('ok')\n}\n") |
| 51 | assert out == 'ok' |
| 52 | } |
| 53 | |
| 54 | // test_optional_clone_return_wraps_payload validates this v3 regression case. |
| 55 | fn test_optional_clone_return_wraps_payload() { |
| 56 | v3_bin := build_v3() |
| 57 | out := run_good(v3_bin, 'optional_clone_return_codegen_input', |
| 58 | "fn clone_array(src []int) ?[]int {\n\treturn src.clone()\n}\n\nfn clone_array_defer(src []int) ?[]int {\n\tdefer {}\n\treturn src.clone()\n}\n\nfn clone_map(src map[string]int) ?map[string]int {\n\treturn src.clone()\n}\n\nfn clone_map_defer(src map[string]int) ?map[string]int {\n\tdefer {}\n\treturn src.clone()\n}\n\nfn array_sum(values []int) int {\n\tmut total := 0\n\tfor value in values {\n\t\ttotal += value\n\t}\n\treturn total\n}\n\nfn main() {\n\tmut values := []int{}\n\tvalues << 2\n\tvalues << 5\n\tarr := clone_array(values) or { []int{} }\n\tarr_defer := clone_array_defer(values) or { []int{} }\n\tmut lookup := map[string]int{}\n\tlookup['a'] = 3\n\tlookup['b'] = 4\n\tcloned := clone_map(lookup) or { map[string]int{} }\n\tcloned_defer := clone_map_defer(lookup) or { map[string]int{} }\n\tassert array_sum(arr) == 7\n\tassert array_sum(arr_defer) == 7\n\tassert cloned['a'] + cloned['b'] == 7\n\tassert cloned_defer['a'] + cloned_defer['b'] == 7\n\tprintln('ok')\n}\n") |
| 59 | assert out == 'ok' |
| 60 | } |
| 61 | |