Skip to content

Commit e3cd39b

Browse files
committed
Move Data to rely less on shared examples
1 parent 8161b84 commit e3cd39b

3 files changed

Lines changed: 63 additions & 66 deletions

File tree

core/data/inspect_spec.rb

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,62 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/inspect'
32

43
describe "Data#inspect" do
5-
it_behaves_like :data_inspect, :inspect
4+
it "returns a string representation showing members and values" do
5+
a = DataSpecs::Measure.new(42, "km")
6+
a.inspect.should == '#<data DataSpecs::Measure amount=42, unit="km">'
7+
end
8+
9+
it "returns a string representation without the class name for anonymous structs" do
10+
Data.define(:a).new("").inspect.should == '#<data a="">'
11+
end
12+
13+
it "returns a string representation without the class name for structs nested in anonymous classes" do
14+
c = Class.new
15+
c.class_eval <<~DOC
16+
Foo = Data.define(:a)
17+
DOC
18+
19+
c::Foo.new("").inspect.should == '#<data a="">'
20+
end
21+
22+
it "returns a string representation without the class name for structs nested in anonymous modules" do
23+
m = Module.new
24+
m.class_eval <<~DOC
25+
Foo = Data.define(:a)
26+
DOC
27+
28+
m::Foo.new("").inspect.should == '#<data a="">'
29+
end
30+
31+
it "does not call #name method" do
32+
struct = DataSpecs::MeasureWithOverriddenName.new(42, "km")
33+
struct.inspect.should == '#<data DataSpecs::MeasureWithOverriddenName amount=42, unit="km">'
34+
end
35+
36+
it "does not call #name method when struct is anonymous" do
37+
klass = Class.new(DataSpecs::Measure) do
38+
def self.name
39+
"A"
40+
end
41+
end
42+
struct = klass.new(42, "km")
43+
struct.inspect.should == '#<data amount=42, unit="km">'
44+
end
45+
46+
context "recursive structure" do
47+
it "returns string representation with recursive attribute replaced with ..." do
48+
a = DataSpecs::Measure.allocate
49+
a.send(:initialize, amount: 42, unit: a)
50+
51+
a.inspect.should == "#<data DataSpecs::Measure amount=42, unit=#<data DataSpecs::Measure:...>>"
52+
end
53+
54+
it "returns string representation with recursive attribute replaced with ... when an anonymous class" do
55+
klass = Class.new(DataSpecs::Measure)
56+
a = klass.allocate
57+
a.send(:initialize, amount: 42, unit: a)
58+
59+
a.inspect.should =~ /#<data amount=42, unit=#<data #<Class:0x.+?>:\.\.\.>>/
60+
end
61+
end
662
end

core/data/shared/inspect.rb

Lines changed: 0 additions & 62 deletions
This file was deleted.

core/data/to_s_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/inspect'
2+
require_relative 'fixtures/classes'
33

44
describe "Data#to_s" do
5-
it_behaves_like :data_inspect, :to_s
5+
it "is an alias of Data#inspect" do
6+
a = DataSpecs::Measure.new(42, "km")
7+
a.method(:to_s).should == a.method(:inspect)
8+
end
69
end

0 commit comments

Comments
 (0)