Skip to content

Instantly share code, notes, and snippets.

@antifuchs
Last active December 16, 2015 11:29
Show Gist options
  • Save antifuchs/5427882 to your computer and use it in GitHub Desktop.
Save antifuchs/5427882 to your computer and use it in GitHub Desktop.
Wonder if that'll work...
@doc """
Provides a convenient macro that allows a test to be
defined with a string. This macro automatically inserts
the atom :ok as the last line of the test. That said,
a passing test always returns :ok, but, more important,
it forces Elixir to not tail call optimize the test and
therefore avoiding hiding lines from the backtrace.
## Examples
test "true is equal to true" do
assert true == true
end
"""
defmacro test(message, contents) do
contents =
case contents do
[do: block] ->
quote do
unquote(contents)
:ok
end
_ ->
quote do
try(unquote(contents))
:ok
end
end
quote do
message = unquote(message)
message = if is_binary(message) do
:"test #{message}"
else
:"test_#{message}"
end
__TESTS = __TESTS ++ [ unquote(message): fn -> unquote(Macro.escape contents)]
def message, [], [], do: __TESTS[unquote(message)].()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment