/
methods.cr
307 lines (294 loc) · 9.37 KB
/
methods.cr
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
module Spec::Methods
# Defines an example group that describes a unit to be tested.
# Inside *&block* examples are defined by `#it` or `#pending`.
#
# Several `describe` blocks can be nested.
#
# Example:
# ```
# require "spec"
#
# describe "Int32" do
# describe "+" do
# it "adds" { (1 + 1).should eq 2 }
# end
# end
# ```
#
# If `focus` is `true`, only this `describe`, and others marked with `focus: true`, will run.
def describe(description = nil, file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &block)
Spec.root_context.describe(description.to_s, file, line, end_line, focus, tags, &block)
end
# Defines an example group that establishes a specific context,
# like *empty array* versus *array with elements*.
# Inside *&block* examples are defined by `#it` or `#pending`.
#
# It is functionally equivalent to `#describe`.
#
# If `focus` is `true`, only this `context`, and others marked with `focus: true`, will run.
def context(description = nil, file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &block)
describe(description.to_s, file, line, end_line, focus, tags, &block)
end
# Defines a concrete test case.
#
# The test is performed by the block supplied to *&block*.
#
# Example:
# ```
# require "spec"
#
# it "adds" { (1 + 1).should eq 2 }
# ```
#
# It is usually used inside a `#describe` or `#context` section.
#
# If `focus` is `true`, only this test, and others marked with `focus: true`, will run.
def it(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &block)
Spec.root_context.it(description.to_s, file, line, end_line, focus, tags, &block)
end
# Defines a pending test case.
#
# *&block* is never evaluated.
# It can be used to describe behaviour that is not yet implemented.
#
# Example:
# ```
# require "spec"
#
# pending "check cat" { cat.alive? }
# ```
#
# It is usually used inside a `#describe` or `#context` section.
#
# If `focus` is `true`, only this test, and others marked with `focus: true`, will run.
def pending(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &block)
pending(description, file, line, end_line, focus, tags)
end
# Defines a yet-to-be-implemented pending test case
#
# If `focus` is `true`, only this test, and others marked with `focus: true`, will run.
def pending(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil)
Spec.root_context.pending(description.to_s, file, line, end_line, focus, tags)
end
# Fails an example.
#
# This method can be used to manually fail an example defined in an `#it` block.
def fail(msg, file = __FILE__, line = __LINE__)
raise Spec::AssertionFailed.new(msg, file, line)
end
# Marks the current example pending
#
# In case an example needs to be pending on some condition that requires executing it,
# this allows to mark it as such rather than letting it fail or never run.
#
# ```
# require "spec"
#
# it "test git" do
# cmd = Process.find_executable("git")
# pending!("git is not available") unless cmd
# cmd.ends_with?("git").should be_true
# end
# ```
def pending!(msg = "Cannot run example", file = __FILE__, line = __LINE__)
raise Spec::ExamplePending.new(msg, file, line)
end
# Executes the given block before each spec in the current context runs.
#
# A context is defined by `describe` or `context` blocks, or outside of them
# it's the root context. Nested contexts inherit the `*_each` blocks of
# their ancestors.
#
# If multiple blocks are registered for the same spec, the blocks defined in
# the outermost context go first. Blocks on the same context are executed in
# order of definition.
#
# ```
# require "spec"
#
# it "sample_a" { }
#
# describe "nested_context" do
# before_each do
# puts "runs before sample_b"
# end
#
# it "sample_b" { }
# end
# ```
def before_each(&block)
if Spec.current_context.is_a?(RootContext)
raise "Can't call `before_each` outside of a describe/context"
end
Spec.current_context.before_each(&block)
end
# Executes the given block after each spec in the current context runs.
#
# A context is defined by `describe` or `context` blocks, or outside of them
# it's the root context. Nested contexts inherit the `*_each` blocks of
# their ancestors.
#
# If multiple blocks are registered for the same spec, the blocks defined in
# the outermost context go first. Blocks on the same context are executed in
# order of definition.
#
# ```
# require "spec"
#
# it "sample_a" { }
#
# describe "nested_context" do
# after_each do
# puts "runs after sample_b"
# end
#
# it "sample_b" { }
# end
# ```
def after_each(&block)
if Spec.current_context.is_a?(RootContext)
raise "Can't call `after_each` outside of a describe/context"
end
Spec.current_context.after_each(&block)
end
# Executes the given block before the first spec in the current context runs.
#
# A context is defined by `describe` or `context` blocks, or outside of them
# it's the root context.
# This is independent of the source location the specs and this hook are
# defined.
#
# If multiple blocks are registered on the same context, they are executed in
# order of definition.
#
# ```
# require "spec"
#
# it "sample_a" { }
#
# describe "nested_context" do
# before_all do
# puts "runs at start of nested_context"
# end
#
# it "sample_b" { }
# end
# ```
def before_all(&block)
if Spec.current_context.is_a?(RootContext)
raise "Can't call `before_all` outside of a describe/context"
end
Spec.current_context.before_all(&block)
end
# Executes the given block after the last spec in the current context runs.
#
# A context is defined by `describe` or `context` blocks, or outside of them
# it's the root context.
# This is independent of the source location the specs and this hook are
# defined.
#
# If multiple blocks are registered on the same context, they are executed in
# order of definition.
#
# ```
# require "spec"
#
# it "sample_a" { }
#
# describe "nested_context" do
# after_all do
# puts "runs at end of nested_context"
# end
#
# it "sample_b" { }
# end
# ```
def after_all(&block)
if Spec.current_context.is_a?(RootContext)
raise "Can't call `after_all` outside of a describe/context"
end
Spec.current_context.after_all(&block)
end
# Executes the given block when each spec in the current context runs.
#
# The block must call `run` on the given `Example::Procsy`
# object.
#
# This is essentially a `before_each` and `after_each` hook combined into one.
# It is useful for example when setup and teardown steps need shared state.
#
# A context is defined by `describe` or `context` blocks, or outside of them
# it's the root context. Nested contexts inherit the `*_each` blocks of
# their ancestors.
#
# If multiple blocks are registered for the same spec, the blocks defined in
# the outermost context go first. Blocks on the same context are executed in
# order of definition.
#
# ```
# require "spec"
#
# it "sample_a" { }
#
# describe "nested_context" do
# around_each do |example|
# puts "runs before sample_b"
# example.run
# puts "runs after sample_b"
# end
#
# it "sample_b" { }
# end
# ```
def around_each(&block : Example::Procsy ->)
if Spec.current_context.is_a?(RootContext)
raise "Can't call `around_each` outside of a describe/context"
end
Spec.current_context.around_each(&block)
end
# Executes the given block when the current context runs.
#
# The block must call `run` on the given `Context::Procsy`
# object.
#
# This is essentially a `before_all` and `after_all` hook combined into one.
# It is useful for example when setup and teardown steps need shared state.
#
# A context is defined by `describe` or `context` blocks. This hook does not
# work outside such a block (i.e. in the root context).
#
# If multiple blocks are registered for the same spec, the blocks defined in
# the outermost context go first. Blocks on the same context are executed in
# order of definition.
#
# ```
# require "spec"
#
# describe "main_context" do
# around_each do |example|
# puts "runs at beginning of main_context"
# example.run
# puts "runs at end of main_context"
# end
#
# it "sample_a" { }
#
# describe "nested_context" do
# around_each do |example|
# puts "runs at beginning of nested_context"
# example.run
# puts "runs at end of nested_context"
# end
#
# it "sample_b" { }
# end
# end
# ```
def around_all(&block : ExampleGroup::Procsy ->)
if Spec.current_context.is_a?(RootContext)
raise "Can't call `around_all` outside of a describe/context"
end
Spec.current_context.around_all(&block)
end
end
include Spec::Methods