Skip to content

Commit 315ca2e

Browse files
committed
Init tests
1 parent a0db683 commit 315ca2e

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

lib/gradient/elixir_fmt.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ defmodule Gradient.ElixirFmt do
1717
def print_error(error, opts) do
1818
file = Keyword.get(opts, :filename)
1919
fmt_loc = Keyword.get(opts, :fmt_location, :verbose)
20-
opts = Keyword.put(opts, :fmt_type_fun, &ElixirType.pretty_print/1)
2120

2221
case file do
2322
nil -> :ok
2423
_ when fmt_loc == :brief -> :io.format("~s:", [file])
2524
_ -> :io.format("~s: ", [file])
2625
end
2726

28-
:io.put_chars(format_type_error(error, opts))
27+
:io.put_chars(format_error(error, opts))
28+
end
29+
30+
def format_error(error, opts) do
31+
opts = Keyword.put(opts, :fmt_type_fun, &ElixirType.pretty_print/1)
32+
format_type_error(error, opts)
2933
end
3034

3135
@impl Gradient.Fmt
4.05 KB
Binary file not shown.

test/examples/type/wrong_ret.ex

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule WrongRet do
2+
@spec ret_wrong_atom() :: atom()
3+
def ret_wrong_atom, do: 1
4+
5+
@spec ret_wrong_atom2() :: atom()
6+
def ret_wrong_atom2, do: {:ok, []}
7+
8+
@spec ret_wrong_atom3() :: atom()
9+
def ret_wrong_atom3, do: %{a: 1}
10+
11+
@spec ret_wrong_atom4() :: atom()
12+
def ret_wrong_atom4, do: false
13+
14+
@spec ret_wrong_integer() :: integer()
15+
def ret_wrong_integer, do: 1.0
16+
17+
@spec ret_wrong_integer2() :: integer()
18+
def ret_wrong_integer2, do: :ok
19+
20+
@spec ret_wrong_integer3() :: integer()
21+
def ret_wrong_integer3, do: true
22+
23+
@spec ret_wrong_integer4() :: integer()
24+
def ret_wrong_integer4, do: [1, 2, 3]
25+
26+
@spec ret_out_of_range_int() :: 1..10
27+
def ret_out_of_range_int, do: 12
28+
29+
@spec ret_wrong_boolean() :: boolean()
30+
def ret_wrong_boolean, do: :ok
31+
32+
@spec ret_wrong_boolean2() :: boolean()
33+
def ret_wrong_boolean2, do: "1234"
34+
35+
@spec ret_wrong_boolean3() :: boolean()
36+
def ret_wrong_boolean3, do: 1
37+
38+
@spec ret_wrong_boolean4() :: boolean()
39+
def ret_wrong_boolean4, do: [a: 1, b: 2]
40+
41+
@spec ret_wrong_keyword() :: keyword()
42+
def ret_wrong_keyword, do: [1, 2, 3]
43+
44+
@spec ret_wrong_tuple() :: tuple()
45+
def ret_wrong_tuple, do: %{a: 1, b: 2}
46+
47+
@spec ret_wrong_map() :: map()
48+
def ret_wrong_map, do: {:a, 1, 2}
49+
end

test/gradient/elixir_fmt_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Gradient.ElixirFmtTest do
33
doctest Gradient.ElixirFmt
44

55
alias Gradient.ElixirFmt
6+
alias Gradient.TestHelpers
7+
alias Gradient.AstSpecifier
68

79
@example_module_path "test/examples/simple_app.ex"
810

@@ -18,6 +20,28 @@ defmodule Gradient.ElixirFmtTest do
1820
assert res == expected
1921
end
2022

23+
describe "types format" do
24+
test "wrong return type" do
25+
{_tokens, ast} = load("/type/Elixir.WrongRet.beam", "/type/wrong_ret.ex")
26+
opts = []
27+
errors = type_check_file(ast, opts)
28+
for e <- errors do
29+
:io.put_chars(e)
30+
end
31+
32+
end
33+
end
34+
35+
def type_check_file(ast, opts) do
36+
forms = AstSpecifier.specify(ast)
37+
opts = Keyword.put(opts, :return_errors, true)
38+
opts = Keyword.put(opts, :forms, forms)
39+
40+
forms
41+
|> :gradualizer.type_check_forms(opts)
42+
|> Enum.map(fn {_, err} -> ElixirFmt.format_error(err, opts) end)
43+
end
44+
2145
@tag :skip
2246
test "format_expr_type_error/4" do
2347
opts = [forms: basic_erlang_forms()]

0 commit comments

Comments
 (0)