-
Notifications
You must be signed in to change notification settings - Fork 3.5k
First implementation of Kernel.dbg/2 #11974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6183ee9
668c339
02d3ac5
dce86a6
130ae1c
f782d93
5997f6b
b0d1c14
8367193
1f63efc
f6c9115
e84a90b
19b1597
9c0a256
4055569
09169b4
b5f3844
7963e4b
6943730
a9be474
80f1d89
9f6fd5b
ae517c8
6330524
4f478a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,6 +269,85 @@ defmodule MacroTest do | |
assert Macro.var(:foo, Other) == {:foo, [], Other} | ||
end | ||
|
||
describe "dbg/3" do | ||
defmacrop dbg_format(ast, options \\ quote(do: [syntax_colors: []])) do | ||
quote do | ||
ExUnit.CaptureIO.with_io(fn -> | ||
unquote(Macro.dbg(ast, options, __CALLER__)) | ||
end) | ||
end | ||
end | ||
Comment on lines
+273
to
+279
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opted to avoid the "no I/O" testing strategy for now in favor of simplicity. Testing with capturing I/O lets us keep the code in |
||
|
||
test "with a simple expression" do | ||
{result, formatted} = dbg_format(1 + 1) | ||
assert result == 2 | ||
assert formatted =~ "1 + 1 #=> 2" | ||
end | ||
|
||
test "with variables" do | ||
my_var = 1 + 1 | ||
{result, formatted} = dbg_format(my_var) | ||
assert result == 2 | ||
assert formatted =~ "my_var #=> 2" | ||
end | ||
|
||
test "with a function call" do | ||
{result, formatted} = dbg_format(Atom.to_string(:foo)) | ||
|
||
assert result == "foo" | ||
assert formatted =~ ~s[Atom.to_string(:foo) #=> "foo"] | ||
end | ||
|
||
test "with a multiline input" do | ||
{result, formatted} = | ||
dbg_format( | ||
case 1 + 1 do | ||
2 -> :two | ||
_other -> :math_is_broken | ||
end | ||
) | ||
|
||
assert result == :two | ||
|
||
assert formatted =~ """ | ||
case 1 + 1 do | ||
2 -> :two | ||
_other -> :math_is_broken | ||
end #=> :two | ||
""" | ||
end | ||
|
||
test "with a pipeline" do | ||
{result, formatted} = dbg_format([:a, :b, :c] |> tl() |> tl |> Kernel.hd()) | ||
assert result == :c | ||
|
||
assert formatted =~ "macro_test.exs" | ||
|
||
assert formatted =~ """ | ||
[:a, :b, :c] #=> [:a, :b, :c] | ||
|> tl() #=> [:b, :c] | ||
|> tl #=> [:c] | ||
|> Kernel.hd() #=> :c | ||
""" | ||
end | ||
|
||
test "with \"syntax_colors: []\" it doesn't print any color sequences" do | ||
fertapric marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{_result, formatted} = dbg_format("hello") | ||
refute formatted =~ "\e[" | ||
end | ||
|
||
test "with \"syntax_colors: [...]\" it forces color sequences" do | ||
{_result, formatted} = dbg_format("hello", syntax_colors: [string: :cyan]) | ||
assert formatted =~ IO.iodata_to_binary(IO.ANSI.format([:cyan, ~s("hello")])) | ||
end | ||
|
||
test "forwards options to the underlying inspect calls" do | ||
value = 'hello' | ||
assert {^value, formatted} = dbg_format(value, syntax_colors: [], charlists: :as_lists) | ||
assert formatted =~ "value #=> [104, 101, 108, 108, 111]\n" | ||
end | ||
end | ||
|
||
describe "to_string/1" do | ||
test "converts quoted to string" do | ||
assert Macro.to_string(quote do: hello(world)) == "hello(world)" | ||
|
Uh oh!
There was an error while loading. Please reload this page.