Module: Mini::Assertions

Included in:
Test::TestCase
Defined in:
lib/mini/test.rb

Instance Method Summary collapse

Instance Method Details

#_assertionsObject



47
48
49
# File 'lib/mini/test.rb', line 47

def _assertions
  @_assertions ||= 0
end

#_assertions=(n) ⇒ Object



43
44
45
# File 'lib/mini/test.rb', line 43

def _assertions= n
  @_assertions = n
end

#assert(test, msg = nil) ⇒ Object

Raises:



51
52
53
54
55
56
# File 'lib/mini/test.rb', line 51

def assert test, msg = nil
  msg ||= "Failed assertion, no message given."
  self._assertions += 1
  raise Mini::Assertion, msg unless test
  true
end

#assert_block(msg = nil) ⇒ Object



58
59
60
61
# File 'lib/mini/test.rb', line 58

def assert_block msg = nil
  msg = message msg, "Expected block to return true value"
  assert yield, msg
end

#assert_empty(obj, msg = nil) ⇒ Object



63
64
65
66
67
# File 'lib/mini/test.rb', line 63

def assert_empty obj, msg = nil
  msg = message msg, "Expected #{obj.inspect} to be empty"
  assert_respond_to obj, :empty?
  assert obj.empty?, msg
end

#assert_equal(exp, act, msg = nil) ⇒ Object



69
70
71
72
# File 'lib/mini/test.rb', line 69

def assert_equal exp, act, msg = nil
  msg = message msg, "Expected #{mu_pp(exp)}, not #{mu_pp(act)}"
  assert(exp == act, msg)
end

#assert_in_delta(exp, act, delta = 0.001, msg = nil) ⇒ Object



74
75
76
77
78
# File 'lib/mini/test.rb', line 74

def assert_in_delta exp, act, delta = 0.001, msg = nil
  n = (exp - act).abs
  msg = message msg, "Expected #{exp} - #{act} (#{n}) to be < #{delta}"
  assert delta > n, msg
end

#assert_in_epsilon(a, b, epsilon = 0.001, msg = nil) ⇒ Object



80
81
82
# File 'lib/mini/test.rb', line 80

def assert_in_epsilon a, b, epsilon = 0.001, msg = nil
  assert_in_delta a, b, [a, b].min * epsilon, msg
end

#assert_includes(collection, obj, msg = nil) ⇒ Object



84
85
86
87
88
# File 'lib/mini/test.rb', line 84

def assert_includes collection, obj, msg = nil
  msg = message msg, "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}"
  assert_respond_to collection, :include?
  assert collection.include?(obj), msg
end

#assert_instance_of(cls, obj, msg = nil) ⇒ Object



90
91
92
93
94
95
# File 'lib/mini/test.rb', line 90

def assert_instance_of cls, obj, msg = nil
  msg = message msg, "Expected #{mu_pp(obj)} to be an instance of #{cls}"
  flip = (Module === obj) && ! (Module === cls) # HACK for specs
  obj, cls = cls, obj if flip
  assert cls === obj, msg
end

#assert_kind_of(cls, obj, msg = nil) ⇒ Object

TODO: merge with instance_of



97
98
99
100
101
102
# File 'lib/mini/test.rb', line 97

def assert_kind_of cls, obj, msg = nil # TODO: merge with instance_of
  msg = message msg, "Expected #{mu_pp(obj)} to be a kind of #{cls}"
  flip = (Module === obj) && ! (Module === cls) # HACK for specs
  obj, cls = cls, obj if flip
  assert obj.kind_of?(cls), msg
end

#assert_match(exp, act, msg = nil) ⇒ Object



104
105
106
107
# File 'lib/mini/test.rb', line 104

def assert_match exp, act, msg = nil
  msg = message msg, "Expected #{mu_pp(act)} to match #{mu_pp(exp)}"
  assert act =~ exp, msg
end

#assert_nil(obj, msg = nil) ⇒ Object



109
110
111
112
# File 'lib/mini/test.rb', line 109

