Skip to content

Commit

Permalink
Merge pull request #1828 from tsion/argumenterror-message
Browse files Browse the repository at this point in the history
Fix #inspect, #to_s, and #message for ArgumentError
  • Loading branch information
jfirebaugh committed Jul 26, 2012
2 parents 31cc036 + 4b38891 commit 88107f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 8 additions & 5 deletions kernel/common/exception.rb
Expand Up @@ -168,12 +168,15 @@ class ZeroDivisionError < StandardError
end

class ArgumentError < StandardError
def message
return @reason_message if @reason_message
if @method_name
"method '#{@method_name}': given #{@given}, expected #{@expected}"
def to_s
if @given and @expected
if @method_name
"method '#{@method_name}': given #{@given}, expected #{@expected}"
else
"given #{@given}, expected #{@expected}"
end
else
"given #{@given}, expected #{@expected}"
super
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/core/exception/arguments_spec.rb
@@ -0,0 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe "ArgumentError" do
it "uses its internal message in #inspect" do
lambda { 1.+(2, 3) }.should raise_error(ArgumentError) do |e|
e.inspect.should == "#<ArgumentError: method '+': given 2, expected 1>"
end
end
end
4 changes: 4 additions & 0 deletions spec/ruby/core/exception/arguments_spec.rb
Expand Up @@ -4,4 +4,8 @@
it "is a subclass of StandardError" do
StandardError.should be_ancestor_of(ArgumentError)
end

it "gives its own class name as message if it has no message" do
ArgumentError.new.message.should == "ArgumentError"
end
end

0 comments on commit 88107f2

Please sign in to comment.