Class: Scampi::Should

Inherits:
Object show all
Defined in:
lib/scampi/should.rb

Overview

The assertion wrapper returned by Object#should.

Supports chainable assertions like .should.be.empty, .should.not == 5, and .should.be.a(matcher). Undefines predicate and operator methods from Object so they can be intercepted via method_missing.

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Should

Wrap an object for assertion.



14
15
16
17
# File 'lib/scampi/should.rb', line 14

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

Catch predicate calls and auto-append ? to the method name. For example, .should.be.empty becomes .empty? on the object.



65
66
67
68
69
70
71
72
73
# File 'lib/scampi/should.rb', line 65

def method_missing(name, *args, &block)
  name = "#{name}?"  if name.to_s =~ /\w[^?]\z/

  desc = @negated ? "not ".dup : "".dup
  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

Identity chain or custom matcher. With no arguments, returns self for further chaining. With a block or lambda, delegates to satisfy.



34
35
36
37
38
39
40
41
42
43
# File 'lib/scampi/should.rb', line 34

def be(*args, &block)
  if args.empty?
    self
  else
    unless block_given?
      block = args.shift
    end
    satisfy(*args, &block)
  end
end

#equal(value) ⇒ Object

Assert equality using ==.



78
# File 'lib/scampi/should.rb', line 78

def equal(value) = self == value

#flunk(reason = "Flunked") ⇒ Object

Unconditionally fail the current spec.

Raises:



94
95
96
# File 'lib/scampi/should.rb', line 94

def flunk(reason="Flunked")
  raise Scampi::Error.new(:failed, reason)
end

#identical_to(value) ⇒ Object Also known as: same_as

Assert object identity (same object in memory).



88
# File 'lib/scampi/should.rb', line 88

def identical_to(value) = self.equal? value

#match(value) ⇒ Object

Assert the object matches a pattern using =~.



83
# File 'lib/scampi/should.rb', line 83

def match(value) = self =~ value

#not(*args, &block) ⇒ Object

Toggle negation. Can be chained: .should.not.be.empty.



22
23
24
25
26
27
28
29
30
# File 'lib/scampi/should.rb', line 22

def not(*args, &block)
  @negated = !@negated

  if args.empty?
    self
  else
    be(*args, &block)
  end
end

#satisfy(description = "", &block) ⇒ Object

Assert that the block returns truthy for the wrapped object.



52
53
54
55
56
57
58
59
60
61
# File 'lib/scampi/should.rb', line 52

def satisfy(description="", &block)
  r = yield(@object)
  if Scampi::Counter[:depth] > 0
    Scampi::Counter[:requirements] += 1
    raise Scampi::Error.new(:failed, description)  unless @negated ^ r
    r
  else
    @negated ? !r : !!r
  end
end