Class: Scampi::Should

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

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Should

Returns a new instance of Should.



7
8
9
10
# File 'lib/scampi/should.rb', line 7

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



47
48
49
50
51
52
53
54
55
# File 'lib/scampi/should.rb', line 47

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



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

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

#equal(value) ⇒ Object



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

def equal(value) = self == value

#flunk(reason = "Flunked") ⇒ Object

Raises:



63
64
65
# File 'lib/scampi/should.rb', line 63

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

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



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

def identical_to(value) = self.equal? value

#match(value) ⇒ Object



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

def match(value) = self =~ value

#not(*args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/scampi/should.rb', line 12

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

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

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



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

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