Module: MiniTest::Assertions

Included in:
Unit::TestCase
Defined in:
lib/minitest/unit.rb

Instance Method Summary collapse

Instance Method Details

#_assertionsObject



51
52
53
# File 'lib/minitest/unit.rb', line 51

def _assertions
  @_assertions ||= 0
end

#_assertions=(n) ⇒ Object



47
48
49
# File 'lib/minitest/unit.rb', line 47

def _assertions= n
  @_assertions = n
end

#assert(test, msg = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/minitest/unit.rb', line 55

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

#assert_block(msg = nil) ⇒ Object



65
66
67
68
# File 'lib/minitest/unit.rb', line 65

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

#assert_empty(obj, msg = nil) ⇒ Object



70
71
72
73
74
# File 'lib/minitest/unit.rb', line 70

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



76
77
78
79
# File 'lib/minitest/unit.rb', line 76

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



81
82
83
84
85
# File 'lib/minitest/unit.rb', line 81

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



87
88
89
# File 'lib/minitest/unit.rb', line 87

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



91
92
93
94
95
96
97
# File 'lib/minitest/unit.rb', line 91

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

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



99
100
101
102
103
104
# File 'lib/minitest/unit.rb', line 99

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

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

TODO: merge with instance_of



106
107
108
109
110
111
112
# File 'lib/minitest/unit.rb', line 106

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}, not #{obj.class}" }
  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



114
115
116
117
118
119
# File 'lib/minitest/unit.rb', line 114

def assert_match exp, act, msg = nil
  msg = message(msg) { "Expected #{mu_pp(exp)} to match #{mu_pp(act)}" }
  assert_respond_to act, :"=~"
  exp = /#{Regexp.escape(exp)}/ if String === exp && String === act
  assert exp =~ act, msg
end

#assert_nil(obj, msg = nil) ⇒ Object



121
122
123
124
# File 'lib/minitest/unit.rb', line 121

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



126
127
128
129
# File 'lib/minitest/unit.rb', line 126

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/minitest/unit.rb', line 131

def assert_raises *exp
  msg = String === exp.last ? exp.pop : nil
  should_raise = false
  begin
    yield
    should_raise = true
  rescue Exception => e
    assert(exp.any? { |ex|
             ex.instance_of?(Module) ? e.kind_of?(ex) : ex == 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



149
150
151
152
153
154
155
156
# File 'lib/minitest/unit.rb', line 149

def assert_respond_to obj, meth, msg = nil
  msg = message(msg) {
      "Expected #{mu_pp(obj)} (#{obj.class}) 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



158
159
160
161
162
163
164
# File 'lib/minitest/unit.rb', line 158

def assert_same exp, act, msg = nil
  msg = message(msg) {
    data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
    "Expected %s (0x%x) to be the same as %s (0x%x)" % data
  }
  assert exp.equal?(act), msg
end

#assert_send(send_ary, m = nil) ⇒ Object



166
167
168
169
170
171
# File 'lib/minitest/unit.rb', line 166

def assert_send send_ary, m = nil
  recv, msg, *args = send_ary
  m = message(m) {
    "Expected #{mu_pp(recv)}.#{msg}(*#{mu_pp(args)}) to return true" }
  assert recv.__send__(msg, *args), m
end

#assert_throws(sym, msg = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/minitest/unit.rb', line 173

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/minitest/unit.rb', line 190

def capture_io
  require 'stringio'

  orig_stdout, orig_stderr         = $stdout, $stderr
  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



205
206
207
# File 'lib/minitest/unit.rb', line 205

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

#flunk(msg = nil) ⇒ Object



209
210
211
212
# File 'lib/minitest/unit.rb', line 209

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

#message(msg = nil, &default) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/minitest/unit.rb', line 214

def message msg = nil, &default
  proc {
    if msg then
      msg = msg.to_s unless String === msg
      msg += '.' unless msg.empty?
      msg += "\n#{default.call}."
      msg.strip
    else
      "#{default.call}."
    end
  }
end

#mu_pp(obj) ⇒ Object



41
42
43
44
45
# File 'lib/minitest/unit.rb', line 41

def mu_pp(obj)
  s = obj.inspect
  s = s.force_encoding(Encoding.default_external) if defined? Encoding
  s
end

#pass(msg = nil) ⇒ Object

used for counting assertions



228
229
230
# File 'lib/minitest/unit.rb', line 228

def pass msg = nil
  assert true
end

#refute(test, msg = nil) ⇒ Object



232
233
234
235
# File 'lib/minitest/unit.rb', line 232

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

#refute_empty(obj, msg = nil) ⇒ Object



237
238
239
240
241
# File 'lib/minitest/unit.rb', line 237

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



243
244
245
246
# File 'lib/minitest/unit.rb', line 243

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



248
249
250
251
252
# File 'lib/minitest/unit.rb', line 248

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



254
255
256
# File 'lib/minitest/unit.rb', line 254

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



258
259
260
261
262
263
264
# File 'lib/minitest/unit.rb', line 258

def refute_includes collection, obj, msg = nil
  msg = message(msg) { "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}" }
  flip = (obj.respond_to? :include?) && ! (collection.respond_to? :include?) # HACK for specs
  obj, collection = collection, obj if flip
  assert_respond_to collection, :include?
  refute collection.include?(obj), msg
end

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



266
267
268
269
270
271
# File 'lib/minitest/unit.rb', line 266

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 obj.instance_of?(cls), msg
end

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

TODO: merge with instance_of



273
274
275
276
277
278
# File 'lib/minitest/unit.rb', line 273

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



280
281
282
283
284
285
# File 'lib/minitest/unit.rb', line 280

def refute_match exp, act, msg = nil
  msg = message(msg) { "Expected #{mu_pp(exp)} to not match #{mu_pp(act)}" }
  assert_respond_to act, :"=~"
  exp = /#{Regexp.escape(exp)}/ if String === exp && String === act
  refute exp =~ act, msg
end

#refute_nil(obj, msg = nil) ⇒ Object



287
288
289
290
# File 'lib/minitest/unit.rb', line 287

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



292
293
294
295
# File 'lib/minitest/unit.rb', line 292

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



297
298
299
300
301
302
# File 'lib/minitest/unit.rb', line 297

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



304
305
306
307
# File 'lib/minitest/unit.rb', line 304

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

#skip(msg = nil, bt = caller) ⇒ Object

Raises:



309
310
311
312
# File 'lib/minitest/unit.rb', line 309

def skip msg = nil, bt = caller
  msg ||= "Skipped, no message given"
  raise MiniTest::Skip, msg, bt
end