summaryrefslogtreecommitdiff
path: root/lib/elixir
diff options
context:
space:
mode:
authorAndrea Leopardi <an.leopardi@gmail.com>2022-07-25 08:59:46 +0200
committerGitHub <noreply@github.com>2022-07-25 08:59:46 +0200
commiteead8deeabf9046eb288c35f4656bc9bd8e76836 (patch)
tree814692b70534d574c624770918f6a9bea143b4d6 /lib/elixir
parent3ae82c9914e524b9e8b29103dad6788f65d426a7 (diff)
downloadelixir-eead8deeabf9046eb288c35f4656bc9bd8e76836.tar.gz
Add Kernel.dbg/0 which debugs binding/0 (#12009)
Diffstat (limited to 'lib/elixir')
-rw-r--r--lib/elixir/lib/kernel.ex4
-rw-r--r--lib/elixir/test/elixir/kernel_test.exs11
2 files changed, 14 insertions, 1 deletions
diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex
index 7bf266818..57ba7655c 100644
--- a/lib/elixir/lib/kernel.ex
+++ b/lib/elixir/lib/kernel.ex
@@ -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,
@@ -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
diff --git a/lib/elixir/test/elixir/kernel_test.exs b/lib/elixir/test/elixir/kernel_test.exs
index df5780022..4f51c917f 100644
--- a/lib/elixir/test/elixir/kernel_test.exs
+++ b/lib/elixir/test/elixir/kernel_test.exs
@@ -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