Module: Test::Unit::Assertions

Included in:
TestCase
Defined in:
lib/test/unit/assertions.rb

Overview

Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is included in Test::Unit::TestCase.

To include it in your own code and use its functionality, you simply need to rescue Test::Unit::AssertionFailedError. Additionally you may override add_assertion to get notified whenever an assertion is made.

Notes:

  • The message to each assertion, if given, will be propagated with the failure.

  • It is easy to add your own assertions based on assert_block().

Example Custom Assertion

def deny(boolean, message = nil)
  message = build_message message, '<?> is not false or nil.', boolean
  assert_block message do
    not boolean
  end
end

Defined Under Namespace

Classes: AssertExceptionHelper, AssertionMessage

Constant Summary collapse

UncaughtThrow =
{
  NameError => /^uncaught throw \`(.+)\'$/, #`
  ArgumentError => /^uncaught throw (.+)$/,
  ThreadError => /^uncaught throw \`(.+)\' in thread / #`
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.use_pp=(value) ⇒ Object



1380
1381
1382
# File 'lib/test/unit/assertions.rb', line 1380

def self.use_pp=(value)
  AssertionMessage.use_pp = value
end

Instance Method Details

#assert(boolean, message = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/test/unit/assertions.rb', line 63

def assert(boolean, message=nil)
  _wrap_assertion do
    case message
    when nil, String, Proc
    else
      error_message = "assertion message must be String or Proc: "
      error_message << "<#{message.inspect}>(<#{message.class}>)"
      raise ArgumentError, error_message, filter_backtrace(caller)
    end
    assert_block("assert should not be called with a block.") do
      !block_given?
    end
    assert_block(build_message(message, "<?> is not true.", boolean)) do
      boolean
    end
  end
end

#assert_alias_method(object, alias_name, original_name, message = nil) ⇒ Object

Passes if object#alias_name is an alias method of object#original_name.

Example:

assert_alias_method([], :length, :size)  # -> pass
assert_alias_method([], :size, :length)  # -> pass
assert_alias_method([], :each, :size)    # -> fail


1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'lib/test/unit/assertions.rb', line 1175

def assert_alias_method(object, alias_name, original_name, message=nil)
  _wrap_assertion do
    find_method_failure_message = Proc.new do |method_name|
      build_message(message,
                    "<?>.? doesn't exist\n" +
                    "(Class: <?>)",
                    object,
                    AssertionMessage.literal(method_name),
                    object.class)
    end

    alias_method = original_method = nil
    assert_block(find_method_failure_message.call(alias_name)) do
      begin
        alias_method = object.method(alias_name)
        true
      rescue NameError
        false
      end
    end
    assert_block(find_method_failure_message.call(original_name)) do
      begin
        original_method = object.method(original_name)
        true
      rescue NameError
        false
      end
    end

    full_message = build_message(message,
                                 "<?> is alias of\n" +
                                 "<?> expected",
                                 alias_method,
                                 original_method)
    assert_block(full_message) do
      alias_method == original_method
    end
  end
end

#assert_block(message = "assert_block failed.") ⇒ Object

:yields:



48
49
50
51
52
53
54
# File 'lib/test/unit/assertions.rb', line 48

def assert_block(message="assert_block failed.") # :yields:
  _wrap_assertion do
    if (! yield)
      raise AssertionFailedError.new(message.to_s)
    end
  end
end

#assert_boolean(actual, message = nil) ⇒ Object

Passes if actual is a boolean value.

Example:

assert_boolean(true) # -> pass
assert_boolean(nil)  # -> fail


951
952
953
954
955
956
957
958
959
# File 'lib/test/unit/assertions.rb', line 951

def assert_boolean(actual, message=nil)
  _wrap_assertion do
    assert_block(build_message(message,
                               "<true> or <false> expected but was\n<?>",
                               actual)) do
      [true, false].include?(actual)
    end
  end
end

#assert_compare(expected, operator, actual, message = nil) ⇒ Object

Passes if expression “expected operator actual” is true.

Example:

assert_compare(1, "<", 10)  # -> pass
assert_compare(1, ">=", 10) # -> fail


1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/test/unit/assertions.rb', line 1000

def assert_compare(expected, operator, actual, message=nil)
  _wrap_assertion do
    assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
    case operator.to_s
    when "<"
      operator_description = "less than"
    when "<="
      operator_description = "less than or equal to"
    when ">"
      operator_description = "greater than"
    when ">="
      operator_description = "greater than or equal to"
    end
    template = <<-EOT
<?> #{operator} <?> should be true
<?> expected #{operator_description}
<?>.
EOT
    full_message = build_message(message, template,
                                 expected, actual,
                                 expected, actual)
    assert_block(full_message) do
      expected.send(operator, actual)
    end
  end
end

#assert_const_defined(object, constant_name, message = nil) ⇒ Object

Passes if object.const_defined?(constant_name)

Example:

assert_const_defined(Test, :Unit)          # -> pass
assert_const_defined(Object, :Nonexistent) # -> fail


1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/test/unit/assertions.rb', line 1095

def assert_const_defined(object, constant_name, message=nil)
  _wrap_assertion do
    full_message = build_message(message,
                                 "<?>.const_defined\\?(<?>) expected.",
                                 object, constant_name)
    assert_block(full_message) do
      object.const_defined?(constant_name)
    end
  end
end

#assert_empty(object, message = nil) ⇒ Object

Passes if object is empty.

Example:

assert_empty("")                       # -> pass
assert_empty([])                       # -> pass
assert_empty({})                       # -> pass
assert_empty(" ")                      # -> fail
assert_empty([nil])                    # -> fail
assert_empty({1 => 2})                 # -> fail


1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
# File 'lib/test/unit/assertions.rb', line 1305

def assert_empty(object, message=nil)
  _wrap_assertion do
    assert_respond_to(object, :empty?,
                      "The object must respond to :empty?.")
    full_message = build_message(message,
                                 "<?> expected to be empty.",
                                 object)
    assert_block(full_message) do
      object.empty?
    end
  end
end

#assert_equal(expected, actual, message = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/test/unit/assertions.rb', line 92

def assert_equal(expected, actual, message=nil)
  diff = AssertionMessage.delayed_diff(expected, actual)
  if expected.respond_to?(:encoding) and
      actual.respond_to?(:encoding) and
      expected.encoding != actual.encoding
    format = <<EOT
<?>(?) expected but was
<?>(?).?
EOT
    full_message = build_message(message, format,
                                 expected, expected.encoding.name,
                                 actual, actual.encoding.name,
                                 diff)
  else
    full_message = build_message(message, <<EOT, expected, actual, diff)
<?> expected but was
<?>.?
EOT
  end
  begin
    assert_block(full_message) { expected == actual }
  rescue AssertionFailedError => failure
    failure.expected = expected
    failure.actual = actual
    failure.inspected_expected = AssertionMessage.convert(expected)
    failure.inspected_actual = AssertionMessage.convert(actual)
    failure.user_message = message
    raise
  end
end

#assert_fail_assertion(message = nil) ⇒ Object

Passes if assertion is failed in block.

Example:

assert_fail_assertion {assert_equal("A", "B")}  # -> pass
assert_fail_assertion {assert_equal("A", "A")}  # -> fail


1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/test/unit/assertions.rb', line 1033

def assert_fail_assertion(message=nil)
  _wrap_assertion do
    full_message = build_message(message,
                                 "Failed assertion was expected.")
    assert_block(full_message) do
      begin
        yield
        false
      rescue AssertionFailedError
        true
      end
    end
  end
end

#assert_false(actual, message = nil) ⇒ Object

Passes if actual is false.

Example:

assert_false(false)  # -> pass
assert_false(nil)    # -> fail


983
984
985
986
987
988
989
990
991
# File 'lib/test/unit/assertions.rb', line 983

def assert_false(actual, message=nil)
  _wrap_assertion do
    assert_block(build_message(message,
                               "<false> expected but was\n<?>",
                               actual)) do
      actual == false
    end
  end
end

#assert_in_delta(expected_float, actual_float, delta = 0.001, message = "") ⇒ Object



613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/test/unit/assertions.rb', line 613

def assert_in_delta(expected_float, actual_float, delta=0.001, message="")
  _wrap_assertion do
    _assert_in_delta_validate_arguments(expected_float,
                                        actual_float,
                                        delta)
    full_message = _assert_in_delta_message(expected_float,
                                            actual_float,
                                            delta,
                                            message)
    assert_block(full_message) do
      (expected_float.to_f - actual_float.to_f).abs <= delta.to_f
    end
  end
end

#assert_in_epsilon(expected_float, actual_float, epsilon = 0.001, message = "") ⇒ Object



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'lib/test/unit/assertions.rb', line 737

def assert_in_epsilon(expected_float, actual_float, epsilon=0.001,
                      message="")
  _wrap_assertion do
    _assert_in_epsilon_validate_arguments(expected_float,
                                          actual_float,
                                          epsilon)
    full_message = _assert_in_epsilon_message(expected_float,
                                              actual_float,
                                              epsilon,
                                              message)
    assert_block(full_message) do
      normalized_expected_float = expected_float.to_f
      delta = normalized_expected_float * epsilon.to_f
      (normalized_expected_float - actual_float.to_f).abs <= delta
    end
  end
end

#assert_include(collection, object, message = nil) ⇒ Object

Passes if collection includes object.

Example:

assert_include([1, 10], 1)            # -> pass
assert_include(1..10, 5)              # -> pass
assert_include([1, 10], 5)            # -> fail
assert_include(1..10, 20)             # -> fail


1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'lib/test/unit/assertions.rb', line 1259

def assert_include(collection, object, message=nil)
  _wrap_assertion do
    assert_respond_to(collection, :include?,
                      "The collection must respond to :include?.")
    full_message = build_message(message,
                                 "<?> expected to include\n<?>.",
                                 collection,
                                 object)
    assert_block(full_message) do
      collection.include?(object)
    end
  end
end

#assert_instance_of(klass, object, message = "") ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/test/unit/assertions.rb', line 200

def assert_instance_of(klass, object, message="")
  _wrap_assertion do
    klasses = nil
    klasses = klass if klass.is_a?(Array)
    assert_block("The first parameter to assert_instance_of should be " +
                 "a Class or an Array of Class.") do
      if klasses
        klasses.all? {|k| k.is_a?(Class)}
      else
        klass.is_a?(Class)
      end
    end
    klass_message = AssertionMessage.maybe_container(klass) do |value|
      "<#{value}>"
    end
    full_message = build_message(message, <<EOT, object, klass_message, object.class)
<?> expected to be an instance of
? but was
<?>.
EOT
    assert_block(full_message) do
      if klasses
        klasses.any? {|k| object.instance_of?(k)}
      else
        object.instance_of?(klass)
      end
    end
  end
end

#assert_kind_of(klass, object, message = "") ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/test/unit/assertions.rb', line 255

def assert_kind_of(klass, object, message="")
  _wrap_assertion do
    klasses = nil
    klasses = klass if klass.is_a?(Array)
    assert_block("The first parameter to assert_kind_of should be " +
                 "a kind_of Module or an Array of a kind_of Module.") do
      if klasses
        klasses.all? {|k| k.kind_of?(Module)}
      else
        klass.kind_of?(Module)
      end
    end
    klass_message = AssertionMessage.maybe_container(klass) do |value|
      "<#{value}>"
    end
    full_message = build_message(message,
                                 "<?> expected to be kind_of\\?\n" +
                                 "? but was\n" +
                                 "<?>.",
                                 object,
                                 klass_message,
                                 object.class)
    assert_block(full_message) do
      if klasses
        klasses.any? {|k| object.kind_of?(k)}
      else
        object.kind_of?(klass)
      end
    end
  end
end

#assert_match(pattern, string, message = "") ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/test/unit/assertions.rb', line 343

def assert_match(pattern, string, message="")
  _wrap_assertion do
    pattern = case(pattern)
      when String
        Regexp.new(Regexp.escape(pattern))
      else
        pattern
    end
    full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
    assert_block(full_message) { string =~ pattern }
  end
end

#assert_nil(object, message = "") ⇒ Object



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

def assert_nil(object, message="")
  full_message = build_message(message, <<EOT, object)
<?> expected to be nil.
EOT
  assert_block(full_message) { object.nil? }
end

#assert_no_match(regexp, string, message = "") ⇒ Object



512
513
514
515
516
517
518
519
# File 'lib/test/unit/assertions.rb', line 512

def assert_no_match(regexp, string, message="")
  _wrap_assertion do
    assert_instance_of(Regexp, regexp,
                       "The first argument to assert_no_match " +
                       "should be a Regexp.")
    assert_not_match(regexp, string, message)
  end
end

#assert_not_const_defined(object, constant_name, message = nil) ⇒ Object

Passes if !object.const_defined?(constant_name)

Example:

assert_not_const_defined(Object, :Nonexistent) # -> pass
assert_not_const_defined(Test, :Unit)          # -> fail


1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
# File 'lib/test/unit/assertions.rb', line 1112

def assert_not_const_defined(object, constant_name, message=nil)
  _wrap_assertion do
    full_message = build_message(message,
                                 "!<?>.const_defined\\?(<?>) expected.",
                                 object, constant_name)
    assert_block(full_message) do
      !object.const_defined?(constant_name)
    end
  end
end

#assert_not_empty(object, message = nil) ⇒ Object

Passes if object is not empty.

Example:

assert_not_empty(" ")                      # -> pass
assert_not_empty([nil])                    # -> pass
assert_not_empty({1 => 2})                 # -> pass
assert_not_empty("")                       # -> fail
assert_not_empty([])                       # -> fail
assert_not_empty({})                       # -> fail


1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
# File 'lib/test/unit/assertions.rb', line 1328

def assert_not_empty(object, message=nil)
  _wrap_assertion do
    assert_respond_to(object, :empty?,
                      "The object must respond to :empty?.")
    full_message = build_message(message,
                                 "<?> expected to not be empty.",
                                 object)
    assert_block(full_message) do
      not object.empty?
    end
  end
end

#assert_not_equal(expected, actual, message = "") ⇒ Object



465
466
467
468
# File 'lib/test/unit/assertions.rb', line 465

def assert_not_equal(expected, actual, message="")
  full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
  assert_block(full_message) { expected != actual }
end

#assert_not_in_delta(expected_float, actual_float, delta = 0.001, message = "") ⇒ Object



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/test/unit/assertions.rb', line 637

def assert_not_in_delta(expected_float, actual_float, delta=0.001, message="")
  _wrap_assertion do
    _assert_in_delta_validate_arguments(expected_float,
                                        actual_float,
                                        delta)
    full_message = _assert_in_delta_message(expected_float,
                                            actual_float,
                                            delta,
                                            message,
                                            :negative_assertion => true)
    assert_block(full_message) do
      (expected_float.to_f - actual_float.to_f).abs > delta.to_f
    end
  end
end

#assert_not_in_epsilon(expected_float, actual_float, epsilon = 0.001, message = "") ⇒ Object



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/test/unit/assertions.rb', line 765

def assert_not_in_epsilon(expected_float, actual_float, epsilon=0.001,
                          message="")
  _wrap_assertion do
    _assert_in_epsilon_validate_arguments(expected_float,
                                          actual_float,
                                          epsilon)
    full_message = _assert_in_epsilon_message(expected_float,
                                              actual_float,
                                              epsilon,
                                              message,
                                              :negative_assertion => true)
    assert_block(full_message) do
      normalized_expected_float = expected_float.to_f
      delta = normalized_expected_float * epsilon.to_f
      (normalized_expected_float - actual_float.to_f).abs > delta
    end
  end
end

#assert_not_include(collection, object, message = nil) ⇒ Object

Passes if collection doesn’t include object.

Example:

assert_not_include([1, 10], 5)            # -> pass
assert_not_include(1..10, 20)             # -> pass
assert_not_include([1, 10], 1)            # -> fail
assert_not_include(1..10, 5)              # -> fail


1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
# File 'lib/test/unit/assertions.rb', line 1281

def assert_not_include(collection, object, message=nil)
  _wrap_assertion do
    assert_respond_to(collection, :include?,
                      "The collection must respond to :include?.")
    full_message = build_message(message,
                                 "<?> expected to not include\n<?>.",
                                 collection,
                                 object)
    assert_block(full_message) do
      not collection.include?(object)
    end
  end
end

#assert_not_match(regexp, string, message = "") ⇒ Object



490
491
492
493
494
495
496
497
498
499
500
# File 'lib/test/unit/assertions.rb', line 490

def assert_not_match(regexp, string, message="")
  _wrap_assertion do
    assert_instance_of(Regexp, regexp,
                       "<REGEXP> in assert_not_match(<REGEXP>, ...) " +
                       "should be a Regexp.")
    full_message = build_message(message,
                                 "<?> expected to not match\n<?>.",
                                 regexp, string)
    assert_block(full_message) { regexp !~ string }
  end
end

#assert_not_nil(object, message = "") ⇒ Object



477
478
479
480
# File 'lib/test/unit/assertions.rb', line 477

def assert_not_nil(object, message="")
  full_message = build_message(message, "<?> expected to not be nil.", object)
  assert_block(full_message){!object.nil?}
end

#assert_not_predicate(object, predicate, message = nil) ⇒ Object

Passes if object.predicate is not true.

Example:

assert_not_predicate([1], :empty?) # -> pass
assert_not_predicate([], :empty?)  # -> fail


1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'lib/test/unit/assertions.rb', line 1151

def assert_not_predicate(object, predicate, message=nil)
  _wrap_assertion do
    assert_respond_to(object, predicate, message)
    actual = object.send(predicate)
    full_message = build_message(message,
                                 "<?>.? is false value expected but was\n" +
                                 "<?>",
                                 object,
                                 AssertionMessage.literal(predicate),
                                 actual)
    assert_block(full_message) do
      not actual
    end
  end
end

#assert_not_respond_to(object, method, message = "") ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/test/unit/assertions.rb', line 319

def assert_not_respond_to(object, method, message="")
  _wrap_assertion do
    full_message = build_message(message,
                                 "<?>.kind_of\\?(Symbol) or\n" +
                                 "<?>.respond_to\\?(:to_str) expected",
                                 method, method)
    assert_block(full_message) do
      method.kind_of?(Symbol) or method.respond_to?(:to_str)
    end
    full_message = build_message(message,
                                 "!<?>.respond_to\\?(?) expected\n" +
                                 "(Class: <?>)",
                                 object, method, object.class)
    assert_block(full_message) {!object.respond_to?(method)}
  end
end

#assert_not_same(expected, actual, message = "") ⇒ Object



448
449
450
451
452
453
454
455
456
# File 'lib/test/unit/assertions.rb', line 448

def assert_not_same(expected, actual, message="")
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
<?>
with id <?> expected to not be equal\\? to
<?>
with id <?>.
EOT
  assert_block(full_message) { !actual.equal?(expected) }
end

#assert_not_send(send_array, message = nil) ⇒ Object

Passes if the method send doesn’t return a true value.

send_array is composed of:

  • A receiver

  • A method

  • Arguments to the method

Example:

assert_not_send([[1, 2], :member?, 1]) # -> fail
assert_not_send([[1, 2], :member?, 4]) # -> pass


916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'lib/test/unit/assertions.rb', line 916

def assert_not_send(send_array, message=nil)
  _wrap_assertion do
    assert_instance_of(Array, send_array,
                       "assert_not_send requires an array " +
                       "of send information")
    assert_operator(send_array.size, :>=, 2,
                    "assert_not_send requires at least a receiver " +
                    "and a message name")
    format = <<EOT
<?> expected to respond to
<?(*?)> with not a true value but was
<?>.
EOT
    receiver, message_name, *arguments = send_array
    result = nil
    full_message =
      build_message(message,
                    format,
                    receiver,
                    AssertionMessage.literal(message_name.to_s),
                    arguments,
                    AssertionMessage.delayed_literal {result})
    assert_block(full_message) do
      result = receiver.__send__(message_name, *arguments)
      not result
    end
  end
end

#assert_nothing_raised(*args) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/test/unit/assertions.rb', line 406

def assert_nothing_raised(*args)
  _wrap_assertion do
    if args.last.is_a?(String)
      message = args.pop
    else
      message = ""
    end

    assert_exception_helper = AssertExceptionHelper.new(self, args)
    begin
      yield
    rescue Exception => e
      if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
          assert_exception_helper.expected?(e))
        failure_message = build_message(message, "Exception raised:\n?", e)
        assert_block(failure_message) {false}
      else
        raise
      end
    end
    nil
  end
