|
| 1 | +import os |
| 2 | +import term |
| 3 | + |
| 4 | +const vroot = @VMODROOT |
| 5 | +const test_dir = os.join_path(vroot, 'vlib', 'v', 'tests', 'vls', 'multifile_gotodef') |
| 6 | +const main_file = os.join_path(test_dir, 'main.v') |
| 7 | +const types_file = os.join_path(test_dir, 'types.v') |
| 8 | +const types_expected = os.join_path('multifile_gotodef', 'types.v') |
| 9 | + |
| 10 | +struct TestCase { |
| 11 | + name string |
| 12 | + line int |
| 13 | + col int |
| 14 | + expected string |
| 15 | + description string |
| 16 | +} |
| 17 | + |
| 18 | +const test_cases = [ |
| 19 | + TestCase{ |
| 20 | + name: 'struct_name_cross_file' |
| 21 | + line: 4 |
| 22 | + col: 12 |
| 23 | + expected: types_expected + ':3:11' |
| 24 | + description: 'Go to struct definition in another file (MyStruct in types.v)' |
| 25 | + }, |
| 26 | + TestCase{ |
| 27 | + name: 'method_call_cross_file' |
| 28 | + line: 8 |
| 29 | + col: 18 |
| 30 | + expected: types_expected + ':14:21' |
| 31 | + description: 'Go to method definition in another file (get_value in types.v)' |
| 32 | + }, |
| 33 | + TestCase{ |
| 34 | + name: 'struct_field_cross_file' |
| 35 | + line: 11 |
| 36 | + col: 22 |
| 37 | + expected: types_expected + ':4:1' |
| 38 | + description: 'Go to struct field definition in another file (value in types.v)' |
| 39 | + }, |
| 40 | + TestCase{ |
| 41 | + name: 'enum_value_cross_file' |
| 42 | + line: 14 |
| 43 | + col: 15 |
| 44 | + expected: types_expected + ':9:1' |
| 45 | + description: 'Go to enum value definition in another file (first in types.v)' |
| 46 | + }, |
| 47 | + TestCase{ |
| 48 | + name: 'enum_short_form_cross_file' |
| 49 | + line: 18 |
| 50 | + col: 4 |
| 51 | + expected: types_expected + ':10:1' |
| 52 | + description: 'Go to enum value definition from short form in match (.second in types.v)' |
| 53 | + }, |
| 54 | +] |
| 55 | + |
| 56 | +fn test_multifile_goto_definition() { |
| 57 | + mut total_errors := 0 |
| 58 | + mut passed := 0 |
| 59 | + |
| 60 | + // Change to vls directory so relative paths match expected output |
| 61 | + original_dir := os.getwd() |
| 62 | + vls_dir := os.join_path(vroot, 'vlib', 'v', 'tests', 'vls') |
| 63 | + os.chdir(vls_dir) or { panic(err) } |
| 64 | + defer { |
| 65 | + os.chdir(original_dir) or {} |
| 66 | + } |
| 67 | + |
| 68 | + for tc in test_cases { |
| 69 | + cmd := 'v -w -check -json-errors -nocolor -vls-mode -line-info "${main_file}:${tc.line}:gd^${tc.col}" ${os.quoted_path('multifile_gotodef')}' |
| 70 | + res := os.execute(cmd) |
| 71 | + |
| 72 | + if res.exit_code < 0 { |
| 73 | + println('${term.red('FAIL')} ${tc.name}: Command failed to execute') |
| 74 | + println(' Command: ${cmd}') |
| 75 | + total_errors++ |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + res_output := $if windows { |
| 80 | + res.output.replace('\r\n', '\n').trim_space() |
| 81 | + } $else { |
| 82 | + res.output.trim_space() |
| 83 | + } |
| 84 | + |
| 85 | + if tc.expected != res_output { |
| 86 | + println('${term.red('FAIL')} ${tc.name}') |
| 87 | + println(' Description: ${tc.description}') |
| 88 | + println(' Line ${tc.line}, Column ${tc.col}') |
| 89 | + println(' Expected: ${tc.expected}') |
| 90 | + println(' Got: ${res_output}') |
| 91 | + total_errors++ |
| 92 | + } else { |
| 93 | + println('${term.green('OK ')} ${tc.name}: ${tc.description}') |
| 94 | + passed++ |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + println('') |
| 99 | + println('${term.header('Summary:', '=')}') |
| 100 | + println('Passed: ${passed}/${test_cases.len}') |
| 101 | + if total_errors > 0 { |
| 102 | + println('${term.red('Failed:')} ${total_errors}') |
| 103 | + } |
| 104 | + |
| 105 | + assert total_errors == 0, 'Some tests failed' |
| 106 | +} |
0 commit comments