Last active
December 16, 2015 11:29
-
-
Save antifuchs/5427882 to your computer and use it in GitHub Desktop.
Wonder if that'll work...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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