end

#assert_nothing_thrown(message = "", &proc) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/test/unit/assertions.rb', line 587

def assert_nothing_thrown(message="", &proc)
  _wrap_assertion do
    assert(block_given?, "Should have passed a block to assert_nothing_thrown")
    begin
      proc.call
    rescue NameError, ArgumentError, ThreadError => error
      raise unless UncaughtThrow[error.class] =~ error.message
      tag = $1
      tag = tag[1..-1].intern if tag[0, 1] == ":"
      full_message = build_message(message,
                                   "<?> was thrown when nothing was expected",
                                   tag)
      flunk(full_message)
    end
    assert(true, "Expected nothing to be thrown")
  end
end

#assert_operator(object1, operator, object2, message = "") ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/test/unit/assertions.rb', line 384

def assert_operator(object1, operator, object2, message="")
  _wrap_assertion do
    full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
    assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
    full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
<?> expected to be
?
<?>.
EOT
    assert_block(full_message) { object1.__send__(operator, object2) }
  end
end

#assert_path_exist(path, message = nil) ⇒ Object

Passes if path exists.

Example:

assert_path_exist("/tmp")          # -> pass
assert_path_exist("/bin/sh")       # -> pass
assert_path_exist("/nonexistent")  # -> fail


1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
# File 'lib/test/unit/assertions.rb', line 1222

