| 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_params_struct_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 | // A `@[params]` struct argument can be supplied as trailing `key: value` call args. |
| 31 | // Regression: the args used to be dropped, generating `make(1, )` (empty arg, trailing comma). |
| 32 | fn test_params_struct_call_args_codegen() { |
| 33 | v3_bin := build_v3() |
| 34 | source := "@[params]\nstruct Cfg {\n\ta int\n\tb string\n\tc bool\n}\n\nfn make(x int, cfg Cfg) string {\n\treturn '\${x}|\${cfg.a}|\${cfg.b}|\${cfg.c}'\n}\n\nfn main() {\n\tprintln(make(1, a: 10, b: 'hi', c: true))\n\tprintln(make(2, b: 'yo'))\n\tprintln(make(3, Cfg{a: 5, b: 'z', c: true}))\n}\n" |
| 35 | out := run_good(v3_bin, 'params_struct_codegen_input', source) |
| 36 | assert out == '1|10|hi|true\n2|0|yo|false\n3|5|z|true' |
| 37 | } |
| 38 | |
| 39 | // The standard-library call that originally exposed the bug: |
| 40 | // `strconv.atof64(s, allow_extra_chars: true)` reachable via `string.f64()`. |
| 41 | // Previously this generated `strconv__atof64(s, )` (dropped param, trailing comma), |
| 42 | // which failed C compilation. We only assert it compiles and runs here; atof64's |
| 43 | // numeric accuracy is covered elsewhere. |
| 44 | fn test_string_f64_params_struct_codegen() { |
| 45 | v3_bin := build_v3() |
| 46 | source := "fn main() {\n\tprintln('\${'3.0'.f64() > 0.0}')\n\tprintln('\${'2.0'.f32() > 0.0}')\n}\n" |
| 47 | out := run_good(v3_bin, 'string_f64_params_input', source) |
| 48 | assert out == 'true\ntrue' |
| 49 | } |
| 50 | |
| 51 | fn test_pool_config_params_struct_can_be_omitted() { |
| 52 | v3_bin := build_v3() |
| 53 | source := "@[params]\nstruct PoolConfig {\n\tmax_open int\n}\n\nfn open_pool(name string, cfg PoolConfig) string {\n\treturn '\${name}|\${cfg.max_open}'\n}\n\nfn main() {\n\tprintln(open_pool('db'))\n\tprintln(open_pool('db', max_open: 4))\n}\n" |
| 54 | out := run_good(v3_bin, 'pool_config_params_input', source) |
| 55 | assert out == 'db|0\ndb|4' |
| 56 | } |
| 57 | |
| 58 | fn test_interface_field_in_params_struct_codegen() { |
| 59 | v3_bin := build_v3() |
| 60 | source := 'interface Reader {\n\tread(mut buf []u8) !int\n}\n\nstruct Conn {}\n\nfn (c Conn) read(mut buf []u8) !int {\n\treturn 0\n}\n\nstruct Config {\n\treader Reader\n\tcap int\n}\n\nfn make(cfg Config) int {\n\treturn cfg.cap\n}\n\nfn main() {\n\tprintln(make(reader: Conn{}, cap: 8))\n}\n' |
| 61 | out := run_good(v3_bin, 'params_interface_field_input', source) |
| 62 | assert out == '8' |
| 63 | } |
| 64 | |
| 65 | fn test_fixed_array_field_struct_init_codegen() { |
| 66 | v3_bin := build_v3() |
| 67 | source := "struct Item {\n\tx int\n}\n\nstruct Header {\n\tdata [2]Item\n\tcur_pos int\n}\n\nfn main() {\n\tmut h := Header{}\n\th.data[0] = Item{x: 4}\n\th.data[1] = Item{x: 9}\n\th.cur_pos = 2\n\tcombined := Header{\n\t\tdata: h.data\n\t\tcur_pos: h.cur_pos\n\t}\n\tprintln('\${combined.cur_pos}|\${combined.data[0].x}|\${combined.data[1].x}')\n}\n" |
| 68 | out := run_good(v3_bin, 'fixed_array_field_struct_init_input', source) |
| 69 | assert out == '2|4|9' |
| 70 | } |
| 71 | |