-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcallbacks.ex
More file actions
117 lines (103 loc) · 2.7 KB
/
callbacks.ex
File metadata and controls
117 lines (103 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
defmodule ElixirScript.Test.Callbacks do
@moduledoc """
Defines ElixirScript.Test callbacks
"""
@doc """
Called before all tests are run in a test file
"""
defmacro setup_all(context \\ quote(do: _), contents) do
do_setup(context, contents, :__elixirscript_test_setup_all)
end
@doc """
Called before each test is run in a test file
"""
defmacro setup(context \\ quote(do: _), contents) do
do_setup(context, contents, :__elixirscript_test_setup)
end
defp do_setup(context, contents, name) do
contents =
case contents do
[do: block] ->
quote do
unquote(block)
end
_ ->
quote do
try(unquote(contents))
end
end
context = Macro.escape(context)
contents = Macro.escape(contents, unquote: true)
quote bind_quoted: [context: context, contents: contents, name: name] do
def unquote(name)(unquote(context)) do
unquote(contents)
end
end
end
@doc """
Called after all tests are run in a test file
"""
defmacro teardown_all(context \\ quote(do: _), contents) do
do_teardown(context, contents, :__elixirscript_test_teardown_all)
end
@doc """
Called after each test is run in a test file
"""
defmacro teardown(context \\ quote(do: _), contents) do
do_teardown(context, contents, :__elixirscript_test_teardown)
end
defp do_teardown(context, contents, name) do
contents =
case contents do
[do: block] ->
quote do
unquote(block)
:ok
end
_ ->
quote do
try(unquote(contents))
:ok
end
end
context = Macro.escape(context)
contents = Macro.escape(contents, unquote: true)
quote bind_quoted: [context: context, contents: contents, name: name] do
def unquote(name)(unquote(context)) do
unquote(contents)
end
end
end
@doc """
Defines a test
"""
defmacro test(message, context \\ quote(do: _), contents) do
contents =
case contents do
[do: block] ->
quote do
unquote(block)
:ok
end
_ ->
quote do
try(unquote(contents))
:ok
end
end
context = Macro.escape(context)
contents = Macro.escape(contents, unquote: true)
name = message
|> String.replace(" ", "_")
|> String.replace(~r/[^A-Za-z0-9]/, "")
name = String.to_atom("__elixirscript_test_case_#{name}")
quote bind_quoted: [context: context, contents: contents, message: message, name: name] do
def unquote(name)() do
%{
message: unquote(message),
test: fn(context) -> unquote(contents) end
}
end
end
end
end