def assert_path_exist(path, message=nil)
  _wrap_assertion do
    failure_message = build_message(message,
                                    "<?> expected to exist",
                                    path)
    assert_block(failure_message) do
      File.exist?(path)
    end
  end
end

#assert_path_not_exist(path, message = nil) ⇒ Object

Passes if path doesn’t exist.

Example:

assert_path_not_exist("/nonexistent")  # -> pass
assert_path_not_exist("/tmp")          # -> fail
assert_path_not_exist("/bin/sh")       # -> fail


1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/test/unit/assertions.rb', line 1240

def assert_path_not_exist(path, message=nil)
  _wrap_assertion do
    failure_message = build_message(message,
                                    "<?> expected to not exist",
                                    path)
    assert_block(failure_message) do
      not File.exist?(path)
    end
  end
end

#assert_predicate(object, predicate, message = nil) ⇒ Object

Passes if object.predicate is true.

Example:

assert_predicate([], :empty?)  # -> pass
assert_predicate([1], :empty?) # -> fail


1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'lib/test/unit/assertions.rb', line 1129

def assert_predicate(object, predicate, message=nil)
  _wrap_assertion do
    assert_respond_to(object, predicate, message)
    actual = object.send(predicate)
    full_message = build_message(message,
                                 "<?>.? is true value expected but was\n" +
                                 "<?>",
                                 object,
                                 AssertionMessage.literal(predicate),
                                 actual)
    assert_block(full_message) do
      actual
    end
  end
