Class: MotionSpec::Should

Inherits:
Object show all
Defined in:
lib/motion-spec/should.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Should

Returns a new instance of Should.



12
13
14
15
# File 'lib/motion-spec/should.rb', line 12

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



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

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(&:inspect).join(', ') << ') failed'

  satisfy(desc) do |object|
    object.__send__(name, *args, &block)
  end
end

Instance Method Details

#be(*args, &block) ⇒ Object Also known as: a, an



25
26
27
28
29
30
# File 'lib/motion-spec/should.rb', line 25

def be(*args, &block)
  return self if args.empty?

  block = args.shift unless block_given?
  satisfy(*args, &block)
end

#eq=(value) ⇒ Object

TODO: This was in the MacBacon specs and kept for backwards compatibilty but I’ve never seen this used before so possibly kill this.



80
81
82
# File 'lib/motion-spec/should.rb', line 80

def eq=(value)
  self === value
end

#equal(value) ⇒ Object Also known as: eq



64
65
66
# File 'lib/motion-spec/should.rb', line 64

def equal(value)
  self == value
end

#flunk(reason = 'Flunked') ⇒ Object

Raises:



84
85
86
# File 'lib/motion-spec/should.rb', line 84

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

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



73
74
75
# File 'lib/motion-spec/should.rb', line 73

def identical_to(value)
  self.equal? value
end

#match(value) ⇒ Object



69
70
71
# File 'lib/motion-spec/should.rb', line 69

def match(value)
  self =~ value
end

#not(*args, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/motion-spec/should.rb', line 17

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

  return self if args.empty?

  be(*args, &block)
end

#satisfy(*args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/motion-spec/should.rb', line 34

def satisfy(*args, &block)
  if args.size == 1 && String === args.first
    description = args.shift
  else
    description = ''
  end

  result = yield(@object, *args)

  if Counter[:depth] > 0
    Counter[:requirements] += 1
    flunk(description) unless @negated ^ result
    result
  else
    @negated ? !result : !!result
  end
end