v2 / vlib / v / gen / c / or_block_line_info_test.v
46 lines · 44 sloc · 1.64 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import os
2
3const line_info_test_vexe = @VEXE
4
5fn test_option_propagation_panic_has_matching_line_info() {
6 tmp_dir := os.join_path(os.vtmp_dir(), 'or_block_line_info_test')
7 os.mkdir_all(tmp_dir)!
8 defer {
9 os.rmdir_all(tmp_dir) or {}
10 }
11 source_path := os.join_path(os.real_path(tmp_dir), 'option_propagation_line_info.vv')
12 os.write_file(source_path,
13 'struct Foo{}\n\nfn (f Foo) foo() int {\n\treturn 1\n}\n\nfn new_foo() ?Foo {\n\treturn none\n}\n\nfn main() {\n\ti := new_foo()?.foo()\n\tprintln(i)\n}\n')!
14 cmd := '${os.quoted_path(line_info_test_vexe)} -g -o - ${os.quoted_path(source_path)}'
15 res := os.execute(cmd)
16 assert res.exit_code == 0, '${cmd}\n${res.output}'
17 lines := res.output.replace('\r\n', '\n').split_into_lines()
18 // On Windows, #line directives use double-escaped backslashes, while
19 // panic_debug uses forward slashes, so normalize paths for comparison.
20 escaped_source_path := source_path.replace('\\', '\\\\')
21 fwd_source_path := source_path.replace('\\', '/')
22 expected_line := '#line 12 "${escaped_source_path}"'
23 expected_panic := 'builtin__panic_debug(12, builtin__tos3("${fwd_source_path}")'
24 mut main_idx := -1
25 for i, line in lines {
26 if line.contains('void main__main(void) {') {
27 main_idx = i
28 break
29 }
30 }
31 assert main_idx >= 0, res.output
32 mut directive_idx := -1
33 mut panic_idx := -1
34 for i := main_idx + 1; i < lines.len; i++ {
35 line := lines[i]
36 if line == expected_line {
37 directive_idx = i
38 }
39 if line.contains(expected_panic) {
40 panic_idx = i
41 break
42 }
43 }
44 assert panic_idx > 0, res.output
45 assert directive_idx == panic_idx - 1, 'expected `${expected_line}` immediately before `${expected_panic}`\n${res.output}'
46}
47