Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Revise assert_raise to handle any kind of Exception #1

Closed
wants to merge 2 commits into from

3 participants

@packagethief

Ruby's default rescue only handles StandardError subclasses. This is a problem when testing for exceptions that don't inherit from StandardError. The standard timeout library is one such offender: Timeout::Error inherits from Interrupt.

This is causing a test in redis-rb to fail, which is what led me here (see packagethief/redis-rb@b5dd841).

I'm not sure about packagethief@958519aaccfc4d27a574712204508571b42a732f. Prior to this change, these tests were failing due to differences in backtrace formatting (./test/fixtures/failure.rb vs. test/fixtures/failure.rb). If they weren't failing for anyone else, just ignore; it's possible that it's version specific (I tested using 1.8.7 p253).

packagethief added some commits
@packagethief packagethief Update output expectations 958519a
@packagethief packagethief Revise assert_raise to rescue any kind of Exception; the default only…
… covers StandardError and subclasses

This is a problem when testing for exceptions that don't inherit from StandardError.
Ruby's timeout library is a notable offender: Timeout::Error < Interrupt.
3abf06a
@pravi

I think I also got this error, I'm building a deb package for cutest and running its own tests fail for me.

pravi@savannah:/media/forge/debian/diaspora/ruby-cutest-1.1.3$ cutest test/run.rb 
.

Cutest::AssertionFailed: "\e[01;36m\n\nCutest::AssertionFailed: \e[1;33mexpression returned false\n\n\e[00m\e[00m- assert false\e[01;30m ./test/fixtures/failure.rb +2\n\e[00m\n" != "\e[01;36m\n\nCutest::AssertionFailed: \e[1;33mexpression returned false\n\n\e[00m- assert false\e[01;30m test/fixtures/failure.rb +2\n\e[00m\n"

- assert_equal(stdout, expected) ./test/run.rb +32
pravi@savannah:/media/forge/debian/diaspora/ruby-cutest-1.1.3$ echo $?
1
pravi@savannah:/media/forge/debian/diaspora/ruby-cutest-1.1.3$ 
@pravi

This patch doesn't apply any more! test fails with both 1.8.7 and 1.9.2 versions or ruby.

Tested with versions
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

packagethief, can you re-base your changes to current cutest revision?

@pravi

hi can anyone rebase this patch to current version?

@djanowski
Owner

My sincerest apologies for not attending to this issue before. If you're trying to fix assert_raise under a timeout condition, can you please add a test for such a case? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 22, 2010
  1. @packagethief
  2. @packagethief

    Revise assert_raise to rescue any kind of Exception; the default only…

    packagethief authored
    … covers StandardError and subclasses
    
    This is a problem when testing for exceptions that don't inherit from StandardError.
    Ruby's timeout library is a notable offender: Timeout::Error < Interrupt.
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 4 deletions.
  1. +1 −1  lib/cutest.rb
  2. +3 −3 test/run.rb
View
2  lib/cutest.rb
@@ -153,7 +153,7 @@ def assert_equal(value, other)
def assert_raise(expected = Exception)
begin
yield
- rescue => exception
+ rescue Exception => exception
ensure
flunk("got #{exception.inspect} instead") unless exception.kind_of?(expected)
success
View
6 test/run.rb
@@ -21,7 +21,7 @@ def capture
end
test "output of failed run" do
- expected = "\n\e[01;36mException: \e[01;33massert false\e[00m # assertion failed\ntest/fixtures/failure.rb +2\n\n"
+ expected = "\n\e[01;36mException: \e[01;33massert false\e[00m # assertion failed\n./test/fixtures/failure.rb +2\n\n"
stdout, stderr = capture do
Cutest.run(Dir["test/fixtures/failure.rb"])
@@ -31,7 +31,7 @@ def capture
end
test "output of failed run" do
- expected = "\n\e[01;36mException: \e[01;33mraise \"Oops\"\e[00m # Oops\ntest/fixtures/exception.rb +2\n\n"
+ expected = "\n\e[01;36mException: \e[01;33mraise \"Oops\"\e[00m # Oops\n./test/fixtures/exception.rb +2\n\n"
stdout, stderr = capture do
Cutest.run(Dir["test/fixtures/exception.rb"])
@@ -41,7 +41,7 @@ def capture
end
test "output of custom assertion" do
- expected = "\n\e[01;36mException: \e[01;33massert_empty \"foo\"\e[00m # not empty\ntest/fixtures/fail_custom_assertion.rb +7\n\n"
+ expected = "\n\e[01;36mException: \e[01;33massert_empty \"foo\"\e[00m # not empty\n./test/fixtures/fail_custom_assertion.rb +7\n\n"
stdout, stderr = capture do
Cutest.run(Dir["test/fixtures/fail_custom_assertion.rb"])
Something went wrong with that request. Please try again.