|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from setup_tests import path_to_uri, run_request, test_dir, write_rpc_request |
| 4 | + |
| 5 | + |
| 6 | +def validate_def(result_array, checks): |
| 7 | + # If no definition is given result is None |
| 8 | + if result_array is None: |
| 9 | + assert not checks[0] |
| 10 | + return None |
| 11 | + assert result_array["uri"] == path_to_uri(checks[2]) |
| 12 | + assert result_array["range"]["start"]["line"] == checks[0] |
| 13 | + assert result_array["range"]["start"]["line"] == checks[1] |
| 14 | + |
| 15 | + |
| 16 | +def def_request(uri: Path, line, char): |
| 17 | + return write_rpc_request( |
| 18 | + 1, |
| 19 | + "textDocument/definition", |
| 20 | + { |
| 21 | + "textDocument": {"uri": str(uri)}, |
| 22 | + "position": {"line": line - 1, "character": char - 1}, |
| 23 | + }, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def test_def_fun_sub_fixed(): |
| 28 | + """Test that going to definition of a function or submodule works.""" |
| 29 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 30 | + file_path = test_dir / "test_prog.f08" |
| 31 | + string += def_request(file_path, 13, 7) |
| 32 | + string += def_request(file_path, 14, 7) |
| 33 | + errcode, results = run_request(string) |
| 34 | + assert errcode == 0 |
| 35 | + fixed_path = str(test_dir / "subdir" / "test_fixed.f") |
| 36 | + ref_res = [[0, 0, fixed_path], [22, 22, fixed_path]] |
| 37 | + assert len(ref_res) == len(results) - 1 |
| 38 | + for i in range(len(ref_res)): |
| 39 | + validate_def(results[i + 1], ref_res[i]) |
| 40 | + |
| 41 | + |
| 42 | +def test_def_variable(): |
| 43 | + """Test that going to definition of a variable works.""" |
| 44 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 45 | + file_path = test_dir / "test_prog.f08" |
| 46 | + string += def_request(file_path, 21, 8) |
| 47 | + errcode, results = run_request(string) |
| 48 | + assert errcode == 0 |
| 49 | + ref_res = [[10, 10, str(test_dir / "test_prog.f08")]] |
| 50 | + assert len(ref_res) == len(results) - 1 |
| 51 | + for i in range(len(ref_res)): |
| 52 | + validate_def(results[i + 1], ref_res[i]) |
| 53 | + |
| 54 | + |
| 55 | +def test_def_type_bound_procedure1(): |
| 56 | + """Test that going to definition of type bound procedure works.""" |
| 57 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 58 | + file_path = test_dir / "test_prog.f08" |
| 59 | + string += def_request(file_path, 22, 21) |
| 60 | + errcode, results = run_request(string) |
| 61 | + assert errcode == 0 |
| 62 | + ref_res = [[21, 21, str(test_dir / "subdir" / "test_free.f90")]] |
| 63 | + assert len(ref_res) == len(results) - 1 |
| 64 | + for i in range(len(ref_res)): |
| 65 | + validate_def(results[i + 1], ref_res[i]) |
| 66 | + |
| 67 | + |
| 68 | +def test_def_type_bound_procedure2(): |
| 69 | + """Test that going to definition of type bound procedure works.""" |
| 70 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 71 | + file_path = test_dir / "test_prog.f08" |
| 72 | + string += def_request(file_path, 22, 43) |
| 73 | + errcode, results = run_request(string) |
| 74 | + assert errcode == 0 |
| 75 | + ref_res = [[14, 14, str(test_dir / "subdir" / "test_free.f90")]] |
| 76 | + assert len(ref_res) == len(results) - 1 |
| 77 | + for i in range(len(ref_res)): |
| 78 | + validate_def(results[i + 1], ref_res[i]) |
| 79 | + |
| 80 | + |
| 81 | +def test_def_type_nested_variable(): |
| 82 | + """Test that going to definition of type bound nested variables works.""" |
| 83 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 84 | + file_path = test_dir / "test_prog.f08" |
| 85 | + string += def_request(file_path, 24, 27) |
| 86 | + errcode, results = run_request(string) |
| 87 | + assert errcode == 0 |
| 88 | + ref_res = [[5, 5, str(test_dir / "subdir" / "test_free.f90")]] |
| 89 | + assert len(ref_res) == len(results) - 1 |
| 90 | + for i in range(len(ref_res)): |
| 91 | + validate_def(results[i + 1], ref_res[i]) |
| 92 | + |
| 93 | + |
| 94 | +def test_def_type_in_submod_function(): |
| 95 | + """Test that going into the definition of a type bound function in a submodule""" |
| 96 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 97 | + file_path = test_dir / "subdir" / "test_submod.F90" |
| 98 | + string += def_request(file_path, 31, 13) |
| 99 | + errcode, results = run_request(string) |
| 100 | + assert errcode == 0 |
| 101 | + ref_res = [[1, 1, str(test_dir / "subdir" / "test_submod.F90")]] |
| 102 | + assert len(ref_res) == len(results) - 1 |
| 103 | + for i in range(len(ref_res)): |
| 104 | + validate_def(results[i + 1], ref_res[i]) |
| 105 | + |
| 106 | + |
| 107 | +def test_def_type_in_submod_procedure(): |
| 108 | + """Test that going into the definition of a type bound procedure in a submodule""" |
| 109 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 110 | + file_path = test_dir / "subdir" / "test_submod.F90" |
| 111 | + string += def_request(file_path, 36, 13) |
| 112 | + errcode, results = run_request(string) |
| 113 | + assert errcode == 0 |
| 114 | + ref_res = [[1, 1, str(test_dir / "subdir" / "test_submod.F90")]] |
| 115 | + assert len(ref_res) == len(results) - 1 |
| 116 | + for i in range(len(ref_res)): |
| 117 | + validate_def(results[i + 1], ref_res[i]) |
| 118 | + |
| 119 | + |
| 120 | +def test_def_include_file(): |
| 121 | + """Test that going into the location of an include file works.""" |
| 122 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 123 | + file_path = test_dir / "test_inc.f90" |
| 124 | + string += def_request(file_path, 3, 16) |
| 125 | + errcode, results = run_request(string) |
| 126 | + assert errcode == 0 |
| 127 | + ref_res = [[2, 2, str(test_dir / "subdir" / "test_inc2.f90")]] |
| 128 | + assert len(ref_res) == len(results) - 1 |
| 129 | + for i in range(len(ref_res)): |
| 130 | + validate_def(results[i + 1], ref_res[i]) |
| 131 | + |
| 132 | + |
| 133 | +def test_def_include_variable1(): |
| 134 | + """Test that going to definition of a variable in an include file works.""" |
| 135 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 136 | + file_path = test_dir / "test_inc.f90" |
| 137 | + string += def_request(file_path, 11, 3) |
| 138 | + errcode, results = run_request(string) |
| 139 | + assert errcode == 0 |
| 140 | + ref_res = [[0, 0, str(test_dir / "subdir" / "test_inc2.f90")]] |
| 141 | + assert len(ref_res) == len(results) - 1 |
| 142 | + for i in range(len(ref_res)): |
| 143 | + validate_def(results[i + 1], ref_res[i]) |
| 144 | + |
| 145 | + |
| 146 | +def test_def_include_variable2(): |
| 147 | + """Test that going to definition of a variable in an include file works.""" |
| 148 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 149 | + file_path = test_dir / "subdir" / "test_inc2.f90" |
| 150 | + string += def_request(file_path, 4, 3) |
| 151 | + errcode, results = run_request(string) |
| 152 | + assert errcode == 0 |
| 153 | + ref_res = [[4, 4, str(test_dir / "test_inc.f90")]] |
| 154 | + assert len(ref_res) == len(results) - 1 |
| 155 | + for i in range(len(ref_res)): |
| 156 | + validate_def(results[i + 1], ref_res[i]) |
| 157 | + |
| 158 | + |
| 159 | +def test_def_include_file_missing(): |
| 160 | + """Test that going to the definition of a missing file will not break fortls""" |
| 161 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 162 | + file_path = test_dir / "test_inc.f90" |
| 163 | + string += def_request(file_path, 13, 14) |
| 164 | + errcode, results = run_request(string) |
| 165 | + assert errcode == 0 |
| 166 | + ref_res = [[None]] |
| 167 | + assert len(ref_res) == len(results) - 1 |
| 168 | + for i in range(len(ref_res)): |
| 169 | + validate_def(results[i + 1], ref_res[i]) |
| 170 | + |
| 171 | + |
| 172 | +def test_def_rename_only_variable(): |
| 173 | + """Test that going to definition of a renamed list variable will take you |
| 174 | + to the original definition. |
| 175 | + """ |
| 176 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 177 | + file_path = test_dir / "subdir" / "test_rename.F90" |
| 178 | + string += def_request(file_path, 14, 6) |
| 179 | + errcode, results = run_request(string) |
| 180 | + assert errcode == 0 |
| 181 | + ref_res = [[6, 6, str(test_dir / "subdir" / "test_rename.F90")]] |
| 182 | + assert len(ref_res) == len(results) - 1 |
| 183 | + for i in range(len(ref_res)): |
| 184 | + validate_def(results[i + 1], ref_res[i]) |
| 185 | + |
| 186 | + |
| 187 | +def test_def_rename_only_variable_nested(): |
| 188 | + """Test that going to definition of a renamed list variable will take you |
| 189 | + to the original definition, tests the multiply renamed case. |
| 190 | + """ |
| 191 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 192 | + file_path = test_dir / "subdir" / "test_rename.F90" |
| 193 | + string += def_request(file_path, 15, 6) |
| 194 | + errcode, results = run_request(string) |
| 195 | + assert errcode == 0 |
| 196 | + ref_res = [[1, 1, str(test_dir / "subdir" / "test_rename.F90")]] |
| 197 | + assert len(ref_res) == len(results) - 1 |
| 198 | + for i in range(len(ref_res)): |
| 199 | + validate_def(results[i + 1], ref_res[i]) |
| 200 | + |
| 201 | + |
| 202 | +def test_def_function_implicit_result_variable(): |
| 203 | + """Test that going to definition on the implicitly defined variable RESULT |
| 204 | + works. |
| 205 | + """ |
| 206 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 207 | + file_path = test_dir / "hover" / "functions.f90" |
| 208 | + string += def_request(file_path, 4, 18) |
| 209 | + errcode, results = run_request(string) |
| 210 | + assert errcode == 0 |
| 211 | + ref_res = [[3, 3, str(test_dir / "hover" / "functions.f90")]] |
| 212 | + assert len(ref_res) == len(results) - 1 |
| 213 | + for i in range(len(ref_res)): |
| 214 | + validate_def(results[i + 1], ref_res[i]) |
0 commit comments