Skip to content

Commit 7e18bc0

Browse files
committed
Add a normalization for better flexible in AstData file
1 parent 90d26e6 commit 7e18bc0

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

test/gradient/ast_specifier_test.exs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Gradient.AstSpecifierTest do
33
doctest Gradient.AstSpecifier
44

55
alias Gradient.AstSpecifier
6+
alias Gradient.AstData
67

78
import Gradient.TestHelpers
89

@@ -11,11 +12,14 @@ defmodule Gradient.AstSpecifierTest do
1112
end
1213

1314
describe "specifying expression" do
14-
for {name, args, expected} <- Gradient.AstData.ast_data() do
15+
for {name, args, expected} <- AstData.ast_data() do
1516
test "#{name}" do
1617
{ast, tokens, opts} = unquote(Macro.escape(args))
17-
expected = unquote(Macro.escape(expected))
18-
assert expected == elem(AstSpecifier.mapper(ast, tokens, opts), 0)
18+
expected = AstData.normalize_expression(unquote(Macro.escape(expected)))
19+
20+
actual = AstData.normalize_expression(elem(AstSpecifier.mapper(ast, tokens, opts), 0))
21+
22+
assert expected == actual
1923
end
2024
end
2125
end

test/support/ast_data.ex

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
defmodule Gradient.AstData do
2+
@moduledoc """
3+
Stores the test cases data for expressions line specifying. To increase the flexibility
4+
the data need normalization before equality assertion. Thus we check only the line change,
5+
not the exact value and there is no need to update expected values when the file content
6+
changes.
7+
8+
This way of testing is useful only for more complex expressions in which we can observe
9+
some line change. For example, look at the pipe operator cases.
10+
"""
11+
212
require Gradient.Debug
313
import Gradient.Debug, only: [elixir_to_ast: 1]
414
import Gradient.TestHelpers
@@ -59,14 +69,6 @@ defmodule Gradient.AstData do
5969
[{:integer, 56, 1}, {:atom, 55, :ok}]}}
6070
end
6171

62-
defp example do
63-
{__ENV__.function,
64-
{__ENV__.line,
65-
elixir_to_ast do
66-
:ok
67-
end, __ENV__.line}, _expected = {}}
68-
end
69-
7072
@spec ast_data() :: [
7173
{atom(), {Types.abstract_expr(), Types.tokens(), Types.options()}, tuple()}
7274
]
@@ -77,4 +79,17 @@ defmodule Gradient.AstData do
7779
{name, {ast, tokens, [line: start_line, end_line: end_line]}, expected}
7880
end)
7981
end
82+
83+
def normalize_expression(expression) do
84+
{expression, _} =
85+
:erl_parse.mapfold_anno(
86+
fn anno, acc ->
87+
{{:erl_anno.line(anno) - acc, :erl_anno.column(anno)}, acc}
88+
end,
89+
:erl_anno.line(elem(expression, 1)),
90+
expression
91+
)
92+
93+
expression
94+
end
8095
end

0 commit comments

Comments
 (0)