|
1 | 1 | require_relative '../../spec_helper' |
2 | | -require_relative 'shared/inspect' |
3 | 2 |
|
4 | 3 | 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 |
6 | 62 | end |
0 commit comments