class Test::Unit::TestAssertions

Public Instance Methods

test_assert_alias_method() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1381
def test_assert_alias_method
  object = Object.new
  class << object
    def original_method
    end
    alias_method :alias_method, :original_method

    def other
    end
  end

  check_nothing_fails do
    assert_alias_method(object, :alias_method, :original_method)
  end

  check_nothing_fails do
    assert_alias_method(object, :original_method, :alias_method)
  end

  check_fail("<#{object.method(:other).inspect}> is alias of\n" +
              "<#{object.method(:original_method).inspect}> expected") do
    assert_alias_method(object, :other, :original_method)
  end

  inspected_object = AssertionMessage.convert(object)
  check_fail("<#{inspected_object}>.nonexistent doesn't exist\n" +
              "(Class: <Object>)") do
    assert_alias_method(object, :nonexistent, :original_method)
  end

  check_fail("<#{inspected_object}>.nonexistent doesn't exist\n" +
              "(Class: <Object>)") do
    assert_alias_method(object, :alias_method, :nonexistent)
  end
end
test_assert_boolean() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1142
def test_assert_boolean
  check_nothing_fails do
    assert_boolean(true)
  end
  check_nothing_fails do
    assert_boolean(false)
  end

  check_fail("<true> or <false> expected but was\n<1>") do
    assert_boolean(1)
  end

  check_fail("<true> or <false> expected but was\n<nil>") do
    assert_boolean(nil)
  end

  check_fail("message.\n<true> or <false> expected but was\n<\"XXX\">") do
    assert_boolean("XXX", "message")
  end
end
test_assert_compare() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1200
      def test_assert_compare
        check_nothing_fails do
          assert_compare(1.4, "<", 10.0)
        end

        check_nothing_fails do
          assert_compare(2, "<=", 2)
        end

        check_nothing_fails do
          assert_compare(14, ">=", 10.0)
        end

        check_nothing_fails do
          assert_compare(14, ">", 13.9)
        end

        expected_message = <<-EOM
<15> < <10> should be true
<15> was expected to be less than
<10>.
EOM
        check_fail(expected_message.chomp) do
          assert_compare(15, "<", 10)
        end

        expected_message = <<-EOM
<15> <= <10> should be true
<15> was expected to be less than or equal to
<10>.
EOM
        check_fail(expected_message.chomp) do
          assert_compare(15, "<=", 10)
        end

        expected_message = <<-EOM
<10> > <15> should be true
<10> was expected to be greater than
<15>.
EOM
        check_fail(expected_message.chomp) do
          assert_compare(10, ">", 15)
        end

        expected_message = <<-EOM
<10> >= <15> should be true
<10> was expected to be greater than or equal to
<15>.
EOM
        check_fail(expected_message.chomp) do
          assert_compare(10, ">=", 15)
        end
      end
test_assert_const_defined() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1323
def test_assert_const_defined
  check_nothing_fails do
    assert_const_defined(Test, :Unit)
  end

  check_nothing_fails do
    assert_const_defined(Test, "Unit")
  end

  check_fail("<Test>.const_defined?(<:Nonexistence>) expected.") do
    assert_const_defined(Test, :Nonexistence)
  end
end
test_assert_fail_assertion() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1254
def test_assert_fail_assertion
  check_nothing_fails do
    assert_fail_assertion do
      flunk
    end
  end

  check_fail("Failed assertion was expected.") do
    assert_fail_assertion do
    end
  end
end
test_assert_false() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1182
def test_assert_false
  check_nothing_fails do
    assert_false(false)
  end

  check_fail("<false> expected but was\n<true>") do
    assert_false(true)
  end

  check_fail("<false> expected but was\n<nil>") do
    assert_false(nil)
  end

  check_fail("message.\n<false> expected but was\n<:false>") do
    assert_false(:false, "message")
  end
