Skip to content

Commit

Permalink
Fix Data.new to accept let positional parameters than needed.
Browse files Browse the repository at this point in the history
Add missing test [Fixes #187]
  • Loading branch information
marcandre committed Apr 5, 2023
1 parent ea97f3e commit 498910c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
9 changes: 2 additions & 7 deletions lib/backports/3.2.0/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,10 @@ def self.members # def self.members
class << klass
def new(*values, **named_values)
if named_values.empty?
case values.size <=> members.size
when -1
missing = members[values.size..-1].map(:inspect).join(", ")
raise ArgumentError, "Missing keywords: #{missing}"
when +1
if values.size > members.size
raise ArgumentError, "wrong number of arguments (given #{values.size}, expected 0..#{members.size})"
when 0
super(**members.zip(values).to_h)
end
super(**members.first(values.size).zip(values).to_h)
else
unless values.empty?
raise ArgumentError, "wrong number of arguments (given #{values.size}, expected 0)"
Expand Down
1 change: 1 addition & 0 deletions test/data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def initialize(*args, **kwargs)

# Missing arguments can be fixed in initialize
assert_equal([[], {foo: 1}], klass.new(foo: 1).passed)
assert_equal([[], {foo: 42}], klass.new(42).passed)

# Extra keyword arguments can be dropped in initialize
assert_equal([[], {foo: 1, bar: 2, baz: 3}], klass.new(foo: 1, bar: 2, baz: 3).passed)
Expand Down

0 comments on commit 498910c

Please sign in to comment.