summaryrefslogtreecommitdiff
path: root/spec/ruby/core/data
diff options
context:
space:
mode:
authorAndrew Konchin <andry.konchin@gmail.com>2025-06-02 19:34:54 +0300
committerBenoit Daloze <eregontp@gmail.com>2025-06-02 21:54:48 +0200
commitd6aa1714fed3e8b5ee8bede00a8853c338ff6092 (patch)
treeea863a44d9026f74b9a07375f401c3e5d8cb4bf5 /spec/ruby/core/data
parent685c8ca9af892f562f64b54dbee73bb9a1999b90 (diff)
Update to ruby/spec@4d2fc4d
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/13495
Diffstat (limited to 'spec/ruby/core/data')
-rw-r--r--spec/ruby/core/data/deconstruct_keys_spec.rb27
-rw-r--r--spec/ruby/core/data/fixtures/classes.rb8
-rw-r--r--spec/ruby/core/data/initialize_spec.rb11
-rw-r--r--spec/ruby/core/data/shared/inspect.rb8
4 files changed, 53 insertions, 1 deletions
diff --git a/spec/ruby/core/data/deconstruct_keys_spec.rb b/spec/ruby/core/data/deconstruct_keys_spec.rb
index 5cae4cbd68..df378f8b98 100644
--- a/spec/ruby/core/data/deconstruct_keys_spec.rb
+++ b/spec/ruby/core/data/deconstruct_keys_spec.rb
@@ -1,10 +1,11 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
-describe "Data#deconstruct" do
+describe "Data#deconstruct_keys" do
it "returns a hash of attributes" do
klass = Data.define(:x, :y)
d = klass.new(1, 2)
+
d.deconstruct_keys([:x, :y]).should == {x: 1, y: 2}
end
@@ -29,6 +30,7 @@ describe "Data#deconstruct" do
it "accepts string attribute names" do
klass = Data.define(:x, :y)
d = klass.new(1, 2)
+
d.deconstruct_keys(['x', 'y']).should == {'x' => 1, 'y' => 2}
end
@@ -58,6 +60,7 @@ describe "Data#deconstruct" do
it "returns an empty hash when there are more keys than attributes" do
klass = Data.define(:x, :y)
d = klass.new(1, 2)
+
d.deconstruct_keys([:x, :y, :x]).should == {}
end
@@ -84,6 +87,28 @@ describe "Data#deconstruct" do
d.deconstruct_keys(nil).should == {x: 1, y: 2}
end
+ it "tries to convert a key with #to_int if index is not a String nor a Symbol, but responds to #to_int" do
+ klass = Data.define(:x, :y)
+ d = klass.new(1, 2)
+
+ key = mock("to_int")
+ key.should_receive(:to_int).and_return(1)
+
+ d.deconstruct_keys([key]).should == { key => 2 }
+ end
+
+ it "raises a TypeError if the conversion with #to_int does not return an Integer" do
+ klass = Data.define(:x, :y)
+ d = klass.new(1, 2)
+
+ key = mock("to_int")
+ key.should_receive(:to_int).and_return("not an Integer")
+
+ -> {
+ d.deconstruct_keys([key])
+ }.should raise_error(TypeError, /can't convert MockObject to Integer/)
+ end
+
it "raises TypeError if index is not a String, a Symbol and not convertible to Integer " do
klass = Data.define(:x, :y)
d = klass.new(1, 2)
diff --git a/spec/ruby/core/data/fixtures/classes.rb b/spec/ruby/core/data/fixtures/classes.rb
index 5db263fa20..2d48780496 100644
--- a/spec/ruby/core/data/fixtures/classes.rb
+++ b/spec/ruby/core/data/fixtures/classes.rb
@@ -9,5 +9,13 @@ module DataSpecs
end
class DataSubclass < Data; end
+
+ MeasureSubclass = Class.new(Measure) do
+ def initialize(amount:, unit:)
+ super
+ end
+ end
+
+ Empty = Data.define()
end
end
diff --git a/spec/ruby/core/data/initialize_spec.rb b/spec/ruby/core/data/initialize_spec.rb
index 37a6c8f2dd..0f75f32f57 100644
--- a/spec/ruby/core/data/initialize_spec.rb
+++ b/spec/ruby/core/data/initialize_spec.rb
@@ -60,4 +60,15 @@ describe "Data#initialize" do
e.message.should.include?("unknown keyword: :system")
}
end
+
+ it "supports super from a subclass" do
+ ms = DataSpecs::MeasureSubclass.new(amount: 1, unit: "km")
+
+ ms.amount.should == 1
+ ms.unit.should == "km"
+ end
+
+ it "supports Data with no fields" do
+ -> { DataSpecs::Empty.new }.should_not raise_error
+ end
end
diff --git a/spec/ruby/core/data/shared/inspect.rb b/spec/ruby/core/data/shared/inspect.rb
index 7f54a46de3..6cd5664da7 100644
--- a/spec/ruby/core/data/shared/inspect.rb
+++ b/spec/ruby/core/data/shared/inspect.rb
@@ -50,5 +50,13 @@ describe :data_inspect, shared: true do
a.send(@method).should == "#<data DataSpecs::Measure amount=42, unit=#<data DataSpecs::Measure:...>>"
end
+
+ it "returns string representation with recursive attribute replaced with ... when an anonymous class" do
+ klass = Class.new(DataSpecs::Measure)
+ a = klass.allocate
+ a.send(:initialize, amount: 42, unit: a)
+
+ a.send(@method).should =~ /#<data amount=42, unit=#<data #<Class:0x.+?>:\.\.\.>>/
+ end
end
end