end
test_assert_instance_of() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 661
def test_assert_instance_of
  check_nothing_fails {
    assert_instance_of(String, "string")
  }
  check_nothing_fails {
    assert_instance_of(String, "string", "successful assert_instance_of")
  }
  check_nothing_fails {
    assert_instance_of(String, "string", "successful assert_instance_of")
  }
  check_fail(%Q{<"string"> was expected to be instance_of?\n<Hash> but was\n<String>.}) {
    assert_instance_of(Hash, "string")
  }
  check_fail(%Q{failed assert_instance_of.\n<"string"> was expected to be instance_of?\n<Hash> but was\n<String>.}) {
    assert_instance_of(Hash, "string", "failed assert_instance_of")
  }

  check_nothing_fails do
    assert_instance_of([Class, NilClass], Array)
  end
  check_fail(%Q{<"string"> was expected to be instance_of?\n[<Class>, <NilClass>] but was\n<String>.}) do
    assert_instance_of([Class, NilClass], "string")
  end
  check_fail(%Q{<Array> was expected to be instance_of?\n[<Module>, <NilClass>] but was\n<Class>.}) do
    assert_instance_of([Module, NilClass], Array)
  end
end
test_assert_kind_of() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 739
def test_assert_kind_of
  check_nothing_fails {
    assert_kind_of(Module, Array)
  }
  check_nothing_fails {
    assert_kind_of(Object, "string", "successful assert_kind_of")
  }
  check_nothing_fails {
    assert_kind_of(String, "string", "successful assert_kind_of")
  }
  check_nothing_fails {
    assert_kind_of(Comparable, 1)
  }
  check_fail(%Q{<"string"> was expected to be kind_of?\n<Class> but was\n<String>.}) {
    assert_kind_of(Class, "string")
  }
  check_fail(%Q{failed assert_kind_of.\n<"string"> was expected to be kind_of?\n<Class> but was\n<String>.}) {
    assert_kind_of(Class, "string", "failed assert_kind_of")
  }

  check_nothing_fails do
    assert_kind_of([Class, NilClass], Array)
  end
  check_fail(%Q{<"string"> was expected to be kind_of?\n[<Class>, <NilClass>] but was\n<String>.}) do
    assert_kind_of([Class, NilClass], "string")
  end
end
test_assert_match() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 792
def test_assert_match
  check_nothing_fails {
    assert_match(/strin./, "string")
  }
  check_nothing_fails {
    assert_match("strin", "string")
  }
  check_nothing_fails {
    assert_match(/strin./, "string", "successful assert_match")
  }
  check_nothing_fails {
    assert_match(/strin./, "string", "successful assert_match")
  }
  check_fail(%Q{</slin./> was expected to be =~\n<"string">.}) {
    assert_match(/slin./, "string")
  }
  check_fail(%Q{</strin\\./> was expected to be =~\n<"string">.}) {
    assert_match("strin.", "string")
  }
  check_fail(%Q{failed assert_match.\n</slin./> was expected to be =~\n<"string">.}) {
    assert_match(/slin./, "string", "failed assert_match")
  }
end
test_assert_nil() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 714
def test_assert_nil
  check_nothing_fails {
    assert_nil(nil)
  }
  check_nothing_fails {
    assert_nil(nil, "successful assert_nil")
  }
  check_nothing_fails {
    assert_nil(nil, "successful assert_nil")
  }
  check_fail(%Q{<"string"> was expected to be nil.}) {
    assert_nil("string")
  }
  check_fail(%Q{failed assert_nil.\n<"string"> was expected to be nil.}) {
    assert_nil("string", "failed assert_nil")
  }
end
test_assert_no_match() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 981
def test_assert_no_match
  check_nothing_fails{assert_no_match(/sling/, "string")}
  check_nothing_fails{assert_no_match(/sling/, "string", "message")}
  check_fail(%Q{The first argument to assert_no_match should be a Regexp.\n<"asdf"> was expected to be instance_of?\n<Regexp> but was\n<String>.}) do
    assert_no_match("asdf", "asdf")
  end
  check_fail(%Q{</string/> was expected to not match\n<"string">.}) do
    assert_no_match(/string/, "string")
  end
  check_fail(%Q{message.\n</string/> was expected to not match\n<"string">.}) do
    assert_no_match(/string/, "string", "message")
  end