def assert_nil obj, msg = nil
  msg = message msg, "Expected #{mu_pp(obj)} to be nil"
  assert obj.nil?, msg
end

#assert_operator(o1, op, o2, msg = nil) ⇒ Object



114
115
116
117
# File 'lib/mini/test.rb', line 114

def assert_operator o1, op, o2, msg = nil
  msg = message msg, "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}"
  assert o1.__send__(op, o2), msg
end

#assert_raises(*exp) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mini/test.rb', line 119

def assert_raises *exp
  msg = String === exp.last ? exp.pop : nil
  should_raise = false
  begin
    yield
    should_raise = true
  rescue Exception => e
    assert_includes exp, e.class
    exception_details(e, "<#{mu_pp(exp)}> exception expected, not")
    return e
  end

  exp = exp.first if exp.size == 1
  flunk "#{mu_pp(exp)} expected but nothing was raised." if should_raise
end

#assert_respond_to(obj, meth, msg = nil) ⇒ Object



135
136
137
138
139
140
# File 'lib/mini/test.rb', line 135

def assert_respond_to obj, meth, msg = nil
  msg = message msg, "Expected #{mu_pp(obj)} to respond to #{meth}"
  flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs
  obj, meth = meth, obj if flip
  assert obj.respond_to?(meth), msg
end

#assert_same(exp, act, msg = nil) ⇒ Object



142
143
144
145
# File 'lib/mini/test.rb', line 142

def assert_same exp, act, msg = nil
  msg = message msg, "Expected #{mu_pp(act)} to be the same as #{mu_pp(exp)}"
  assert exp.equal?(act), msg
end

#assert_throws(sym, msg = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mini/test.rb', line 147

def assert_throws sym, msg = nil
  default = "Expected #{mu_pp(sym)} to have been thrown"
  caught = true
  catch(sym) do
    begin
      yield
    rescue ArgumentError => e     # 1.9 exception
      default += ", not #{e.message.split(/ /).last}"
    rescue NameError => e         # 1.8 exception
      default += ", not #{e.name.inspect}"
    end
    caught = false
  end

  assert caught, message(msg, default)
end

#capture_ioObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/mini/test.rb', line 164

def capture_io
  require 'stringio'

  orig_stdout, orig_stderr         = $stdout.dup, $stderr.dup
  captured_stdout, captured_stderr = StringIO.new, StringIO.new
  $stdout, $stderr                 = captured_stdout, captured_stderr

  yield

  return captured_stdout.string, captured_stderr.string
ensure
  $stdout = orig_stdout
  $stderr = orig_stderr
end

#exception_details(e, msg) ⇒ Object



179
180
181
# File 'lib/mini/test.rb', line 179

def exception_details e, msg
  "#{msg}\nClass: <#{e.class}>\nMessage: <#{e.message.inspect}>\n---Backtrace---\n#{Mini::filter_backtrace(e.backtrace).join("\n")}\n---------------"
end

#fail(msg = nil) ⇒ Object Also known as: flunk



183
184
185
186
# File 'lib/mini/test.rb', line 183

def fail msg = nil
  msg ||= "Epic Fail!"
  assert false, msg
end

#message(msg, default) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/mini/test.rb', line 190

def message msg, default
  if msg then
    msg += '.' unless msg.empty?
    msg += "\n#{default}."
    msg.strip
  else
    "#{default}."
  end
end

#mu_pp(obj) ⇒ Object



39
40
41
# File 'lib/mini/test.rb', line 39

def mu_pp(obj)
  obj.inspect
end

#pass(msg = nil) ⇒ Object

used for counting assertions



201
202
203
# File 'lib/mini/test.rb', line 201

def pass msg = nil
  assert true
end

#refute(test, msg = nil) ⇒ Object



205
206
207
208
# File 'lib/mini/test.rb', line 205

def refute test, msg = nil
  msg ||= "Failed refutation, no message given"
  not assert(! test, msg)
end

#refute_empty(obj, msg = nil) ⇒ Object