end

#assert_raise(*args, &block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/test/unit/assertions.rb', line 141

def assert_raise(*args, &block)
  assert_expected_exception = Proc.new do |*_args|
    message, assert_exception_helper, actual_exception = _args
    expected = assert_exception_helper.expected_exceptions
    full_message = build_message(message,
                                 "<?> exception expected but was\n?",
                                 expected, actual_exception)
    assert_block(full_message) do
      expected == [] or assert_exception_helper.expected?(actual_exception)
    end
  end
  _assert_raise(assert_expected_exception, *args, &block)
end

#assert_raise_kind_of(*args, &block) ⇒ Object

Passes if the block raises one of the given exceptions or sub exceptions of the given exceptions.

Example:

assert_raise_kind_of(SystemCallError) do
  raise Errno::EACCES
end


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

def assert_raise_kind_of(*args, &block)
  assert_expected_exception = Proc.new do |*_args|
    message, assert_exception_helper, actual_exception = _args
    expected = assert_exception_helper.expected_exceptions
    full_message = build_message(message,
                                 "<?> family exception expected " +
                                 "but was\n?",
                                 expected, actual_exception)
    assert_block(full_message) do
      assert_exception_helper.expected?(actual_exception, :kind_of?)
    end
  end
  _assert_raise(assert_expected_exception, *args, &block)
