Skip to content

Commit 0526ca3

Browse files
committed
Add specs for Struct and Data initialize with empty kwargs
See jruby#9214 and jruby#9224.
1 parent d661885 commit 0526ca3

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

spec/ruby/core/data/fixtures/classes.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
module DataSpecs
22
if Data.respond_to?(:define)
33
Measure = Data.define(:amount, :unit)
4+
Single = Data.define(:value)
45

56
class MeasureWithOverriddenName < Measure
67
def self.name
78
"A"
89
end
910
end
1011

12+
class SingleWithOverriddenName < Single
13+
def self.name
14+
"A"
15+
end
16+
end
17+
1118
class DataSubclass < Data; end
1219

1320
MeasureSubclass = Class.new(Measure) do

spec/ruby/core/data/initialize_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
data.unit.should == "km"
3838
end
3939

40+
it "accepts positional arguments with empty keyword arguments" do
41+
data = DataSpecs::Single.new(42, **{})
42+
43+
data.value.should == 42
44+
45+
data = DataSpecs::Measure.new(42, "km", **{})
46+
47+
data.amount.should == 42
48+
data.unit.should == "km"
49+
end
50+
4051
it "raises ArgumentError if no arguments are given" do
4152
-> {
4253
DataSpecs::Measure.new
@@ -111,6 +122,17 @@ def initialize(*, **)
111122
ScratchPad.recorded.should == [:initialize, [], {amount: 42, unit: "m"}]
112123
end
113124

125+
it "accepts positional arguments with empty keyword arguments" do
126+
data = DataSpecs::SingleWithOverriddenName.new(42, **{})
127+
128+
data.value.should == 42
129+
130+
data = DataSpecs::MeasureWithOverriddenName.new(42, "km", **{})
131+
132+
data.amount.should == 42
133+
data.unit.should == "km"
134+
end
135+
114136
# See https://github.com/ruby/psych/pull/765
115137
it "can be deserialized by calling Data.instance_method(:initialize)" do
116138
d1 = DataSpecs::Area.new(width: 2, height: 3)

spec/ruby/core/struct/fixtures/classes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module StructClasses
33
class Apple < Struct; end
44

55
Ruby = Struct.new(:version, :platform)
6+
Single = Struct.new(:value)
67

78
Car = Struct.new(:make, :model, :year)
89

spec/ruby/core/struct/initialize_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@
4848
positional_args.version.should == keyword_args.version
4949
positional_args.platform.should == keyword_args.platform
5050
end
51+
52+
it "accepts positional arguments with empty keyword arguments" do
53+
data = StructClasses::Single.new(42, **{})
54+
55+
data.value.should == 42
56+
57+
data = StructClasses::Ruby.new("3.2", "OS", **{})
58+
59+
data.version.should == "3.2"
60+
data.platform.should == "OS"
61+
end
5162
end

0 commit comments

Comments
 (0)