Class: SpecUnit::ShouldHelper

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
Defined in:
lib/spec-unit/should_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, sym, *args, &block) ⇒ ShouldHelper

Returns a new instance of ShouldHelper.



7
8
9
10
11
12
13
# File 'lib/spec-unit/should_helper.rb', line 7

def initialize(object, sym, *args, &block)
  $spec_unit_test.send(:add_assertion)
  @object = object
  @not = !!(sym.to_s =~ /^should_not/)
  sym = sym.to_s.sub(/_not/, '').to_sym if @not
  self.send(sym, *args, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/spec-unit/should_helper.rb', line 151

def method_missing(sym, *args)
  if sym.to_s =~ /^should_be_/ || sym.to_s =~ /^should_have_/ || sym.to_s =~ /^should_/
    should_with_predicate($', *args)
  else
    self
  end 
end

Instance Method Details

#not_wrapperObject



15
16
17
# File 'lib/spec-unit/should_helper.rb', line 15

def not_wrapper
  @not ? !yield : yield
end

#should_be(another_object = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spec-unit/should_helper.rb', line 34

def should_be(another_object = nil)
  if another_object.nil?
    should_with_method
  else
    if @not
      assert_not_same another_object, @object
    else
      assert_same another_object, @object
    end
  end
end

#should_be_a_kind_of(klass) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/spec-unit/should_helper.rb', line 46

def should_be_a_kind_of(klass)
  if @not
    assert_block { !@object.kind_of?(klass) }
  else
    assert_kind_of klass, @object
  end
end

#should_be_an_instance_of(klass) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/spec-unit/should_helper.rb', line 54

def should_be_an_instance_of(klass)
  if @not
    assert_block { !@object.instance_of?(klass) }
  else
    assert_instance_of klass, @object
  end
end

#should_be_close(another_object, delta) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/spec-unit/should_helper.rb', line 62

def should_be_close(another_object, delta)
  if @not
    assert_block { (another_object - delta > @object) || (another_object + delta < @object) }
  else
    assert_in_delta another_object, @object, delta
  end
end

#should_equal(another_object) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/spec-unit/should_helper.rb', line 70

def should_equal(another_object)
  if @not
    assert_not_equal another_object, @object
  else
    assert_equal another_object, @object
  end
end

#should_have(count) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/spec-unit/should_helper.rb', line 78

def should_have(count)
  if @not
    assert_not_equal count, @object.size
  else
    assert_equal count, @object.size
  end
end

#should_have_at_least(count) ⇒ Object



86
87
88
# File 'lib/spec-unit/should_helper.rb', line 86

def should_have_at_least(count)
  assert( not_wrapper { @object.size >= count } )
end

#should_have_at_most(count) ⇒ Object



90
91
92
# File 'lib/spec-unit/should_helper.rb', line 90

def should_have_at_most(count)
  assert( not_wrapper { @object.size <= count } )
end

#should_include(another_object) ⇒ Object



94
95
96
# File 'lib/spec-unit/should_helper.rb', line 94

def should_include(another_object)
  assert( not_wrapper { @object.include?(another_object) } )
end

#should_raise(error = Exception) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/spec-unit/should_helper.rb', line 98

def should_raise(error=Exception)
  assert_block do
    not_wrapper do
      begin
        @object.call && false 
      rescue error
        true
      rescue Exception
        false
      end
    end
  end
end

#should_respond_to(method) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/spec-unit/should_helper.rb', line 112

def should_respond_to(method)
  if @not
    assert_block { !@object.respond_to?(method) }
  else
    assert_respond_to @object, method
  end
end

#should_satisfy(&block) ⇒ Object



120
121
122
# File 'lib/spec-unit/should_helper.rb', line 120

def should_satisfy(&block)
  assert( not_wrapper { block.call(@object) } )
end

#should_throw(symbol = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/spec-unit/should_helper.rb', line 124

def should_throw(symbol=nil)
  if @not
    assert_block do
      begin
        caught = true
        if symbol
          catch(symbol) do
            @object.call
            caught = false
          end
        else
          @object.call
          caught = false
        end
      rescue NameError, ThreadError => error
        if UncaughtThrow[error.class] !~ error.message
          raise error
        end
        caught = symbol.nil? ? true : false
      end
      !caught
    end
  else
    assert_throws(symbol, &@object)
  end
end

#should_with_methodObject



23
24
25
# File 'lib/spec-unit/should_helper.rb', line 23

def should_with_method
  self
end

#should_with_predicate(meth, *args) ⇒ Object



19
20
21
# File 'lib/spec-unit/should_helper.rb', line 19

def should_with_predicate(meth, *args)
  assert( not_wrapper { @object.send(meth.to_sym, *args) })
end