end

#assert_raise_message(expected, message = nil) ⇒ Object

Passes if an exception is raised in block and its message is expected.

Example:

assert_raise_message("exception") {raise "exception"}  # -> pass
assert_raise_message(/exc/i) {raise "exception"}       # -> pass
assert_raise_message("exception") {raise "EXCEPTION"}  # -> fail
assert_raise_message("exception") {}                   # -> fail


1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
# File 'lib/test/unit/assertions.rb', line 1057

def assert_raise_message(expected, message=nil)
  _wrap_assertion do
    full_message = build_message(message,
                                 "<?> exception message expected " +
                                 "but none was thrown.",
                                 expected)
    exception = nil
    assert_block(full_message) do
      begin
        yield
        false
      rescue Exception => exception
        true
      end
    end

    actual = exception.message
    diff = AssertionMessage.delayed_diff(expected, actual)
    full_message =
      build_message(message,
                    "<?> exception message expected but was\n" +
                    "<?>.?", expected, actual, diff)
    assert_block(full_message) do
      if expected.is_a?(Regexp)
        expected =~ actual
      else
        expected == actual
      end
    end
  end
end

#assert_raises(*args, &block) ⇒ Object



161
162
163
# File 'lib/test/unit/assertions.rb', line 161

def assert_raises(*args, &block)
  assert_raise(*args, &block)
