Skip to content

Commit b49e1f5

Browse files
committed
Add pipe test and introduce AstData module for specifying expressions
1 parent 163bedf commit b49e1f5

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

test/gradient/ast_specifier_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ defmodule Gradient.AstSpecifierTest do
1010
{:ok, state}
1111
end
1212

13+
describe "specifying expression" do
14+
for {name, args, expected} <- Gradient.AstData.ast_data() do
15+
test "#{name}" do
16+
{ast, tokens, opts} = unquote(Macro.escape(args))
17+
expected = unquote(Macro.escape(expected))
18+
assert expected == elem(AstSpecifier.mapper(ast, tokens, opts), 0)
19+
end
20+
end
21+
end
22+
1323
describe "run_mappers/2" do
1424
test "messy test on simple_app" do
1525
{tokens, ast} = example_data()

test/support/ast_data.ex

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
defmodule Gradient.AstData do
2+
require Gradient.Debug
3+
import Gradient.Debug, only: [elixir_to_ast: 1]
4+
import Gradient.TestHelpers
5+
alias Gradient.Types
6+
7+
@tokens __ENV__.file |> load_tokens()
8+
9+
defp pipe do
10+
{__ENV__.function,
11+
{__ENV__.line,
12+
elixir_to_ast do
13+
1
14+
|> is_atom()
15+
16+
'1'
17+
|> is_atom()
18+
19+
:ok
20+
|> is_atom()
21+
22+
[1, 2, 3]
23+
|> is_atom()
24+
25+
{1, 2, 3}
26+
|> is_atom()
27+
28+
"a"
29+
|> is_atom()
30+
end, __ENV__.line},
31+
{:block, 11,
32+
[
33+
{:call, 14, {:remote, 14, {:atom, 14, :erlang}, {:atom, 14, :is_atom}},
34+
[{:integer, 13, 1}]},
35+
{:call, 17, {:remote, 17, {:atom, 17, :erlang}, {:atom, 17, :is_atom}},
36+
[{:cons, 16, {:integer, 16, 49}, {nil, 16}}]},
37+
{:call, 20, {:remote, 20, {:atom, 20, :erlang}, {:atom, 20, :is_atom}},
38+
[{:atom, 19, :ok}]},
39+
{:call, 23, {:remote, 23, {:atom, 23, :erlang}, {:atom, 23, :is_atom}},
40+
[
41+
{:cons, 22, {:integer, 22, 1},
42+
{:cons, 22, {:integer, 22, 2}, {:cons, 22, {:integer, 22, 3}, {nil, 22}}}}
43+
]},
44+
{:call, 26, {:remote, 26, {:atom, 26, :erlang}, {:atom, 26, :is_atom}},
45+
[{:tuple, 25, [{:integer, 25, 1}, {:integer, 25, 2}, {:integer, 25, 3}]}]},
46+
{:call, 29, {:remote, 29, {:atom, 29, :erlang}, {:atom, 29, :is_atom}},
47+
[{:bin, 28, [{:bin_element, 28, {:string, 28, 'a'}, :default, :default}]}]}
48+
]}}
49+
end
50+
51+
@spec ast_data() :: [{Types.abstract_expr(), Types.tokens(), Types.options()}]
52+
def ast_data do
53+
[pipe()]
54+
|> Enum.map(fn {{name, _}, {start_line, ast, end_line}, expected} ->
55+
tokens = Gradient.Tokens.drop_tokens_to_line(@tokens, start_line)
56+
{name, {ast, tokens, [line: start_line, end_line: end_line]}, expected}
57+
end)
58+
end
59+
end

test/support/helpers.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ defmodule Gradient.TestHelpers do
2929
{tokens, ast}
3030
end
3131

32+
def load_tokens(path) do
33+
with {:ok, code} <- File.read(path),
34+
{:ok, tokens} <- :elixir.string_to_tokens(String.to_charlist(code), 1, 1, path, []) do
35+
tokens
36+
end
37+
end
38+
3239
@spec example_data() :: {T.tokens(), T.forms()}
3340
def example_data() do
3441
beam_path = Path.join(@examples_build_path, "Elixir.SimpleApp.beam") |> String.to_charlist()

0 commit comments

Comments
 (0)