Skip to content

Commit 27da270

Browse files
committed
Simplify tests with rename_example
- Make rename_example a `.ex` file so we can later test multi-file refactors - Simplify rename_example to make it easier to follow - Extract some common patterns out in the tests
1 parent aecdfa7 commit 27da270

File tree

3 files changed

+128
-174
lines changed

3 files changed

+128
-174
lines changed

apps/language_server/test/providers/rename_test.exs

Lines changed: 113 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,18 @@ defmodule ElixirLS.LanguageServer.Providers.RenameTest do
3636
# _a + b
3737
{line, char} = {3, 5}
3838

39-
assert {:ok, %{"documentChanges" => changes}} =
40-
Rename.rename(%SourceFile{text: text, version: 0}, @fake_uri, line, char, "test")
41-
42-
assert %{
43-
"textDocument" => %{
44-
"uri" => @fake_uri,
45-
"version" => 1
46-
},
47-
"edits" => [
48-
%{
49-
"range" => %{end: %{character: 11, line: 1}, start: %{character: 10, line: 1}},
50-
"newText" => "test"
51-
},
52-
%{
53-
"range" => %{end: %{character: 5, line: 2}, start: %{character: 4, line: 2}},
54-
"newText" => "test"
55-
}
56-
]
57-
} == List.first(changes)
39+
edits =
40+
Rename.rename(%SourceFile{text: text, version: 0}, @fake_uri, line, char, "test")
41+
|> assert_return_structure_and_get_edits(@fake_uri, 1)
42+
43+
expected_edits =
44+
[
45+
%{line: 1, start_char: 10, end_char: 11},
46+
%{line: 2, start_char: 4, end_char: 5}
47+
]
48+
|> get_expected_edits("test")
49+
50+
assert sort_edit_by_start_line(edits) == expected_edits
5851
end
5952

6053
test "nema -> name" do
@@ -69,112 +62,91 @@ defmodule ElixirLS.LanguageServer.Providers.RenameTest do
6962
# "Hello " <> ne_ma
7063
{line, char} = {3, 19}
7164

72-
assert {:ok, %{"documentChanges" => [changes]}} =
73-
Rename.rename(
74-
%SourceFile{text: text, version: 0},
75-
@fake_uri,
76-
line,
77-
char,
78-
"name"
79-
)
80-
81-
assert %{
82-
"textDocument" => %{
83-
"uri" => @fake_uri,
84-
"version" => 1
85-
},
86-
"edits" => [
87-
%{
88-
"range" => %{end: %{character: 16, line: 1}, start: %{character: 12, line: 1}},
89-
"newText" => "name"
90-
},
91-
%{
92-
"range" => %{end: %{character: 20, line: 2}, start: %{character: 16, line: 2}},
93-
"newText" => "name"
94-
}
95-
]
96-
} == changes
65+
edits =
66+
Rename.rename(
67+
%SourceFile{text: text, version: 0},
68+
@fake_uri,
69+
line,
70+
char,
71+
"name"
72+
)
73+
|> assert_return_structure_and_get_edits(@fake_uri, 1)
74+
75+
expected_edits =
76+
[
77+
%{line: 1, start_char: 12, end_char: 16},
78+
%{line: 2, start_char: 16, end_char: 20}
79+
]
80+
|> get_expected_edits("name")
81+
82+
assert sort_edit_by_start_line(edits) == expected_edits
9783
end
9884
end
9985

10086
describe "renaming local function" do
101-
test "create_message -> store_message" do
102-
file_path = FixtureHelpers.get_path("rename_example.exs")
87+
test "subtract -> new_subtract" do
88+
file_path = FixtureHelpers.get_path("rename_example.ex")
10389
text = File.read!(file_path)
10490
uri = SourceFile.path_to_uri(file_path)
10591

106-
# |> _create_message
107-
{line, char} = {28, 8}
108-
109-
assert {:ok, %{"documentChanges" => [changes]}} =
110-
Rename.rename(
111-
%SourceFile{text: text, version: 0},
112-
uri,
113-
line,
114-
char,
115-
"store_message"
116-
)
117-
118-
assert %{
119-
"textDocument" => %{
120-
"uri" => uri,
121-
"version" => 1
122-
},
123-
"edits" => [
124-
%{
125-
"newText" => "store_message",
126-
"range" => %{end: %{character: 21, line: 43}, start: %{character: 7, line: 43}}
127-
},
128-
%{
129-
"newText" => "store_message",
130-
"range" => %{end: %{character: 21, line: 27}, start: %{character: 7, line: 27}}
131-
}
132-
]
133-
} == changes
92+
# d = subtract(a, b)
93+
{line, char} = {6, 10}
94+
95+
edits =
96+
Rename.rename(
97+
%SourceFile{text: text, version: 0},
98+
uri,
99+
line,
100+
char,
101+
"new_subtract"
102+
)
103+
|> assert_return_structure_and_get_edits(uri, 1)
104+
105+
expected_edits =
106+
[
107+
%{line: 5, start_char: 8, end_char: 16},
108+
%{line: 13, start_char: 7, end_char: 15}
109+
]
110+
|> get_expected_edits("new_subtract")
111+
112+
assert sort_edit_by_start_line(edits) == expected_edits
134113
end
135114