end

#assert_respond_to(object, method, message = "") ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/test/unit/assertions.rb', line 294

def assert_respond_to(object, method, message="")
  _wrap_assertion do
    full_message = build_message(message,
                                 "<?>.kind_of\\?(Symbol) or\n" +
                                 "<?>.respond_to\\?(:to_str) expected",
                                 method, method)
    assert_block(full_message) do
      method.kind_of?(Symbol) or method.respond_to?(:to_str)
    end
    full_message = build_message(message,
                                 "<?>.respond_to\\?(?) expected\n" +
                                 "(Class: <?>)",
                                 object, method, object.class)
    assert_block(full_message) {object.respond_to?(method)}
  end
end

#assert_same(expected, actual, message = "") ⇒ Object



365
366
367
368
369
370
371
372
373
# File 'lib/test/unit/assertions.rb', line 365

def assert_same(expected, actual, message="")
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
<?>
with id <?> expected to be equal\\? to
<?>
with id <?>.
EOT
  assert_block(full_message) { actual.equal?(expected) }
end

#assert_send(send_array, message = nil) ⇒ Object



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/test/unit/assertions.rb', line 876

def assert_send(send_array, message=nil)
  _wrap_assertion do
    assert_instance_of(Array, send_array,
                       "assert_send requires an array " +
                       "of send information")
    assert_operator(send_array.size, :>=, 2,
                    "assert_send requires at least a receiver " +
                    "and a message name")
    format = <<EOT
