diff options
Diffstat (limited to 'test/ruby/test_exception.rb')
| -rw-r--r-- | test/ruby/test_exception.rb | 133 |
1 files changed, 67 insertions, 66 deletions
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index c5f7a94a77..31e5aa9f6b 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -416,7 +416,7 @@ class TestException < Test::Unit::TestCase assert_in_out_err([], "$@ = 1", [], /\$! not set \(ArgumentError\)$/) - assert_in_out_err([], <<-INPUT, [], /backtrace must be Array of String \(TypeError\)$/) + assert_in_out_err([], <<-INPUT, [], /backtrace must be an Array of String or an Array of Thread::Backtrace::Location \(TypeError\)$/) begin raise rescue @@ -508,6 +508,16 @@ end.join assert_raise(TypeError) { e.set_backtrace(1) } assert_raise(TypeError) { e.set_backtrace([1]) } + + error = assert_raise(TypeError) do + e.set_backtrace(caller_locations(1, 1) + ["foo"]) + end + assert_include error.message, "backtrace must be an Array of String or an Array of Thread::Backtrace::Location" + + error = assert_raise(TypeError) do + e.set_backtrace(["foo"] + caller_locations(1, 1)) + end + assert_include error.message, "backtrace must be an Array of String or an Array of Thread::Backtrace::Location" end def test_exit_success_p @@ -982,7 +992,7 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| assert_equal 1, outs.size assert_equal 0, errs.size err = outs.first.force_encoding('utf-8') - assert err.valid_encoding?, 'must be valid encoding' + assert_predicate err, :valid_encoding? assert_match %r/\u3042/, err end end @@ -1054,8 +1064,7 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| def capture_warning_warn(category: false) verbose = $VERBOSE - deprecated = Warning[:deprecated] - experimental = Warning[:experimental] + categories = Warning.categories.to_h {|cat| [cat, Warning[cat]]} warning = [] ::Warning.class_eval do @@ -1074,15 +1083,13 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| end $VERBOSE = true - Warning[:deprecated] = true - Warning[:experimental] = true + Warning.categories.each {|cat| Warning[cat] = true} yield return warning ensure $VERBOSE = verbose - Warning[:deprecated] = deprecated - Warning[:experimental] = experimental + categories.each {|cat, flag| Warning[cat] = flag} ::Warning.class_eval do remove_method :warn @@ -1101,19 +1108,13 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| end def test_warn_deprecated_backwards_compatibility_category - omit "no method to test" - - warning = capture_warning_warn { } - - assert_match(/deprecated/, warning[0]) - end - - def test_warn_deprecated_category - omit "no method to test" - - warning = capture_warning_warn(category: true) { } + (message, category), = capture_warning_warn(category: true) do + $; = "www" + $; = nil + end - assert_equal :deprecated, warning[0][1] + assert_include message, 'deprecated' + assert_equal :deprecated, category end def test_kernel_warn_uplevel @@ -1127,11 +1128,11 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| assert_raise(ArgumentError) {warn("test warning", uplevel: -1)} assert_in_out_err(["-e", "warn 'ok', uplevel: 1"], '', [], /warning:/) warning = capture_warning_warn {warn("test warning", {uplevel: 0})} - assert_match(/test warning.*{:uplevel=>0}/m, warning[0]) + assert_match(/test warning.*{uplevel: 0}/m, warning[0]) warning = capture_warning_warn {warn("test warning", **{uplevel: 0})} assert_equal("#{__FILE__}:#{__LINE__-1}: warning: test warning\n", warning[0]) warning = capture_warning_warn {warn("test warning", {uplevel: 0}, **{})} - assert_equal("test warning\n{:uplevel=>0}\n", warning[0]) + assert_equal("test warning\n{uplevel: 0}\n", warning[0]) assert_raise(ArgumentError) {warn("test warning", foo: 1)} end @@ -1185,48 +1186,24 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| def test_warning_category assert_raise(TypeError) {Warning[nil]} assert_raise(ArgumentError) {Warning[:XXXX]} - assert_include([true, false], Warning[:deprecated]) - assert_include([true, false], Warning[:experimental]) - end - def test_warning_category_deprecated - warning = EnvUtil.verbose_warning do - deprecated = Warning[:deprecated] - Warning[:deprecated] = true - Warning.warn "deprecated feature", category: :deprecated - ensure - Warning[:deprecated] = deprecated - end - assert_equal "deprecated feature", warning + all_assertions_foreach("categories", *Warning.categories) do |cat| + value = Warning[cat] + assert_include([true, false], value) - warning = EnvUtil.verbose_warning do - deprecated = Warning[:deprecated] - Warning[:deprecated] = false - Warning.warn "deprecated feature", category: :deprecated - ensure - Warning[:deprecated] = deprecated - end - assert_empty warning - end - - def test_warning_category_experimental - warning = EnvUtil.verbose_warning do - experimental = Warning[:experimental] - Warning[:experimental] = true - Warning.warn "experimental feature", category: :experimental - ensure - Warning[:experimental] = experimental - end - assert_equal "experimental feature", warning - - warning = EnvUtil.verbose_warning do - experimental = Warning[:experimental] - Warning[:experimental] = false - Warning.warn "experimental feature", category: :experimental + enabled = EnvUtil.verbose_warning do + Warning[cat] = true + Warning.warn "#{cat} feature", category: cat + end + disabled = EnvUtil.verbose_warning do + Warning[cat] = false + Warning.warn "#{cat} feature", category: cat + end ensure - Warning[:experimental] = experimental + Warning[cat] = value + assert_equal "#{cat} feature", enabled + assert_empty disabled end - assert_empty warning end def test_undef_Warning_warn @@ -1421,11 +1398,7 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| end def test_marshal_circular_cause - begin - raise RuntimeError, "err", [], cause: Exception.new - rescue => e - end - dump = Marshal.dump(e).sub(/o:\x0EException\x08;.0;.0;.0/, "@\x05") + dump = "\x04\bo:\x11RuntimeError\b:\tmesgI\"\berr\x06:\x06ET:\abt[\x00:\ncause@\x05" assert_raise_with_message(ArgumentError, /circular cause/, ->{dump.inspect}) do e = Marshal.load(dump) assert_same(e, e.cause) @@ -1468,6 +1441,7 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| end def test_detailed_message_under_gc_compact_stress + omit "compaction doesn't work well on s390x" if RUBY_PLATFORM =~ /s390x/ # https://github.com/ruby/ruby/pull/5077 EnvUtil.under_gc_compact_stress do e = RuntimeError.new("foo\nbar\nbaz") assert_equal("foo (RuntimeError)\nbar\nbaz", e.detailed_message) @@ -1524,7 +1498,7 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| assert_not_empty(stderr.grep(pattern)) error, = stderr.grep(/unexpected end-of-input/) assert_not_nil(error) - assert_match(/<.*unexpected end-of-input.*>/, error) + assert_match(/<.*unexpected end-of-input.*>|\^ unexpected end-of-input,/, error) end end end @@ -1551,4 +1525,31 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| assert_in_out_err(%W[-r#{lib} #{main}], "", [], [:*, "\n""path=#{main}\n", :*]) end end + + class Ex; end + + def test_exception_message_for_unexpected_implicit_conversion_type + a = Ex.new + def self.x(a) = nil + + assert_raise_with_message(TypeError, "no implicit conversion of TestException::Ex into Hash") do + x(**a) + end + assert_raise_with_message(TypeError, "no implicit conversion of TestException::Ex into Proc") do + x(&a) + end + + def a.to_a = 1 + def a.to_hash = 1 + def a.to_proc = 1 + assert_raise_with_message(TypeError, "can't convert TestException::Ex to Array (TestException::Ex#to_a gives Integer)") do + x(*a) + end + assert_raise_with_message(TypeError, "can't convert TestException::Ex to Hash (TestException::Ex#to_hash gives Integer)") do + x(**a) + end + assert_raise_with_message(TypeError, "can't convert TestException::Ex to Proc (TestException::Ex#to_proc gives Integer)") do + x(&a) + end + end end |