end
test_assert_not_const_defined() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1337
def test_assert_not_const_defined
  check_nothing_fails do
    assert_not_const_defined(Test, :Nonexistence)
  end

  check_fail("!<Test>.const_defined?(<:Unit>) expected.") do
    assert_not_const_defined(Test, :Unit)
  end

  check_fail("!<Test>.const_defined?(<\"Unit\">) expected.") do
    assert_not_const_defined(Test, "Unit")
  end
end
test_assert_not_equal() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 929
def test_assert_not_equal
  check_nothing_fails {
    assert_not_equal("string1", "string2")
  }
  check_nothing_fails {
    assert_not_equal("string1", "string2", "message")
  }
  check_fail(%Q{<"string"> was expected to be != to\n<"string">.}) {
    assert_not_equal("string", "string")
  }
  check_fail(%Q{message.\n<"string"> was expected to be != to\n<"string">.}) {
    assert_not_equal("string", "string", "message")
  }
end
test_assert_not_instance_of() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 689
def test_assert_not_instance_of
  check_nothing_fails {
    assert_not_instance_of(NilClass, "string")
  }
  check_nothing_fails {
    assert_not_instance_of(NilClass, "string", "successful assert_instance_of")
  }
  check_fail(%Q{<"string"> was expected to not be instance_of?\n<String> but was.}) {
    assert_not_instance_of(String, "string")
  }
  check_fail(%Q{failed assert.\n<"string"> was expected to not be instance_of?\n<String> but was.}) {
    assert_not_instance_of(String, "string", "failed assert")
  }

  check_nothing_fails do
    assert_not_instance_of([Module, NilClass], Array)
  end
  check_fail(%Q{<Array> was expected to not be instance_of?\n[<Class>, <NilClass>] but was.}) do
    assert_not_instance_of([Class, NilClass], Array)
  end
  check_fail(%Q{<"str"> was expected to not be instance_of?\n[<Numeric>, <String>] but was.}) do
    assert_not_instance_of([Numeric, String], 'str')
  end
end
test_assert_not_kind_of() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 767
def test_assert_not_kind_of
  check_nothing_fails {
    assert_not_kind_of(Class, 42)
  }
  check_nothing_fails {
    assert_not_kind_of(Symbol, "string", "successful assert_not_kind_of")
  }
  check_nothing_fails {
    assert_not_kind_of(Integer, 1.1)
  }
  check_fail(%Q{<1> was expected to not be kind_of?\n<Integer> but was.}) {
    assert_not_kind_of(Integer, 1)
  }
  check_fail(%Q{failed assert_not_kind_of.\n<"string"> was expected to not be kind_of?\n<String> but was.}) {
    assert_not_kind_of(String, "string", "failed assert_not_kind_of")
  }

  check_nothing_fails do
    assert_not_kind_of([String, NilClass], 100)
  end
  check_fail(%Q{<Array> was expected to not be kind_of?\n[<Class>, <NilClass>] but was.}) do
    assert_not_kind_of([Class, NilClass], Array)
  end
end
test_assert_not_match_fail_match() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 966
def test_assert_not_match_fail_match
  check_fail("</string/> was expected to not match\n" +
              "<\"string\">.") do
    assert_not_match(/string/, "string")
  end
end
test_assert_not_match_fail_match_with_message() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 973
def test_assert_not_match_fail_match_with_message
  check_fail("message.\n" +
              "</string/> was expected to not match\n" +
              "<\"string\">.") do
    assert_not_match(/string/, "string", "message")
  end
end
test_assert_not_match_fail_not_regexp() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 956
def test_assert_not_match_fail_not_regexp
  check_fail("<REGEXP> in assert_not_match(<REGEXP>, ...) " +
              "should be a Regexp.\n" +
              "<\"asdf\"> was expected to be instance_of?\n" +
              "<Regexp> but was\n" +
              "<String>.") do
    assert_not_match("asdf", "asdf")
  end
end
test_assert_not_match_pass() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 944
def test_assert_not_match_pass
  check_nothing_fails do
    assert_not_match(/sling/, "string")
  end
end
test_assert_not_match_pass_with_message() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 950
def test_assert_not_match_pass_with_message
  check_nothing_fails do
    assert_not_match(/sling/, "string", "message")
  end
