Skip to content

Commit

Permalink
Add Kernel.dbg/0 which debugs binding/0 (#12009)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Jul 25, 2022
1 parent 3ae82c9 commit eead8de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5806,6 +5806,8 @@ defmodule Kernel do
|> String.split() #=> ["Elixir", "is", "cool"]
|> List.first() #=> "Elixir"
With no arguments, `dbg()` debugs information about the current binding. See `binding/1`.
## Configuring the debug function
One of the benefits of `dbg/2` is that its debugging logic is configurable,
Expand Down Expand Up @@ -5846,7 +5848,7 @@ defmodule Kernel do
are inspected. They are the same options accepted by `inspect/2`.
"""
@doc since: "1.14.0"
defmacro dbg(code, options \\ []) do
defmacro dbg(code \\ quote(do: binding()), options \\ []) do
{mod, fun, args} = Application.compile_env!(__CALLER__, :elixir, :dbg_callback)
apply(mod, fun, [code, options, __CALLER__ | args])
end
Expand Down
11 changes: 11 additions & 0 deletions lib/elixir/test/elixir/kernel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,5 +1480,16 @@ defmodule KernelTest do
assert output =~ "[:foo, :foo, :foo]"
refute output =~ "\\e["
end

test "prints binding() if no arguments are given" do
my_var = 1
my_other_var = :foo

output = capture_io(fn -> dbg() end)

assert output =~ "binding()"
assert output =~ "my_var:"
assert output =~ "my_other_var:"
end
end
end

0 comments on commit eead8de

Please sign in to comment.