@@ -200,62 +200,92 @@ module Spec
200
200
end
201
201
end
202
202
203
+ # This module defines a number of methods to create expectations, which are
204
+ # automatically included into the top level namespace.
205
+ #
206
+ # Expectations are used by `Spec::ObjectExtensions#should` and `Spec::ObjectExtensions#should_not`.
203
207
module Expectations
208
+ # Creates an `Expectation` that passes if actual equals *value* (`==`).
204
209
def eq (value )
205
210
Spec ::EqualExpectation .new value
206
211
end
207
212
213
+ # Creates an `Expectation` that passes if actual and *value* are identical (`.same?`).
208
214
def be (value )
209
215
Spec ::BeExpectation .new value
210
216
end
211
217
218
+ # Creates an `Expectation` that passes if actual is true (`== true`).
212
219
def be_true
213
220
eq true
214
221
end
215
222
223
+ # Creates an `Expectation` that passes if actual is false (`== false`).
216
224
def be_false
217
225
eq false
218
226
end
219
227
228
+ # Creates an `Expectation` that passes if actual is truthy (neither `nil` nor `false`).
220
229
def be_truthy
221
230
Spec ::BeTruthyExpectation .new
222
231
end
223
232
233
+ # Creates an `Expectation` that passes if actual is falsy (`nil` or `false`).
224
234
def be_falsey
225
235
Spec ::BeFalseyExpectation .new
226
236
end
227
237
238
+ # Creates an `Expectation` that passes if actual is nil (`== nil`).
228
239
def be_nil
229
240
Spec ::BeNilExpectation .new
230
241
end
231
242
243
+ # Creates an `Expectation` that passes if actual is within *delta* of *expected*.
232
244
def be_close (expected, delta)
233
245
Spec ::CloseExpectation .new(expected, delta)
234
246
end
235
247
248
+ # Returns a factory to create a comparison `Expectation` that:
249
+ #
250
+ # * passes if actual is lesser than *value*: `be < value`
251
+ # * passes if actual is lesser than or equal *value*: `be <= value`
252
+ # * passes if actual is greater than *value*: `be > value`
253
+ # * passes if actual is greater than or equal *value*: `be >= value`
236
254
def be
237
255
Spec ::Be
238
256
end
239
257
258
+ # Creates an `Expectation` that passes if actual matches *value* (`=~`).
240
259
def match (value )
241
260
Spec ::MatchExpectation .new(value)
242
261
end
243
262
244
- # Passes if actual includes *expected*. Works on collections and `String`.
263
+ # Creates an `Expectation` that passes if actual includes *expected* (`.includes?`).
264
+ # Works on collections and `String`.
245
265
def contain (expected )
246
266
Spec ::ContainExpectation .new(expected)
247
267
end
248
268
269
+ # Creates an `Expectation` that passes if actual is of type *type* (`is_a?`).
249
270
macro be_a (type )
250
271
Spec ::BeAExpectation ({{type }}).new
251
272
end
252
273
274
+ # Runs the block and passes if it raises an exception of type *klass*.
275
+ #
276
+ # It returns the rescued exception.
253
277
macro expect_raises (klass )
254
278
expect_raises({{klass}}, nil ) do
255
279
{{yield }}
256
280
end
257
281
end
258
282
283
+ # Runs the block and passes if it raises an exception of type *klass* and the error message matches.
284
+ #
285
+ # If *message* is a string, it matches if the exception's error message contains that string.
286
+ # If *message* is a regular expression, it is used to match the error message.
287
+ #
288
+ # It returns the rescued exception.
259
289
macro expect_raises (klass , message , file = __FILE__ , line = __LINE__ )
260
290
% failed = false
261
291
begin
@@ -297,12 +327,18 @@ module Spec
297
327
end
298
328
299
329
module ObjectExtensions
330
+ # Validates an expectation and fails the example if it does not match.
331
+ #
332
+ # See `Spec::Expecations` for available expectations.
300
333
def should (expectation, file = __FILE__ , line = __LINE__ )
301
334
unless expectation.match self
302
335
fail(expectation.failure_message(self ), file, line)
303
336
end
304
337
end
305
338
339
+ # Validates an expectation and fails the example if it matches.
340
+ #
341
+ # See `Spec::Expecations` for available expectations.
306
342
def should_not (expectation, file = __FILE__ , line = __LINE__ )
307
343
if expectation.match self
308
344
fail(expectation.negative_failure_message(self ), file, line)
0 commit comments