<?> expected to respond to
<?(*?)> with a true value but was
<?>.
EOT
    receiver, message_name, *arguments = send_array
    result = nil
    full_message =
      build_message(message,
                    format,
                    receiver,
                    AssertionMessage.literal(message_name.to_s),
                    arguments,
                    AssertionMessage.delayed_literal {result})
    assert_block(full_message) do
      result = receiver.__send__(message_name, *arguments)
      result
    end
  end
end

#assert_throw(expected_object, message = "", &proc) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/test/unit/assertions.rb', line 536

def assert_throw(expected_object, message="", &proc)
  _wrap_assertion do
    begin
      catch([]) {}
    rescue TypeError
      assert_instance_of(Symbol, expected_object,
                         "assert_throws expects the symbol that should be thrown for its first argument")
    end
    assert_block("Should have passed a block to assert_throw.") do
      block_given?
    end
    caught = true
    begin
      catch(expected_object) do
        proc.call
        caught = false
      end
      full_message = build_message(message,
                                   "<?> should have been thrown.",
                                   expected_object)
      assert_block(full_message) {caught}
    rescue NameError, ArgumentError, ThreadError => error
      raise unless UncaughtThrow[error.class] =~ error.message
      tag = $1
      tag = tag[1..-1].intern if tag[0, 1] == ":"
      full_message = build_message(message,
                                   "<?> expected to be thrown but\n" +
                                   "<?> was thrown.",
                                   expected_object, tag)
      flunk(full_message)
    end
  end
end

#assert_throws(*args, &block) ⇒ Object

Alias of assert_throw.

Will be deprecated in 1.9, and removed in 2.0.



574
575
576
# File 'lib/test/unit/assertions.rb', line 574

def assert_throws(*args, &block)
  assert_throw(*args, &block)
end

#assert_true(actual, message = nil) ⇒ Object

Passes if actual is true.

Example:

assert_true(true)  # -> pass
assert_true(:true) # -> fail


967
968
969
970
971
972
973
974
975
# File 'lib/test/unit/assertions.rb', line 967

def assert_true(actual, message=nil)
  _wrap_assertion do
    assert_block(build_message(message,
                               "<true> expected but was\n<?>",
                               actual)) do
      actual == true
    end
  end
end

#build_message(head, template = nil, *arguments) ⇒ Object



1346
1347
1348
1349
# File 'lib/test/unit/assertions.rb', line 1346

def build_message(head, template=nil, *arguments)
  template &&= template.chomp
  return AssertionMessage.new(head, template, arguments)
end

#flunk(message = "Flunked") ⇒ Object



437
438
439
# File 'lib/test/unit/assertions.rb', line 437

def flunk(message="Flunked")
  assert_block(build_message(message)){false}
end