Skip to content

Commit 05c81d2

Browse files
committed
Apply review - Use sigils, fix doc, update one test
1 parent 9485320 commit 05c81d2

File tree

3 files changed

+39
-83
lines changed

3 files changed

+39
-83
lines changed

lib/gradient/debug.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Gradient.Debug do
88
@type erlang_form() :: Gradient.Types.form()
99

1010
@doc ~S"""
11-
Translate Elixir code to abstract code.
11+
Translate the Elixir code to the Erlang AST.
1212
"""
1313
defmacro elixir_to_ast(do: code) do
1414
quoted_to_ast(code)
@@ -19,7 +19,7 @@ defmodule Gradient.Debug do
1919
end
2020

2121
@doc ~S"""
22-
Translate Elixir quoted to abstract code.
22+
Translate the Elixir AST to the Erlang AST.
2323
"""
2424
@spec quoted_to_ast(elixir_form()) :: erlang_form()
2525
def quoted_to_ast(qt) do

test/gradient/elixir_expr_test.exs

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ defmodule Gradient.ElixirExprTest do
9393
end
9494
|> ElixirExpr.pp_expr()
9595

96-
assert "receive do {:hello, msg} -> msg after 1000 -> \"nothing happened\" end" == actual
96+
assert ~s(receive do {:hello, msg} -> msg after 1000 -> "nothing happened" end) == actual
9797
end
9898

9999
test "call pipe" do
@@ -140,7 +140,7 @@ defmodule Gradient.ElixirExprTest do
140140
end
141141
|> ElixirExpr.pp_expr()
142142

143-
assert "try do raise \"ok\"; catch :error, e -> IO.puts(Exception.format(:error, e, __STACKTRACE__)); reraise e, __STACKTRACE__ end" ==
143+
assert ~s(try do raise "ok"; catch :error, e -> IO.puts(Exception.format(:error, e, __STACKTRACE__\)\); reraise e, __STACKTRACE__ end) ==
144144
actual
145145
end
146146

@@ -155,7 +155,7 @@ defmodule Gradient.ElixirExprTest do
155155
end
156156
|> ElixirExpr.pp_expr()
157157

158-
assert "try do raise \"oops\"; catch :error, %RuntimeError{} = _ -> \"Error!\" end" ==
158+
assert ~s(try do raise "oops"; catch :error, %RuntimeError{} = _ -> "Error!" end) ==
159159
actual
160160
end
161161

@@ -215,7 +215,7 @@ defmodule Gradient.ElixirExprTest do
215215
end
216216
|> ElixirExpr.pp_expr()
217217

218-
assert "try do throw \"good\"; :ok; else v when v == :ok -> :ok; v -> :nok; catch :error, %RuntimeError{} = e -> 11; e; :throw, val -> val; :throw, _ -> 0; after IO.puts(\"Cleaning!\") end" ==
218+
assert ~s(try do throw "good"; :ok; else v when v == :ok -> :ok; v -> :nok; catch :error, %RuntimeError{} = e -> 11; e; :throw, val -> val; :throw, _ -> 0; after IO.puts("Cleaning!"\) end) ==
219219
actual
220220
end
221221

@@ -301,73 +301,29 @@ defmodule Gradient.ElixirExprTest do
301301
end
302302

303303
test "try with rescue and catch" do
304-
try_expr =
305-
{:try, 3,
306-
[
307-
{:case, 4, {:atom, 4, true},
308-
[
309-
{:clause, [generated: true, location: 4], [{:atom, 0, false}], [],
310-
[
311-
{:call, 7, {:remote, 7, {:atom, 7, :erlang}, {:atom, 7, :error}},
312-
[
313-
{:call, 7, {:remote, 7, {:atom, 7, RuntimeError}, {:atom, 7, :exception}},
314-
[
315-
{:bin, 7,
316-
[
317-
{:bin_element, 7, {:string, 7, 'oops'}, :default, :default}
318-
]}
319-
]}
320-
]}
321-
]},
322-
{:clause, [generated: true, location: 4], [{:atom, 0, true}], [],
323-
[
324-
{:call, 5, {:remote, 5, {:atom, 5, :erlang}, {:atom, 5, :throw}},
325-
[
326-
{:bin, 5, [{:bin_element, 5, {:string, 5, 'good'}, :default, :default}]}
327-
]}
328-
]}
329-
]}
330-
], [],
331-
[
332-
{:clause, 10,
333-
[
334-
{:tuple, 10,
335-
[
336-
{:atom, 10, :error},
337-
{:var, 10, :_@1},
338-
{:var, 10, :___STACKTRACE__@1}
339-
]}
340-
],
341-
[
342-
[
343-
{:op, 10, :andalso,
344-
{:op, 10, :==,
345-
{:call, 10, {:remote, 10, {:atom, 10, :erlang}, {:atom, 10, :map_get}},
346-
[{:atom, 10, :__struct__}, {:var, 10, :_@1}]}, {:atom, 10, RuntimeError}},
347-
{:call, 10, {:remote, 10, {:atom, 10, :erlang}, {:atom, 10, :map_get}},
348-
[{:atom, 10, :__exception__}, {:var, 10, :_@1}]}}
349-
]
350-
],
351-
[
352-
{:match, 10, {:var, 10, :_e@1}, {:var, 10, :_@1}},
353-
{:integer, 11, 11},
354-
{:var, 12, :_e@1}
355-
]},
356-
{:clause, 14,
357-
[
358-
{:tuple, 14,
359-
[
360-
{:atom, 14, :throw},
361-
{:var, 14, :_val@1},
362-
{:var, 14, :___STACKTRACE__@1}
363-
]}
364-
], [], [{:integer, 15, 12}, {:var, 16, :_val@1}]}
365-
], []}
366-
367-
result = ElixirExpr.pp_expr(try_expr)
368-
369-
assert "try do if true do throw \"good\" else raise \"oops\" end;" <>
370-
" catch :error, %RuntimeError{} = e -> 11; e; :throw, val -> 12; val end" == result
304+
actual =
305+
elixir_to_ast do
306+
try do
307+
if true do
308+
throw("good")
309+
else
310+
raise "oops"
311+
end
312+
rescue
313+
e in RuntimeError ->
314+
11
315+
e
316+
catch
317+
val ->
318+
12
319+
val
320+
end
321+
end
322+
|> ElixirExpr.pp_expr()
323+
324+
assert ~s(try do if true do throw "good" else raise "oops" end;) <>
325+
~s( catch :error, %RuntimeError{} = e -> 11; e; :throw, val -> 12; val end) ==
326+
actual
371327
end
372328
end
373329
end

