зеркало из https://github.com/github/ruby.git
OpenStruct: improve error message when passing wrong number of arguments.
Patch by Lisa Ugray (issue #15515) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67556 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
1119bb4794
Коммит
816c5323fe
|
@ -213,7 +213,7 @@ class OpenStruct
|
|||
len = args.length
|
||||
if mname = mid[/.*(?==\z)/m]
|
||||
if len != 1
|
||||
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
||||
raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
||||
end
|
||||
modifiable?[new_ostruct_member!(mname)] = args[0]
|
||||
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
|
||||
|
@ -221,6 +221,8 @@ class OpenStruct
|
|||
new_ostruct_member!(mid) unless frozen?
|
||||
@table[mid]
|
||||
end
|
||||
elsif @table.key?(mid)
|
||||
raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
|
||||
else
|
||||
begin
|
||||
super
|
||||
|
|
|
@ -32,10 +32,17 @@ describe "OpenStruct#method_missing when called with a method name ending in '='
|
|||
end
|
||||
|
||||
describe "OpenStruct#method_missing when passed additional arguments" do
|
||||
it "raises a NoMethodError" do
|
||||
it "raises a NoMethodError when the key does not exist" do
|
||||
os = OpenStruct.new
|
||||
lambda { os.method_missing(:test, 1, 2, 3) }.should raise_error(NoMethodError)
|
||||
end
|
||||
|
||||
ruby_version_is "2.7" do
|
||||
it "raises an ArgumentError when the key exists" do
|
||||
os = OpenStruct.new(test: 20)
|
||||
lambda { os.method_missing(:test, 1, 2, 3) }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "OpenStruct#method_missing when not passed any additional arguments" do
|
||||
|
|
Загрузка…
Ссылка в новой задаче