Class: Should
Instance Method Summary
collapse
Constructor Details
#initialize(object) ⇒ Should
Returns a new instance of Should.
388
389
390
391
|
# File 'lib/mac_bacon.rb', line 388
def initialize(object)
@object = object
@negated = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
432
433
434
435
436
437
438
439
440
|
# File 'lib/mac_bacon.rb', line 432
def method_missing(name, *args, &block)
name = "#{name}?" if name.to_s =~ /\w[^?]\z/
desc = @negated ? "not " : ""
desc << @object.inspect << "." << name.to_s
desc << "(" << args.map{|x|x.inspect}.join(", ") << ") failed"
satisfy(desc) { |x| x.__send__(name, *args, &block) }
end
|
Instance Method Details
#be(*args, &block) ⇒ Object
Also known as:
a, an
403
404
405
406
407
408
409
410
|
# File 'lib/mac_bacon.rb', line 403
def be(*args, &block)
if args.empty?
self
else
block = args.shift unless block_given?
satisfy(*args, &block)
end
end
|
#equal(value) ⇒ Object
442
|
# File 'lib/mac_bacon.rb', line 442
def equal(value) self == value end
|
#flunk(reason = "Flunked") ⇒ Object
447
448
449
|
# File 'lib/mac_bacon.rb', line 447
def flunk(reason="Flunked")
raise Bacon::Error.new(:failed, reason)
end
|
#identical_to(value) ⇒ Object
Also known as:
same_as
444
|
# File 'lib/mac_bacon.rb', line 444
def identical_to(value) self.equal? value end
|
#match(value) ⇒ Object
443
|
# File 'lib/mac_bacon.rb', line 443
def match(value) self =~ value end
|
#not(*args, &block) ⇒ Object
393
394
395
396
397
398
399
400
401
|
# File 'lib/mac_bacon.rb', line 393
def not(*args, &block)
@negated = !@negated
if args.empty?
self
else
be(*args, &block)
end
end
|
#satisfy(*args, &block) ⇒ Object
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
# File 'lib/mac_bacon.rb', line 415
def satisfy(*args, &block)
if args.size == 1 && String === args.first
description = args.shift
else
description = ""
end
r = yield(@object, *args)
if Bacon::Counter[:depth] > 0
Bacon::Counter[:requirements] += 1
raise Bacon::Error.new(:failed, description) unless @negated ^ r
r
else
@negated ? !r : !!r
end
end
|