136115
test "rename function with multiple heads: handle_error -> handle_errors" do
137-
file_path = FixtureHelpers.get_path("rename_example.exs")
116+
file_path = FixtureHelpers.get_path("rename_example.ex")
138117
text = File.read!(file_path)
139118
uri = SourceFile.path_to_uri(file_path)
140119

141-
{line, char} = {29, 8}
142-
143-
assert {:ok, %{"documentChanges" => [changes]}} =
144-
Rename.rename(
145-
%SourceFile{text: text, version: 0},
146-
uri,
147-
line,
148-
char,
149-
"handle_errors"
150-
)
151-
152-
assert %{
153-
"textDocument" => %{
154-
"uri" => uri,
155-
"version" => 1
156-
},
157-
"edits" => [
158-
%{
159-
"newText" => "handle_errors",
160-
"range" => %{end: %{character: 19, line: 39}, start: %{character: 7, line: 39}}
161-
},
162-
%{
163-
"newText" => "handle_errors",
164-
"range" => %{end: %{character: 19, line: 37}, start: %{character: 7, line: 37}}
165-
},
166-
%{
167-
"newText" => "handle_errors",
168-
"range" => %{end: %{character: 19, line: 28}, start: %{character: 7, line: 28}}
169-
}
170-
]
171-
} == changes
120+
# c = add(a, b)
121+
{line, char} = {5, 9}
122+
123+
edits =
124+
Rename.rename(
125+
%SourceFile{text: text, version: 0},
126+
uri,
127+
line,
128+
char,
129+
"new_add"
130+
)
131+
|> assert_return_structure_and_get_edits(uri, 1)
132+
133+
expected_edits =
134+
[
135+
%{line: 4, start_char: 8, end_char: 11},
136+
%{line: 6, start_char: 4, end_char: 7},
137+
%{line: 9, start_char: 7, end_char: 10},
138+
%{line: 10, start_char: 7, end_char: 10},
139+
%{line: 11, start_char: 7, end_char: 10}
140+
]
141+
|> get_expected_edits("new_add")
142+
143+
assert sort_edit_by_start_line(edits) == expected_edits
172144
end
173145
end
174146

175147
describe "not yet (fully) supported/working renaming cases" do
176148
test "rename started with cursor at function definition" do
177-
file_path = FixtureHelpers.get_path("rename_example.exs")
149+
file_path = FixtureHelpers.get_path("rename_example.ex")
178150
text = File.read!(file_path)
179151
uri = SourceFile.path_to_uri(file_path)
180152

@@ -212,4 +184,38 @@ defmodule ElixirLS.LanguageServer.Providers.RenameTest do
212184
} == List.first(changes)
213185
end
214186
end
187+
188+
defp get_expected_edits(edits, new_text) when is_list(edits),
189+
do: Enum.map(edits, &get_expected_edits(&1, new_text))
190+
191+
defp get_expected_edits(%{line: line, start_char: start_char, end_char: end_char}, new_text) do
192+
%{
193+
"newText" => new_text,
194+
"range" => %{
195+
start: %{line: line, character: start_char},
196+
end: %{line: line, character: end_char}
197+
}
198+
}
199+
end
200+
201+
defp assert_return_structure_and_get_edits(rename_result, uri, version) do
202+
assert {:ok,
203+
%{
204+
"documentChanges" => [
205+
%{
206+
"textDocument" => %{
207+
"uri" => ^uri,
208+
"version" => ^version
209+
},
210+
"edits" => edits
211+
}
212+
]
213+
}} = rename_result
214+
215+
edits
216+
end
217+
218+
defp sort_edit_by_start_line(edits) do
219+
Enum.sort(edits, &(&1["range"].start.line < &2["range"].start.line))
220+
end
215221
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule ElixirLS.Test.RenameExample do
2+
def main do
3+
a = 5
4+
b = 10
5+
c = add(a, b)
6+
d = subtract(a, b)
7+
add(c, d)
8+
end
9+
10+
defp add(a, b)
11+
defp add(a, b) when is_integer(a) and is_integer(b), do: a + b
12+
defp add(a, b) when is_binary(a) and is_binary(b), do: a <> b
13+
14+
defp subtract(a, b), do: a - b
15+
end

apps/language_server/test/support/fixtures/rename_example.exs

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)