|
| 1 | +from setup_tests import ( |
| 2 | + check_post_msg, |
| 3 | + path_to_uri, |
| 4 | + run_request, |
| 5 | + test_dir, |
| 6 | + write_rpc_request, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +def rename_request(new_name: str, file_path, ln: int, ch: int): |
| 11 | + return write_rpc_request( |
| 12 | + 1, |
| 13 | + "textDocument/rename", |
| 14 | + { |
| 15 | + "newName": new_name, |
| 16 | + "textDocument": {"uri": str(file_path)}, |
| 17 | + "position": {"line": ln, "character": ch}, |
| 18 | + }, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def check_rename_response(response: dict, references: dict): |
| 23 | + # Loop over URI's if the change spans multiple files there will be more than 1 |
| 24 | + for uri, changes in response.items(): |
| 25 | + refs = references[uri] |
| 26 | + # Loop over all the changes in the current URI, instances of object |
| 27 | + for c, r in zip(changes, refs): |
| 28 | + assert c["range"] == r["range"] |
| 29 | + assert c["newText"] == r["newText"] |
| 30 | + |
| 31 | + |
| 32 | +def create(new_text: str, sln: int, sch: int, eln: int, ech: int): |
| 33 | + return { |
| 34 | + "range": { |
| 35 | + "start": {"line": sln, "character": sch}, |
| 36 | + "end": {"line": eln, "character": ech}, |
| 37 | + }, |
| 38 | + "newText": new_text, |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +def test_rename_var(): |
| 43 | + """ "Test simple variable rename""" |
| 44 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 45 | + file_path = test_dir / "test_prog.f08" |
| 46 | + string += rename_request("str_rename", file_path, 5, 25) |
| 47 | + errcode, results = run_request(string) |
| 48 | + assert errcode == 0 |
| 49 | + ref = {} |
| 50 | + ref[path_to_uri(str(file_path))] = [create("str_rename", 5, 20, 5, 29)] |
| 51 | + check_rename_response(results[1]["changes"], ref) |
| 52 | + |
| 53 | + |
| 54 | +def test_rename_var_across_module(): |
| 55 | + """Test renaming objects like variables across modules works""" |
| 56 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 57 | + file_path = test_dir / "test_prog.f08" |
| 58 | + string += rename_request("new_module_var", file_path, 26, 15) |
| 59 | + errcode, results = run_request(string) |
| 60 | + assert errcode == 0 |
| 61 | + ref = {} |
| 62 | + ref[path_to_uri(str(test_dir / "subdir" / "test_free.f90"))] = [ |
| 63 | + create("new_module_var", 32, 11, 32, 26) |
| 64 | + ] |
| 65 | + ref[path_to_uri(str(file_path))] = [create("new_module_var", 2, 44, 2, 59)] |
| 66 | + ref[path_to_uri(str(file_path))].append(create("new_module_var", 26, 8, 26, 23)) |
| 67 | + |
| 68 | + check_rename_response(results[1]["changes"], ref) |
| 69 | + |
| 70 | + |
| 71 | +def test_rename_empty(): |
| 72 | + """Test that renaming nothing will not error""" |
| 73 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "rename")}) |
| 74 | + file_path = test_dir / "rename" / "test_rename_imp_type_bound_proc.f90" |
| 75 | + string += rename_request("bar", file_path, 9, 0) |
| 76 | + errcode, results = run_request(string, ["-n", "1"]) |
| 77 | + assert errcode == 0 |
| 78 | + assert results[1] is None |
| 79 | + |
| 80 | + |
| 81 | +def test_rename_member_type_ptr(): |
| 82 | + """Test that renaming type bound pointers of procedure methods rename |
| 83 | + only the pointer and not the implementation, even if the pointer and the |
| 84 | + implementation share the same name |
| 85 | + """ |
| 86 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 87 | + file_path = test_dir / "test_prog.f08" |
| 88 | + string += rename_request("bp_rename", file_path, 18, 25) |
| 89 | + errcode, results = run_request(string) |
| 90 | + assert errcode == 0 |
| 91 | + ref = {} |
| 92 | + ref[path_to_uri(str(file_path))] = [create("bp_rename", 18, 16, 18, 26)] |
| 93 | + ref[path_to_uri(str(test_dir / "subdir" / "test_free.f90"))] = [ |
| 94 | + create("bp_rename", 15, 27, 15, 37) |
| 95 | + ] |
| 96 | + check_rename_response(results[1]["changes"], ref) |
| 97 | + |
| 98 | + |
| 99 | +def test_rename_member_type_ptr_null(): |
| 100 | + """Test renaming type bound pointers of procedure methods works when pointing to |
| 101 | + null |
| 102 | + """ |
| 103 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 104 | + file_path = test_dir / "test_prog.f08" |
| 105 | + string += rename_request("bp_rename", file_path, 17, 25) |
| 106 | + errcode, results = run_request(string) |
| 107 | + assert errcode == 0 |
| 108 | + ref = {} |
| 109 | + ref[path_to_uri(str(file_path))] = [create("bp_rename", 17, 16, 17, 28)] |
| 110 | + ref[path_to_uri(str(test_dir / "subdir" / "test_free.f90"))] = [ |
| 111 | + create("bp_rename", 11, 43, 11, 55) |
| 112 | + ] |
| 113 | + check_rename_response(results[1]["changes"], ref) |
| 114 | + |
| 115 | + |
| 116 | +def test_rename_type_bound_proc_no_ptr(): |
| 117 | + """Test renaming type bound pointers of procedure methods works when no pointer |
| 118 | + is setup. Requesting to rename the procedure should rename, the implementation |
| 119 | + and the Method itself i.e. call self%foo() |
| 120 | +
|
| 121 | + Requesting to rename the implementation should also rename the procedure |
| 122 | + and all the locations it is called in |
| 123 | + """ |
| 124 | + root = test_dir / "rename" |
| 125 | + string = write_rpc_request(1, "initialize", {"rootPath": str(root)}) |
| 126 | + file_path = root / "test_rename_imp_type_bound_proc.f90" |
| 127 | + # Rename the procedure name and check if implementation also renames |
| 128 | + string += rename_request("bar", file_path, 5, 23) |
| 129 | + # Rename the implementation name and check if declaration, references change |
| 130 | + string += rename_request("bar", file_path, 10, 18) |
| 131 | + errcode, results = run_request(string) |
| 132 | + assert errcode == 0 |
| 133 | + ref = {} |
| 134 | + ref[path_to_uri(str(file_path))] = [create("bar", 5, 21, 5, 24)] |
| 135 | + ref[path_to_uri(str(file_path))].append(create("bar", 10, 15, 10, 18)) |
| 136 | + ref[path_to_uri(str(file_path))].append(create("bar", 12, 18, 12, 21)) |
| 137 | + ref[path_to_uri(str(file_path))].append(create("bar", 13, 19, 13, 22)) |
| 138 | + check_rename_response(results[1]["changes"], ref) |
| 139 | + check_rename_response(results[2]["changes"], ref) |
| 140 | + |
| 141 | + |
| 142 | +def test_rename_non_existent_file(): |
| 143 | + """Test renaming type bound pointers of procedure methods works when pointing to |
| 144 | + null |
| 145 | + """ |
| 146 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)}) |
| 147 | + file_path = test_dir / "fake.f90" |
| 148 | + string += rename_request("bar", file_path, 5, 23) |
| 149 | + errcode, results = run_request(string) |
| 150 | + assert errcode == 0 |
| 151 | + assert results[1] is None |
| 152 | + |
| 153 | + |
| 154 | +def test_rename_nested(): |
| 155 | + """Test renaming heavily nested constructs works""" |
| 156 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "rename")}) |
| 157 | + file_path = test_dir / "rename" / "test_rename_nested.f90" |
| 158 | + string += rename_request("bar", file_path, 6, 23) |
| 159 | + errcode, results = run_request(string, ["-n", "1"]) |
| 160 | + assert errcode == 0 |
| 161 | + ref = {} |
| 162 | + ref[path_to_uri(str(file_path))] = [create("bar", 6, 23, 6, 26)] |
| 163 | + ref[path_to_uri(str(file_path))].append(create("bar", 9, 27, 9, 30)) |
| 164 | + check_rename_response(results[1]["changes"], ref) |
| 165 | + |
| 166 | + |
| 167 | +def test_rename_intrinsic(): |
| 168 | + """Test renaming an intrinsic function, while no other function exists |
| 169 | + with the same name, will throw an error |
| 170 | + """ |
| 171 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "rename")}) |
| 172 | + file_path = test_dir / "rename" / "test_rename_nested.f90" |
| 173 | + string += rename_request("bar", file_path, 8, 27) |
| 174 | + errcode, results = run_request(string, ["-n", "1"]) |
| 175 | + assert errcode == 0 |
| 176 | + check_post_msg(results[1], "Rename failed: Cannot rename intrinsics", 2) |
| 177 | + assert results[2] is None |
| 178 | + |
| 179 | + |
| 180 | +def test_rename_use_only_rename(): |
| 181 | + """Test renaming constructs of `use mod, only: val => root_val |
| 182 | + are handled correctly |
| 183 | + """ |
| 184 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "subdir")}) |
| 185 | + file_path = test_dir / "subdir" / "test_rename.F90" |
| 186 | + string += rename_request("bar", file_path, 13, 5) |
| 187 | + errcode, results = run_request(string, ["-n", "1"]) |
| 188 | + # FIXME: to be implemented |
| 189 | + assert errcode == 0 |
| 190 | + |
| 191 | + |
| 192 | +def test_rename_skip_intrinsic(): |
| 193 | + """Test that renaming functions named the same as intrinsic functions e.g. size() |
| 194 | + will only rename the user defined functions |
| 195 | + """ |
| 196 | + string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "rename")}) |
| 197 | + file_path = test_dir / "rename" / "test_rename_intrinsic.f90" |
| 198 | + string += rename_request("bar", file_path, 22, 13) |
| 199 | + errcode, results = run_request(string, ["-n", "1"]) |
| 200 | + # FIXME: to be implemented |
| 201 | + assert errcode == 0 |
0 commit comments