Skip to content

Commit cc85fea

Browse files
add wildcard function matcher (#51)
* add wildcard matcher * Add test for assert_call with a wildcard function name * Use `:_` instead Co-authored-by: Angelika Tyborska <[email protected]>
1 parent bd7dc58 commit cc85fea

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

lib/elixir_analyzer/exercise_test/assert_call/compiler.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCall.Compiler do
129129
@spec matching_function_call?(Macro.t(), nil | AssertCall.function_signature()) :: boolean()
130130
def matching_function_call?(_node, nil), do: false
131131

132+
def matching_function_call?(
133+
{{:., _, [{:__aliases__, _, module_path}, _name]}, _, _args},
134+
{module_path, :_}
135+
) do
136+
true
137+
end
138+
132139
def matching_function_call?(
133140
{{:., _, [{:__aliases__, _, module_path}, name]}, _, _args},
134141
{module_path, name}

test/elixir_analyzer/exercise_test/assert_call_test.exs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
66
comments: [] do
77
defmodule AssertCallVerification do
88
def function() do
9+
List.first([1, 2, 3])
910
result = helper()
1011
IO.puts(result)
1112

@@ -29,6 +30,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
2930
] do
3031
defmodule AssertCallVerification do
3132
def function() do
33+
List.last([1, 2, 3])
3234
private_helper() |> IO.puts()
3335
end
3436

@@ -49,6 +51,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
4951
] do
5052
defmodule AssertCallVerification do
5153
def function() do
54+
List.first([1, 2, 3])
5255
other()
5356
IO.puts("1")
5457
end
@@ -75,6 +78,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
7578
] do
7679
defmodule AssertCallVerification do
7780
def function() do
81+
List.flatten([1, 2, 3])
7882
result = helper()
7983
private_helper()
8084
end
@@ -95,6 +99,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
9599
] do
96100
defmodule AssertCallVerification do
97101
def function() do
102+
List.first([1, 2, 3])
98103
result = helper()
99104
private_helper() |> other()
100105
end
@@ -112,4 +117,50 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
112117
end
113118
end
114119
end
120+
121+
test_exercise_analysis "missing call to a List function in function/0 solution",
122+
comments: [
123+
"didn't find a call to a List function in function/0"
124+
] do
125+
defmodule AssertCallVerification do
126+
def function() do
127+
result = helper()
128+
IO.puts(result)
129+
130+
private_helper() |> IO.puts()
131+
end
132+
133+
def helper do
134+
List.first([1, 2, 3])
135+
:helped
136+
end
137+
138+
defp private_helper do
139+
:privately_helped
140+
end
141+
end
142+
end
143+
144+
test_exercise_analysis "missing call to a List function in solution",
145+
comments: [
146+
"didn't find a call to a List function",
147+
"didn't find a call to a List function in function/0"
148+
] do
149+
defmodule AssertCallVerification do
150+
def function() do
151+
result = helper()
152+
IO.puts(result)
153+
154+
private_helper() |> IO.puts()
155+
end
156+
157+
def helper do
158+
:helped
159+
end
160+
161+
defp private_helper do
162+
:privately_helped
163+
end
164+
end
165+
end
115166
end

test/elixir_analyzer/exercise_test/assert_not_call_test.exs renamed to test/elixir_analyzer/exercise_test/assert_no_call_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,24 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertNoCallTest do
9696
end
9797
end
9898
end
99+
100+
test_exercise_analysis "test wildcard",
101+
comments: [
102+
"don't call List module functions"
103+
] do
104+
defmodule AssertNoCallVerification do
105+
def function() do
106+
"something"
107+
List.last([])
108+
end
109+
110+
def helper do
111+
:helped
112+
end
113+
114+
defp private_helper do
115+
:privately_helped
116+
end
117+
end
118+
end
99119
end

test/support/analyzer_verification/assert_call.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,17 @@ defmodule ElixirAnalyzer.Support.AnalyzerVerification.AssertCall do
4444
calling_fn module: AssertCallVerification, name: :function
4545
comment "didn't find a call to IO.puts/1 in function/0"
4646
end
47+
48+
assert_call "finds call to any List function anywhere" do
49+
type :informational
50+
called_fn module: List, name: :_
51+
comment "didn't find a call to a List function"
52+
end
53+
54+
assert_call "finds call to any List function anywhere" do
55+
type :informational
56+
called_fn module: List, name: :_
57+
calling_fn module: AssertCallVerification, name: :function
58+
comment "didn't find a call to a List function in function/0"
59+
end
4760
end

test/support/analyzer_verification/assert_no_call.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ defmodule ElixirAnalyzer.Support.AnalyzerVerification.AssertNoCall do
3131
called_fn module: Atom, name: :to_string
3232
comment "found a call to Atom.to_string/1 in helper/0 function in solution"
3333
end
34+
35+
assert_no_call "does not call any function from List module" do
36+
type :informational
37+
called_fn module: List, name: :_
38+
comment "don't call List module functions"
39+
end
3440
end

0 commit comments

Comments
 (0)