Skip to content

Commit

Permalink
add wildcard function matcher (#51)
Browse files Browse the repository at this point in the history
* add wildcard matcher

* Add test for assert_call with a wildcard function name

* Use `:_` instead

Co-authored-by: Angelika Tyborska <[email protected]>
  • Loading branch information
neenjaw and angelikatyborska authored Apr 7, 2021
1 parent bd7dc58 commit cc85fea
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/elixir_analyzer/exercise_test/assert_call/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCall.Compiler do
@spec matching_function_call?(Macro.t(), nil | AssertCall.function_signature()) :: boolean()
def matching_function_call?(_node, nil), do: false

def matching_function_call?(
{{:., _, [{:__aliases__, _, module_path}, _name]}, _, _args},
{module_path, :_}
) do
true
end

def matching_function_call?(
{{:., _, [{:__aliases__, _, module_path}, name]}, _, _args},
{module_path, name}
Expand Down
51 changes: 51 additions & 0 deletions test/elixir_analyzer/exercise_test/assert_call_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
comments: [] do
defmodule AssertCallVerification do
def function() do
List.first([1, 2, 3])
result = helper()
IO.puts(result)

Expand All @@ -29,6 +30,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
] do
defmodule AssertCallVerification do
def function() do
List.last([1, 2, 3])
private_helper() |> IO.puts()
end

Expand All @@ -49,6 +51,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
] do
defmodule AssertCallVerification do
def function() do
List.first([1, 2, 3])
other()
IO.puts("1")
end
Expand All @@ -75,6 +78,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
] do
defmodule AssertCallVerification do
def function() do
List.flatten([1, 2, 3])
result = helper()
private_helper()
end
Expand All @@ -95,6 +99,7 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
] do
defmodule AssertCallVerification do
def function() do
List.first([1, 2, 3])
result = helper()
private_helper() |> other()
end
Expand All @@ -112,4 +117,50 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertCallTest do
end
end
end

test_exercise_analysis "missing call to a List function in function/0 solution",
comments: [
"didn't find a call to a List function in function/0"
] do
defmodule AssertCallVerification do
def function() do
result = helper()
IO.puts(result)

private_helper() |> IO.puts()
end

def helper do
List.first([1, 2, 3])
:helped
end

defp private_helper do
:privately_helped
end
end
end

test_exercise_analysis "missing call to a List function in solution",
comments: [
"didn't find a call to a List function",
"didn't find a call to a List function in function/0"
] do
defmodule AssertCallVerification do
def function() do
result = helper()
IO.puts(result)

private_helper() |> IO.puts()
end

def helper do
:helped
end

defp private_helper do
:privately_helped
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,24 @@ defmodule ElixirAnalyzer.ExerciseTest.AssertNoCallTest do
end
end
end

test_exercise_analysis "test wildcard",
comments: [
"don't call List module functions"
] do
defmodule AssertNoCallVerification do
def function() do
"something"
List.last([])
end

def helper do
:helped
end

defp private_helper do
:privately_helped
end
end
end
end
13 changes: 13 additions & 0 deletions test/support/analyzer_verification/assert_call.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ defmodule ElixirAnalyzer.Support.AnalyzerVerification.AssertCall do
calling_fn module: AssertCallVerification, name: :function
comment "didn't find a call to IO.puts/1 in function/0"
end

assert_call "finds call to any List function anywhere" do
type :informational
called_fn module: List, name: :_
comment "didn't find a call to a List function"
end

assert_call "finds call to any List function anywhere" do
type :informational
called_fn module: List, name: :_
calling_fn module: AssertCallVerification, name: :function
comment "didn't find a call to a List function in function/0"
end
end
6 changes: 6 additions & 0 deletions test/support/analyzer_verification/assert_no_call.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ defmodule ElixirAnalyzer.Support.AnalyzerVerification.AssertNoCall do
called_fn module: Atom, name: :to_string
comment "found a call to Atom.to_string/1 in helper/0 function in solution"
end

assert_no_call "does not call any function from List module" do
type :informational
called_fn module: List, name: :_
comment "don't call List module functions"
end
end

0 comments on commit cc85fea

Please sign in to comment.