test/support/expr_data.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ defmodule Gradient.ExprData do
2828
{"char", {:char, 0, ?c}, "?c"},
2929
{"float", {:float, 0, 12.0}, "12.0"},
3030
{"integer", {:integer, 0, 1}, "1"},
31-
{"erlang string", {:string, 0, 'ala ma kota'}, "\'ala ma kota\'"}
31+
{"erlang string", {:string, 0, 'ala ma kota'}, ~s('ala ma kota')}
3232
]
3333
end
3434

3535
def list_test_data() do
3636
[
3737
{"charlist",
3838
{:cons, 0, {:integer, 0, 97},
39-
{:cons, 0, {:integer, 0, 108}, {:cons, 0, {:integer, 0, 97}, {nil, 0}}}}, "\'ala\'"},
39+
{:cons, 0, {:integer, 0, 108}, {:cons, 0, {:integer, 0, 97}, {nil, 0}}}}, ~s('ala')},
4040
{"int list",
4141
{:cons, 0, {:integer, 0, 0},
4242
{:cons, 0, {:integer, 0, 1}, {:cons, 0, {:integer, 0, 2}, {nil, 0}}}}, "[0, 1, 2]"},
@@ -72,11 +72,11 @@ defmodule Gradient.ExprData do
7272
def exception_test_data() do
7373
[
7474
{"throw", elixir_to_ast(throw({:ok, 12})), "throw {:ok, 12}"},
75-
{"raise/1", elixir_to_ast(raise "test error"), "raise \"test error\""},
75+
{"raise/1", elixir_to_ast(raise "test error"), ~s(raise "test error")},
7676
{"raise/1 without msg", elixir_to_ast(raise RuntimeError), "raise RuntimeError"},
77-
{"raise/2", elixir_to_ast(raise RuntimeError, "test error"), "raise \"test error\""},
77+
{"raise/2", elixir_to_ast(raise RuntimeError, "test error"), ~s(raise "test error")},
7878
{"custom raise", elixir_to_ast(raise ArithmeticError, "only odd numbers"),
79-
"raise ArithmeticError, \"only odd numbers\""}
79+
~s(raise ArithmeticError, "only odd numbers")}
8080
]
8181
end
8282

@@ -94,11 +94,11 @@ defmodule Gradient.ExprData do
9494

9595
def map_test_data do
9696
[
97-
{"string map", elixir_to_ast(%{"a" => 12}), "%{\"a\" => 12}"},
97+
{"string map", elixir_to_ast(%{"a" => 12}), ~s(%{"a" => 12})},
9898
{"map pm", elixir_to_ast(%{a: a} = %{a: 12}), "%{a: a} = %{a: 12}"},
9999
{"update map", elixir_to_ast(%{%{} | a: 1}), "%{%{} | a: 1}"},
100100
{"struct expr", elixir_to_ast(%{__struct__: TestStruct, name: "John"}),
101-
"%TestStruct{name: \"John\"}"}
101+
~s(%TestStruct{name: "John"})}
102102
]
103103
end
104104

@@ -158,7 +158,7 @@ defmodule Gradient.ExprData do
158158
"a" <> x
159159
end
160160

161-
{"binary <> joining", ast, "x = \"b\"; <<\"a\", x::binary>>"}
161+
{"binary <> joining", ast, ~s(x = "b"; <<"a", x::binary>>)}
162162
end
163163

164164
defp bin_with_bin_var do
@@ -168,7 +168,7 @@ defmodule Gradient.ExprData do
168168
<<"a", "b", x::binary>>
169169
end
170170

171-
{"binary with bin var", ast, "x = \"b\"; <<\"a\", \"b\", x::binary>>"}
171+
{"binary with bin var", ast, ~s(x = "b"; <<"a", "b", x::binary>>)}
172172
end
173173

174174
defp bin_with_pp_int_size do
@@ -177,7 +177,7 @@ defmodule Gradient.ExprData do
177177
<<a::16>> = <<"abcd">>
178178
end
179179

180-
{"binary with int size", ast, "<<a::16>> = \"abcd\""}
180+
{"binary with int size", ast, ~s(<<a::16>> = "abcd")}
181181
end
182182

183183
defp bin_with_pp_and_bitstring_size do

0 commit comments

Comments
 (0)