210
211
212
213
214
# File 'lib/mini/test.rb', line 210

def refute_empty obj, msg = nil
  msg = message msg, "Expected #{obj.inspect} to not be empty"
  assert_respond_to obj, :empty?
  refute obj.empty?, msg
end

#refute_equal(exp, act, msg = nil) ⇒ Object



216
217
218
219
# File 'lib/mini/test.rb', line 216

def refute_equal exp, act, msg = nil
  msg = message msg, "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}"
  refute exp == act, msg
end

#refute_in_delta(exp, act, delta = 0.001, msg = nil) ⇒ Object



221
222
223
224
225
# File 'lib/mini/test.rb', line 221

def refute_in_delta exp, act, delta = 0.001, msg = nil
  n = (exp - act).abs
  msg = message msg, "Expected #{exp} - #{act} (#{n}) to not be < #{delta}"
  refute delta > n, msg
end

#refute_in_epsilon(a, b, epsilon = 0.001, msg = nil) ⇒ Object



227
228
229
# File 'lib/mini/test.rb', line 227

def refute_in_epsilon a, b, epsilon = 0.001, msg = nil
  refute_in_delta a, b, a * epsilon, msg
end

#refute_includes(collection, obj, msg = nil) ⇒ Object



231
232
233
234
235
# File 'lib/mini/test.rb', line 231

def refute_includes collection, obj, msg = nil
  msg = message msg, "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}"
  assert_respond_to collection, :include?
  refute collection.include?(obj), msg
end

#refute_instance_of(cls, obj, msg = nil) ⇒ Object



237
238
239
240
241
242
# File 'lib/mini/test.rb', line 237

def refute_instance_of cls, obj, msg = nil
  msg = message msg, "Expected #{mu_pp(obj)} to not be an instance of #{cls}"
  flip = (Module === obj) && ! (Module === cls) # HACK for specs
  obj, cls = cls, obj if flip
  refute cls === obj, msg
end

#refute_kind_of(cls, obj, msg = nil) ⇒ Object

TODO: merge with instance_of



244
245
246
247
248
249
# File 'lib/mini/test.rb', line 244

def refute_kind_of cls, obj, msg = nil # TODO: merge with instance_of
  msg = message msg, "Expected #{mu_pp(obj)} to not be a kind of #{cls}"
  flip = (Module === obj) && ! (Module === cls) # HACK for specs
  obj, cls = cls, obj if flip
  refute obj.kind_of?(cls), msg
end

#refute_match(exp, act, msg = nil) ⇒ Object



251
252
253
254
# File 'lib/mini/test.rb', line 251

def refute_match exp, act, msg = nil
  msg = message msg, "Expected #{mu_pp(act)} to not match #{mu_pp(exp)}"
  refute act =~ exp, msg
end

#refute_nil(obj, msg = nil) ⇒ Object



256
257
258
259
# File 'lib/mini/test.rb', line 256

def refute_nil obj, msg = nil
  msg = message msg, "Expected #{mu_pp(obj)} to not be nil"
  refute obj.nil?, msg
end

#refute_operator(o1, op, o2, msg = nil) ⇒ Object



261
262
263
264
# File 'lib/mini/test.rb', line 261

def refute_operator o1, op, o2, msg = nil
  msg = message msg, "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}"
  refute o1.__send__(op, o2), msg
end

#refute_respond_to(obj, meth, msg = nil) ⇒ Object



266
267
268
269
270
271
# File 'lib/mini/test.rb', line 266

def refute_respond_to obj, meth, msg = nil
  msg = message msg, "Expected #{mu_pp(obj)} to not respond to #{meth}"
  flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs
  obj, meth = meth, obj if flip
  refute obj.respond_to?(meth), msg
end

#refute_same(exp, act, msg = nil) ⇒ Object



273
274
275
276
# File 'lib/mini/test.rb', line 273

def refute_same exp, act, msg = nil
  msg = message msg, "Expected #{mu_pp(act)} to not be the same as #{mu_pp(exp)}"
  refute exp.equal?(act), msg
end