Skip to content

Commit

Permalink
Better tests for runtime if:
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed Dec 20, 2023
1 parent 64d629c commit 2a1e09c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/telemetria/module_hooks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Telemetria.Hooks do
args: [ast_tuple()],
guards: [ast_tuple()],
body: [{:do, ast_tuple()}],
conditional: (any() -> boolean()),
conditional: (any() -> boolean()) | nil,
options: [option()]
}
defstruct ~w|annotation_type env kind fun arity args guards body conditional options|a
Expand Down
2 changes: 1 addition & 1 deletion test/support/telemetria_tester.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Test.Telemetria.S do
@moduledoc false
defstruct(foo: 42, bar: :baz)

def allow?(arg), do: arg == :forty_two
def allow?(arg), do: arg == :persistent_term.get(__MODULE__, :forty_two)
end

defmodule Test.Telemetria.Example do
Expand Down
44 changes: 44 additions & 0 deletions test/telemetria_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,50 @@ defmodule Telemetria.Test do
assert log =~ ~s|call: [args: [i: "42"], result: :forty_two]|
end

test "@telemetry if: runtime (true)" do
:persistent_term.put(Test.Telemetria.S, :forty_two)

log =
capture_log(fn ->
assert 0 == Example.annotated_1(nil)
assert :forty_two == Example.annotated_1("42")
assert 42 == Example.annotated_1(42)
Process.sleep(100)
end)

assert log =~ "event: [:test, :telemetria, :example, :annotated_1]"
assert log =~ "event: [:test, :telemetria, :example, :annotated_2]"
refute log =~ "[debug]"
assert log =~ "[info]"
assert log =~ ~s|call: [args: [foo: 42], result: 42]|
assert log =~ "[warning]"
assert log =~ ~s|call: [args: "[i: 42]", result: "42"]|
assert log =~ "[error]"
assert log =~ ~s|call: [args: [i: "42"], result: :forty_two]|
end

test "@telemetry if: runtime (false)" do
:persistent_term.put(Test.Telemetria.S, :not_forty_two)

log =
capture_log(fn ->
assert 0 == Example.annotated_1(nil)
assert :forty_two == Example.annotated_1("42")
assert 42 == Example.annotated_1(42)
Process.sleep(100)
end)

assert log =~ "event: [:test, :telemetria, :example, :annotated_1]"
assert log =~ "event: [:test, :telemetria, :example, :annotated_2]"
refute log =~ "[debug]"
assert log =~ "[info]"
assert log =~ ~s|call: [args: [foo: 42], result: 42]|
assert log =~ "[warning]"
assert log =~ ~s|call: [args: "[i: 42]", result: "42"]|
refute log =~ "[error]"
refute log =~ ~s|call: [args: [i: "42"], result: :forty_two]|
end

test "@telemetry deep pattern match" do
log =
capture_log(fn ->
Expand Down

0 comments on commit 2a1e09c

Please sign in to comment.