|
| 1 | +import os |
| 2 | +import term |
| 3 | +import v.util.diff |
| 4 | + |
| 5 | +const vroot = os.real_path(@VMODROOT) |
| 6 | +const test_file = os.join_path(vroot, 'vlib', 'v', 'tests', 'vls', 'goto_def_test_data.vv') |
| 7 | +const mod1_text_file = os.join_path(vroot, 'vlib', 'v', 'tests', 'vls', 'sample_mod1', |
| 8 | + 'sample.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: 'method_definition' |
| 21 | + line: 27 |
| 22 | + col: 13 |
| 23 | + expected: '${test_file}:20:20' |
| 24 | + description: 'Go to method definition from method call' |
| 25 | + }, |
| 26 | + TestCase{ |
| 27 | + name: 'enum_value_qualified' |
| 28 | + line: 30 |
| 29 | + col: 20 |
| 30 | + expected: '${test_file}:11:1' |
| 31 | + description: 'Go to enum value definition (qualified form: LocalEnum.first)' |
| 32 | + }, |
| 33 | + TestCase{ |
| 34 | + name: 'enum_value_short_form' |
| 35 | + line: 33 |
| 36 | + col: 3 |
| 37 | + expected: '${test_file}:12:1' |
| 38 | + description: 'Go to enum value definition (short form: .second in match)' |
| 39 | + }, |
| 40 | + TestCase{ |
| 41 | + name: 'type_alias_cast' |
| 42 | + line: 37 |
| 43 | + col: 15 |
| 44 | + expected: '${test_file}:16:5' |
| 45 | + description: 'Go to type alias definition in cast expression' |
| 46 | + }, |
| 47 | + TestCase{ |
| 48 | + name: 'sum_type_cast' |
| 49 | + line: 39 |
| 50 | + col: 13 |
| 51 | + expected: '${test_file}:18:5' |
| 52 | + description: 'Go to sum type definition in cast expression' |
| 53 | + }, |
| 54 | + TestCase{ |
| 55 | + name: 'struct_init' |
| 56 | + line: 42 |
| 57 | + col: 13 |
| 58 | + expected: '${test_file}:6:7' |
| 59 | + description: 'Go to struct definition from StructInit' |
| 60 | + }, |
| 61 | + TestCase{ |
| 62 | + name: 'variable_reference' |
| 63 | + line: 45 |
| 64 | + col: 17 |
| 65 | + expected: '${test_file}:25:5' |
| 66 | + description: 'Go to variable definition from reference' |
| 67 | + }, |
| 68 | + TestCase{ |
| 69 | + name: 'imported_enum_value' |
| 70 | + line: 48 |
| 71 | + col: 25 |
| 72 | + expected: '${mod1_text_file}:25:1' |
| 73 | + description: 'Go to imported enum value definition' |
| 74 | + }, |
| 75 | + TestCase{ |
| 76 | + name: 'field_access' |
| 77 | + line: 51 |
| 78 | + col: 16 |
| 79 | + expected: '${test_file}:7:1' |
| 80 | + description: 'Go to field definition from field access' |
| 81 | + }, |
| 82 | +] |
| 83 | + |
| 84 | +fn test_goto_definition() { |
| 85 | + mut total_errors := 0 |
| 86 | + mut passed := 0 |
| 87 | + |
| 88 | + for tc in test_cases { |
| 89 | + cmd := 'v -w -check -json-errors -nocolor -vls-mode -line-info "${test_file}:${tc.line}:gd^${tc.col}" ${os.quoted_path(test_file)}' |
| 90 | + res := os.execute(cmd) |
| 91 | + |
| 92 | + if res.exit_code < 0 { |
| 93 | + println('${term.red('FAIL')} ${tc.name}: Command failed to execute') |
| 94 | + println(' Command: ${cmd}') |
| 95 | + total_errors++ |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + res_output := $if windows { |
| 100 | + res.output.replace('\r\n', '\n').trim_space() |
| 101 | + } $else { |
| 102 | + res.output.trim_space() |
| 103 | + } |
| 104 | + |
| 105 | + if tc.expected != res_output { |
| 106 | + println('${term.red('FAIL')} ${tc.name}') |
| 107 | + println(' Description: ${tc.description}') |
| 108 | + println(' Line ${tc.line}, Column ${tc.col}') |
| 109 | + if diff_ := diff.compare_text(tc.expected, res_output) { |
| 110 | + println(' Difference:') |
| 111 | + println(diff_) |
| 112 | + } else { |
| 113 | + println(' Expected: ${tc.expected}') |
| 114 | + println(' Got: ${res_output}') |
| 115 | + } |
| 116 | + total_errors++ |
| 117 | + } else { |
| 118 | + println('${term.green('OK ')} ${tc.name}: ${tc.description}') |
| 119 | + passed++ |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + println('') |
| 124 | + println('${term.header('Summary:', '=')}') |
| 125 | + println('Passed: ${passed}/${test_cases.len}') |
| 126 | + if total_errors > 0 { |
| 127 | + println('${term.red('Failed:')} ${total_errors}') |
| 128 | + } |
| 129 | + |
| 130 | + assert total_errors == 0, 'Some tests failed' |
| 131 | +} |
0 commit comments