Correctly raise TypeError even if nil or false is given #1936

Merged
merged 2 commits into from Oct 3, 2012
View
@@ -234,7 +234,7 @@ def join_inner(timeout = undefined)
end
private :join_inner
- def raise(exc=$!, msg=nil, trace=nil)
+ def raise(exc=undefined, msg=nil, trace=nil)
Rubinius.lock(self)
unless @alive
@@ -243,11 +243,18 @@ def raise(exc=$!, msg=nil, trace=nil)
end
begin
+ if exc.equal?(undefined)
+ no_argument = true
+ exc = $!
+ end
+
if exc.respond_to? :exception
exc = exc.exception msg
Kernel.raise TypeError, 'exception class/object expected' unless Exception === exc
exc.set_backtrace trace if trace
- elsif exc.kind_of? String or !exc
+ elsif no_argument
+ exc = RuntimeError.exception nil
+ elsif exc.kind_of? String
exc = RuntimeError.exception exc
else
Kernel.raise TypeError, 'exception class/object expected'
View
@@ -18,7 +18,7 @@ def raise(exc=undefined, msg=undefined, ctx=nil)
exc = exc.exception msg
end
raise ::TypeError, 'exception class/object expected' unless exc.kind_of?(::Exception)
- elsif exc.kind_of? String or !exc
+ elsif exc.kind_of? String
exc = ::RuntimeError.exception exc
else
raise ::TypeError, 'exception class/object expected'
@@ -16,6 +16,31 @@
lambda { @object.raise }.should raise_error(RuntimeError)
end
+ it "raises a given Exception instance" do
+ error = RuntimeError.new
+ lambda { @object.raise(error) }.should raise_error(error)
+ end
+
+ it "raises a RuntimeError if string given" do
+ lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
+ end
+
+ it "raises a TypeError when passed a non-Exception object" do
+ lambda { @object.raise(Object.new) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed true" do
+ lambda { @object.raise(true) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed false" do
+ lambda { @object.raise(false) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed nil" do
+ lambda { @object.raise(nil) }.should raise_error(TypeError)
+ end
+
it "re-raises the rescued exception" do
lambda do
begin