end
test_assert_not_nil() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 732
def test_assert_not_nil
  check_nothing_fails{assert_not_nil(false)}
  check_nothing_fails{assert_not_nil(false, "message")}
  check_fail("<nil> was expected to not be nil."){assert_not_nil(nil)}
  check_fail("message.\n<nil> was expected to not be nil.") {assert_not_nil(nil, "message")}
end
test_assert_not_operator() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1047
def test_assert_not_operator
  check_nothing_fails {
    assert_not_operator("thing", :==, "Thing", "message")
  }
  check_fail(%Q{<42>\ngiven as the operator for #assert_not_operator must be a Symbol or #respond_to?(:to_str).}) do
    assert_not_operator("thing", 42, "message")
  end
  check_fail(%Q{message.\n<0> was expected to not be\n==\n<0.0>.}) {
    assert_not_operator(0, :==, 0.0, "message")
  }
end
test_assert_not_predicate() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1366
def test_assert_not_predicate
  check_nothing_fails do
    assert_not_predicate([1], :empty?)
  end

  check_fail("<[]>.empty? is false value expected but was\n<true>") do
    assert_not_predicate([], :empty?)
  end

  check_fail("<[]>.respond_to?(:nonexistent?) expected\n" +
              "(Class: <Array>)") do
    assert_not_predicate([], :nonexistent?)
  end
end
test_assert_not_respond_to_fail_number() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1089
def test_assert_not_respond_to_fail_number
  check_fail("<0.15>.kind_of?(Symbol) or\n" +
              "<0.15>.respond_to?(:to_str) expected") do
    assert_respond_to("thing", 0.15)
  end
end
test_assert_not_respond_to_pass_string() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1083
def test_assert_not_respond_to_pass_string
  check_nothing_fails do
    assert_not_respond_to("thing", :nonexistent, "message")
  end
end
test_assert_not_respond_to_pass_symbol() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1077
def test_assert_not_respond_to_pass_symbol
  check_nothing_fails do
    assert_not_respond_to("thing", :nonexistent, "message")
  end
end
test_assert_not_same() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 912
def test_assert_not_same
  thing = "thing"
  thing2 = thing.dup
  check_nothing_fails {
    assert_not_same(thing, thing2)
  }
  check_nothing_fails {
    assert_not_same(thing, thing2, "message")
  }
  check_fail(%Q{<"thing">\nwith id <#{thing.__id__}> was expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
    assert_not_same(thing, thing)
  }
  check_fail(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> was expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
    assert_not_same(thing, thing, "message")
  }
end
test_assert_nothing_raised() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 836
      def test_assert_nothing_raised
        check_nothing_fails(:dont_care) {
          assert_nothing_raised {
            1 + 1
          }
        }
        check_nothing_fails(:dont_care) {
          assert_nothing_raised("successful assert_nothing_raised") {
            1 + 1
          }
        }
        check_nothing_fails(:dont_care) {
          assert_nothing_raised("successful assert_nothing_raised") {
            1 + 1
          }
        }
        check_nothing_fails(:dont_care) {
          begin
            assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
              raise ZeroDivisionError.new("ArgumentError")
            }
          rescue ZeroDivisionError
          end
        }
        expected_message =
          "<Object> must be a subclass of Exception, " +
          "an object of Exception subclasses or a Module."
        check_fail(expected_message) {
          assert_nothing_raised(Object) {
            1 + 1
          }
        }
        expected_message = <<-EOM.chomp
Exception raised:
RuntimeError(<Error>)
EOM
        check_fail_exception(expected_message) {
          assert_nothing_raised {
            raise "Error"
          }
        }
        expected_message = <<-EOM.chomp
failed assert_nothing_raised.
Exception raised:
RuntimeError(<Error>)
EOM
        check_fail_exception(expected_message) {
          assert_nothing_raised("failed assert_nothing_raised") {
            raise "Error"
          }
        }
        expected_message = <<-EOM.chomp
Exception raised:
RuntimeError(<Error>)
EOM
        check_fail_exception(expected_message) {
          assert_nothing_raised(StandardError, RuntimeError) {
            raise "Error"
          }
        }
        check_fail("Failure.") do
          assert_nothing_raised do
            flunk("Failure")
          end
        end
      end
test_assert_nothing_thrown() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1018
def test_assert_nothing_thrown
  check_nothing_fails do
    assert_nothing_thrown("message") do
      1 + 1
    end
  end

  tag = :thing
  inspected = inspect_tag(tag)
  check_fail("message.\n" +
              "<#{inspected}> was thrown when nothing was expected.") do
    assert_nothing_thrown("message") do
      throw tag
    end
  end
end
test_assert_operator() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1035
def test_assert_operator
  check_nothing_fails {
    assert_operator("thing", :==, "thing", "message")
  }
  check_fail(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do
    assert_operator("thing", 0.15, "thing")
  end
  check_fail(%Q{message.\n<"thing1"> was expected to be\n==\n<"thing2">.}) {
    assert_operator("thing1", :==, "thing2", "message")
  }
end
test_assert_path_exist() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1417
def test_assert_path_exist
  check_nothing_fails do
    assert_path_exist(__FILE__)
  end

  nonexistent_file = __FILE__ + ".nonexistent"
  check_fail("<#{nonexistent_file.inspect}> was expected to exist") do
    assert_path_exist(nonexistent_file)
  end
end
test_assert_path_not_exist() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1428
def test_assert_path_not_exist
  nonexistent_file = __FILE__ + ".nonexistent"
  check_nothing_fails do
    assert_path_not_exist(nonexistent_file)
  end

  check_fail("<#{__FILE__.inspect}> was expected to not exist") do
    assert_path_not_exist(__FILE__)
  end
end
test_assert_predicate() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1351
def test_assert_predicate
  check_nothing_fails do
    assert_predicate([], :empty?)
  end

  check_fail("<[1]>.empty? is true value expected but was\n<false>") do
    assert_predicate([1], :empty?)
  end

  check_fail("<[1]>.respond_to?(:nonexistent?) expected\n" +
              "(Class: <Array>)") do
    assert_predicate([1], :nonexistent?)
  end
end
test_assert_raise_fail() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 502
      def test_assert_raise_fail
        check_fail("<RuntimeError> exception was expected but none was thrown.") do
          assert_raise(RuntimeError) do
            1 + 1
          end
        end

        message = <<-EOM.chomp
failed assert_raise.
<ArgumentError> exception expected but was
<RuntimeError(<Error>)>.
EOM
        check_fail_exception(message) do
          assert_raise(ArgumentError, "failed assert_raise") do
            raise "Error"
          end
        end

        message = <<-EOM
<Object> must be a subclass of Exception, an object of Exception subclasses or a Module.
EOM
        check_fail(message.chomp) do
          assert_nothing_raised(Object) do
            1 + 1
          end
        end
      end
test_assert_raise_instance() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 582
      def test_assert_raise_instance
        return_value = nil
        check_nothing_fails(true) do
          return_value = assert_raise(RuntimeError.new("Error")) do
            raise "Error"
          end
        end
        check(return_value.kind_of?(Exception),
              "Should have returned the exception " +
              "from a successful assert_raise")
        check(return_value.message == "Error",
              "Should have returned the correct exception " +
              "from a successful assert_raise")

        message = <<-EOM.chomp
<RuntimeError(<XXX>)> exception expected but was
<RuntimeError(<Error>)>.

diff:
- RuntimeError(<XXX>)
?               ^^^
+ RuntimeError(<Error>)
?               ^^^^^
EOM
        check_fail_exception(message) do
          return_value = assert_raise(RuntimeError.new("XXX")) do
            raise "Error"
          end
        end

        different_error_class = Class.new(StandardError)
        message = <<-EOM.chomp
<#{different_error_class.inspect}(<Error>)> exception expected but was
<RuntimeError(<Error>)>.
EOM
        check_fail_exception(message) do
          assert_raise(different_error_class.new("Error")) do
            raise "Error"
          end
        end

        different_error = different_error_class.new("Error")
        def different_error.inspect
          "DifferentError: \"Error\""
        end
        message = <<-EOM.chomp
<DifferentError: "Error"> exception expected but was
<RuntimeError(<Error>)>.
EOM
        check_fail_exception(message) do
          assert_raise(different_error) do
            raise "Error"
          end
        end

        check_nothing_fails(true) do
          assert_raise(different_error_class.new("Error"),
                       RuntimeError.new("Error"),
                       RuntimeError.new("XXX")) do
            raise "Error"
          end
        end
      end
test_assert_raise_jruby() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 646
def test_assert_raise_jruby
  omit("For JRuby") unless Object.const_defined?(:Java)

  exception = Java::JavaLang::StringIndexOutOfBoundsException

  return_value = nil
  check_nothing_fails(true) do
    return_value = assert_raise(exception) do
      Java::JavaLang::String.new("abc").char_at(4)
    end
  end
  check(return_value.instance_of?(exception),
        "Should have returned #{exception} but was #{return_value.class}")
end
test_assert_raise_kind_of() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1305
      def test_assert_raise_kind_of
        check_nothing_fails(true) do
          assert_raise_kind_of(SystemCallError) do
            raise Errno::EACCES
          end
        end

        expected_message = <<-EOM.chomp
<SystemCallError> family exception expected but was
<RuntimeError(<XXX>)>.
EOM
        check_fail_exception(expected_message) do
          assert_raise_kind_of(SystemCallError) do
            raise RuntimeError, "XXX"
          end
        end
      end
test_assert_raise_message() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1267
      def test_assert_raise_message
        check_nothing_fails do
          assert_raise_message("Raise!") do
            raise "Raise!"
          end
        end

        check_nothing_fails do
          assert_raise_message("Raise!") do
            raise Exception, "Raise!"
          end
        end

        check_nothing_fails do
          assert_raise_message(/raise/i) do
            raise "Raise!"
          end
        end

        expected_message = <<-EOM
<"Expected message"> exception message expected but was
<"Actual message">.
EOM
        check_fail(expected_message.chomp) do
          assert_raise_message("Expected message") do
            raise "Actual message"
          end
        end

        expected_message = <<-EOM
<"Expected message"> exception message was expected but none was thrown.
EOM
        check_fail(expected_message.chomp) do
          assert_raise_message("Expected message") do
          end
        end
      end
test_assert_raise_module() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 530
      def test_assert_raise_module
        exceptions = [ArgumentError, TypeError]
        modules = [Math, Comparable]
        rescues = exceptions + modules

        exceptions.each do |exc|
          return_value = nil
          check_nothing_fails(true) do
            return_value = assert_raise(*rescues) do
              raise exc, "Error"
            end
          end
          check(return_value.instance_of?(exc),
                "Should have returned #{exc} but was #{return_value.class}")
          check(return_value.message == "Error",
                "Should have returned the correct exception " +
                "from a successful assert_raise")
        end

        modules.each do |mod|
          return_value = nil
          check_nothing_fails(true) do
            return_value = assert_raise(*rescues) do
              raise Exception.new("Error").extend(mod)
            end
          end
          check(mod === return_value,
                "Should have returned #{mod}")
          check(return_value.message == "Error",
                "Should have returned the correct exception " +
                "from a successful assert_raise")
        end

        check_fail("<[ArgumentError, TypeError, Math, Comparable]> exception " +
                    "was expected but none was thrown.") do
          assert_raise(*rescues) do
            1 + 1
          end
        end

        message = <<-EOM.chomp
failed assert_raise.
<[ArgumentError, TypeError]> exception expected but was
<RuntimeError(<Error>)>.
EOM
        check_fail_exception(message) do
          assert_raise(ArgumentError, TypeError, "failed assert_raise") do
            raise "Error"
          end
        end
      end
test_assert_raise_success() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 463
def test_assert_raise_success
  return_value = nil
  check_nothing_fails(true) do
    return_value = assert_raise(RuntimeError) do
      raise "Error"
    end
  end
  check(return_value.kind_of?(Exception),
        "Should have returned the exception " +
        "from a successful assert_raise")
  check(return_value.message == "Error",
        "Should have returned the correct exception " +
        "from a successful assert_raise")

  check_nothing_fails(true) do
    assert_raise(ArgumentError, "successful assert_raise") do
      raise ArgumentError.new("Error")
    end
  end

  check_nothing_fails(true) do
    assert_raise(RuntimeError) do
      raise "Error"
    end
  end

  check_nothing_fails(true) do
    assert_raise(RuntimeError, "successful assert_raise") do
      raise "Error"
    end
  end

  check_nothing_fails(true) do
    assert_raise do
      raise Exception, "Any exception"
    end
  end
end
test_assert_respond_to() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1059
def test_assert_respond_to
  check_nothing_fails {
    assert_respond_to("thing", :to_s, "message")
  }
  check_nothing_fails {
    assert_respond_to("thing", "to_s", "message")
  }
  check_fail("<0.15>.kind_of?(Symbol) or\n" +
              "<0.15>.respond_to?(:to_str) expected") {
    assert_respond_to("thing", 0.15)
  }
  check_fail("message.\n" +
              "<:symbol>.respond_to?(:nonexistence) expected\n" +
              "(Class: <Symbol>)") {
    assert_respond_to(:symbol, :nonexistence, "message")
  }
end
test_assert_same() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 816
def test_assert_same
  thing = "thing"
  check_nothing_fails {
    assert_same(thing, thing)
  }
  check_nothing_fails {
    assert_same(thing, thing, "successful assert_same")
  }
  check_nothing_fails {
    assert_same(thing, thing, "successful assert_same")
  }
  thing2 = thing.dup
  check_fail(%Q{<"thing">\nwith id <#{thing.__id__}> was expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
    assert_same(thing, thing2)
  }
  check_fail(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> was expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
    assert_same(thing, thing2, "failed assert_same")
  }
end
test_assert_send() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1104
      def test_assert_send
        object = Object.new
        class << object
          private
          def return_argument(argument, bogus)
            return argument
          end
        end
        check_nothing_fails do
          assert_send([object, :return_argument, true, "bogus"], "message")
        end

        inspected_object = AssertionMessage.convert(object)
        expected_message = <<-EOM
message.
<#{inspected_object}> was expected to respond to
<return_argument(*[false, "bogus"])> with a true value but was
<false>.
EOM
        check_fail(expected_message.chomp) do
          assert_send([object, :return_argument, false, "bogus"], "message")
        end
      end
test_assert_throw() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 995
def test_assert_throw
  check_nothing_fails do
    assert_throw(:thing, "message") do
      throw :thing
    end
  end

  tag = :thing2
  check_fail("message.\n" +
              "<:thing> was expected to be thrown but\n" +
              "<#{inspect_tag(tag)}> was thrown.") do
    assert_throw(:thing, "message") do
      throw :thing2
    end
  end
  check_fail("message.\n" +
              "<:thing> should have been thrown.") do
    assert_throw(:thing, "message") do
      1 + 1
    end
  end
end
test_assert_true() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1163
def test_assert_true
  check_nothing_fails do
    assert_true(true)
  end

  check_fail("<true> expected but was\n<false>") do
    assert_true(false)
  end

  check_fail("<true> expected but was\n<1>") do
    assert_true(1)
  end

  exception = check_fail("message.\n<true> expected but was\n<nil>") do
    assert_true(nil, "message")
  end
  assert_equal("message", exception.user_message)
end
test_condition_invariant() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1128
def test_condition_invariant
  object = Object.new
  def object.inspect
    @changed = true
  end
  def object.==(other)
    @changed ||= false
    return (!@changed)
  end
  check_nothing_fails do
    assert_equal(object, object, "message")
  end
end
test_flunk() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 903
def test_flunk
  check_fail("Flunked.") {
    flunk
  }
  check_fail("flunk message.") {
    flunk("flunk message")
  }
end
tset_assert_not_respond_to_fail_existence() click to toggle source
# File test-unit-3.3.4/test/test-assertions.rb, line 1096
def tset_assert_not_respond_to_fail_existence
  check_fail("message.\n" +
              "!<:symbol>.respond_to?(:to_s) expected\n" +
              "(Class: <Symbol>)") do
    assert_respond_to(:symbol, :to_s, "message")
  end
end