Skip to content

Commit dbf5d60

Browse files
committed
Add tests for ElixirChecker
1 parent e2d89be commit dbf5d60

File tree

8 files changed

+78
-0
lines changed

8 files changed

+78
-0
lines changed
1.56 KB
Binary file not shown.
1.58 KB
Binary file not shown.
1.98 KB
Binary file not shown.

test/examples/spec_after_spec.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
defmodule SpecAfterSpec do
2+
@spec convert(integer()) :: float()
3+
@spec convert(atom()) :: binary()
4+
def convert(int) when is_integer(int), do: int / 1
5+
def convert(atom) when is_atom(atom), do: to_string(atom)
6+
end

test/examples/spec_correct.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
defmodule CorrectSpec do
2+
@spec convert(integer()) :: float()
3+
def convert(int) when is_integer(int), do: int / 1
4+
@spec convert(atom()) :: binary()
5+
def convert(atom) when is_atom(atom), do: to_string(atom)
6+
end

test/examples/spec_wrong_name.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule SpecWrongName do
2+
@spec convert(integer()) :: float()
3+
def convert(int) when is_integer(int), do: int / 1
4+
5+
@spec convert(atom()) :: binary()
6+
def last_two(list) do
7+
[last, penultimate | _tail] = Enum.reverse(list)
8+
[penultimate, last]
9+
end
10+
11+
@spec last_two(atom()) :: atom()
12+
def last_three(:ok) do
13+
:ok
14+
end
15+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
defmodule Gradient.ElixirCheckerTest do
2+
use ExUnit.Case
3+
doctest Gradient.ElixirChecker
4+
5+
alias Gradient.ElixirChecker
6+
7+
import Gradient.TestHelpers
8+
9+
test "checker options" do
10+
ast = load("Elixir.SpecWrongName.beam")
11+
12+
assert [] = ElixirChecker.check(ast, ex_check: false)
13+
assert [] != ElixirChecker.check(ast, ex_check: true)
14+
end
15+
16+
test "all specs are correct" do
17+
ast = load("Elixir.CorrectSpec.beam")
18+
19+
assert [] = ElixirChecker.check(ast, ex_check: true)
20+
end
21+
22+
test "spec name doesn't match the function name" do
23+
ast = load("Elixir.SpecWrongName.beam")
24+
25+
assert [
26+
{_,
27+
{:spec_error, :wrong_spec_name, 11, :last_two, 1}},
28+
{_,
29+
{:spec_error, :wrong_spec_name, 5, :convert, 1}}
30+
] = ElixirChecker.check(ast, [])
31+
end
32+
33+
test "more than one spec per function clause is not allowed" do
34+
ast = load("Elixir.SpecAfterSpec.beam")
35+
36+
assert [
37+
{_,
38+
{:spec_error, :spec_after_spec, 3, :convert_a, 1}}
39+
] = ElixirChecker.check(ast, [])
40+
end
41+
end

test/support/helpers.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ defmodule Gradient.TestHelpers do
33

44
@examples_path "test/examples/"
55

6+
@spec load(String.t()) :: T.forms()
7+
def load(beam_file) do
8+
beam_file = String.to_charlist(@examples_path <> beam_file)
9+
10+
{:ok, {_, [abstract_code: {:raw_abstract_v1, ast}]}} =
11+
:beam_lib.chunks(beam_file, [:abstract_code])
12+
13+
ast
14+
end
15+
616
@spec load(String.t(), String.t()) :: {T.tokens(), T.forms()}
717
def load(beam_file, ex_file) do
818
beam_file = String.to_charlist(@examples_path <> beam_file)

0 commit comments

Comments
 (0)