Skip to content

Commit 369466f

Browse files
committed
[GR-18163] Remove Struct.members method in explicitly inherited subclasses
PullRequest: truffleruby/4488
2 parents b3a2cec + 8491afc commit 369466f

File tree

16 files changed

+174
-9
lines changed

16 files changed

+174
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Compatibility:
1212
* Support String patterns in `StringScanner#{exist?,scan_until,skip_until,check_until,search_full}` methods (@andrykonchin).
1313
* Implement `ObjectSpace::WeakKeyMap` (#3681, @andrykonchin).
1414
* Fix `String#slice` called with negative offset (@andrykonchin).
15+
* Fix explicitly inherited `Struct` subclasses and don't provide `#members` method (#3802, @andrykonchin).
1516

1617
Performance:
1718

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module DataSpecs
22
guard -> { ruby_version_is "3.2" and Data.respond_to?(:define) } do
33
Measure = Data.define(:amount, :unit)
4+
5+
class DataSubclass < Data; end
46
end
57
end

spec/ruby/core/data/members_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
3+
4+
ruby_version_is "3.2" do
5+
describe "Data#members" do
6+
it "returns an array of attribute names" do
7+
measure = DataSpecs::Measure.new(amount: 42, unit: 'km')
8+
measure.members.should == [:amount, :unit]
9+
end
10+
end
11+
12+
describe "DataClass#members" do
13+
it "returns an array of attribute names" do
14+
DataSpecs::Measure.members.should == [:amount, :unit]
15+
end
16+
17+
context "class inheriting Data" do
18+
it "isn't available in a subclass" do
19+
DataSpecs::DataSubclass.should_not.respond_to?(:members)
20+
end
21+
end
22+
end
23+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe "Process::Tms#cstime" do
4+
it "returns cstime attribute" do
5+
cstime = Object.new
6+
Process::Tms.new(nil, nil, nil, cstime).cstime.should == cstime
7+
end
8+
end
9+
10+
describe "Process::Tms#cstime=" do
11+
it "assigns a value to the cstime attribute" do
12+
cstime = Object.new
13+
tms = Process::Tms.new
14+
tms.cstime = cstime
15+
tms.cstime.should == cstime
16+
end
17+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe "Process::Tms#cutime" do
4+
it "returns cutime attribute" do
5+
cutime = Object.new
6+
Process::Tms.new(nil, nil, cutime, nil).cutime.should == cutime
7+
end
8+
end
9+
10+
describe "Process::Tms#cutime=" do
11+
it "assigns a value to the cutime attribute" do
12+
cutime = Object.new
13+
tms = Process::Tms.new
14+
tms.cutime = cutime
15+
tms.cutime.should == cutime
16+
end
17+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe "Process::Tms#stime" do
4+
it "returns stime attribute" do
5+
stime = Object.new
6+
Process::Tms.new(nil, stime, nil, nil).stime.should == stime
7+
end
8+
end
9+
10+
describe "Process::Tms#stime=" do
11+
it "assigns a value to the stime attribute" do
12+
stime = Object.new
13+
tms = Process::Tms.new
14+
tms.stime = stime
15+
tms.stime.should == stime
16+
end
17+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe "Process::Tms#utime" do
4+
it "returns utime attribute" do
5+
utime = Object.new
6+
Process::Tms.new(utime, nil, nil, nil).utime.should == utime
7+
end
8+
end
9+
10+
describe "Process::Tms#utime=" do
11+
it "assigns a value to the ctime attribute" do
12+
utime = Object.new
13+
tms = Process::Tms.new
14+
tms.utime = utime
15+
tms.utime.should == utime
16+
end
17+
end

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ def initialize(*)
2929
super
3030
end
3131
end
32+
33+
class StructSubclass < Struct; end
3234
end

spec/ruby/core/struct/keyword_init_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
23

34
ruby_version_is "3.1" do
45
# See https://bugs.ruby-lang.org/issues/18008
@@ -36,5 +37,11 @@
3637
struct = Struct.new(:arg, keyword_init: {})
3738
struct.keyword_init?.should be_true
3839
end
40+
41+
context "class inheriting Struct" do
42+
it "isn't available in a subclass" do
43+
StructClasses::StructSubclass.should_not.respond_to?(:keyword_init?)
44+
end
45+
end
3946
end
4047
end

spec/ruby/core/struct/members_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@
1111

1212
it_behaves_like :struct_accessor, :members
1313
end
14+
15+
describe "StructClass#members" do
16+
it "returns an array of attribute names" do
17+
StructClasses::Car.members.should == [:make, :model, :year]
18+
end
19+
20+
context "class inheriting Struct" do
21+
it "isn't available in a subclass" do
22+
StructClasses::StructSubclass.should_not.respond_to?(:members)
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)