|
| 1 | +require "spec_helper" |
| 2 | + |
| 3 | +if defined?(Data) |
| 4 | + RSpec.describe SuperDiff, type: :unit do |
| 5 | + describe ".inspect_object" do |
| 6 | + context "given as_lines: false" do |
| 7 | + subject(:output) do |
| 8 | + described_class.inspect_object(object, as_lines: false) |
| 9 | + end |
| 10 | + |
| 11 | + context "given an anonymous Data object" do |
| 12 | + let(:object) { Data.define(:x, :y).new(1, 2) } |
| 13 | + |
| 14 | + it "shows the data" do |
| 15 | + expect(output).to eq("#<data x: 1, y: 2>") |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + context "given a named Data object" do |
| 20 | + let(:object) { SuperDiff::Test::Point.new(1, 2) } |
| 21 | + |
| 22 | + it "shows the data" do |
| 23 | + expect(output).to eq("#<data SuperDiff::Test::Point x: 1, y: 2>") |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + context "given a Data object that defines #attributes_for_super_diff" do |
| 28 | + let(:klass) do |
| 29 | + Data.define(:x, :y) do |
| 30 | + def attributes_for_super_diff |
| 31 | + { beep: :boop } |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + let(:object) { klass.new(1, 2) } |
| 36 | + |
| 37 | + it "uses the custom attributes" do |
| 38 | + expect(output).to start_with("#<#<Class:0x").and end_with( |
| 39 | + "beep: :boop>" |
| 40 | + ) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context "given as_lines: true" do |
| 46 | + subject(:tiered_lines) do |
| 47 | + described_class.inspect_object( |
| 48 | + object, |
| 49 | + as_lines: true, |
| 50 | + type: :noop, |
| 51 | + indentation_level: 1 |
| 52 | + ) |
| 53 | + end |
| 54 | + |
| 55 | + context "given an anonymous Data object" do |
| 56 | + let(:object) { Data.define(:x, :y).new(1, 2) } |
| 57 | + |
| 58 | + it "shows the data" do |
| 59 | + expect(tiered_lines).to match( |
| 60 | + [ |
| 61 | + an_object_having_attributes( |
| 62 | + value: "#<data {", |
| 63 | + collection_bookend: :open |
| 64 | + ), |
| 65 | + an_object_having_attributes( |
| 66 | + prefix: "x: ", |
| 67 | + value: "1", |
| 68 | + add_comma: true |
| 69 | + ), |
| 70 | + an_object_having_attributes( |
| 71 | + prefix: "y: ", |
| 72 | + value: "2", |
| 73 | + add_comma: false |
| 74 | + ), |
| 75 | + an_object_having_attributes( |
| 76 | + value: "}>", |
| 77 | + collection_bookend: :close |
| 78 | + ) |
| 79 | + ] |
| 80 | + ) |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "given a named Data object" do |
| 85 | + let(:object) { SuperDiff::Test::Point.new(1, 2) } |
| 86 | + |
| 87 | + it "shows the data" do |
| 88 | + expect(tiered_lines).to match( |
| 89 | + [ |
| 90 | + an_object_having_attributes( |
| 91 | + value: "#<data SuperDiff::Test::Point {", |
| 92 | + collection_bookend: :open |
| 93 | + ), |
| 94 | + an_object_having_attributes( |
| 95 | + prefix: "x: ", |
| 96 | + value: "1", |
| 97 | + add_comma: true |
| 98 | + ), |
| 99 | + an_object_having_attributes( |
| 100 | + prefix: "y: ", |
| 101 | + value: "2", |
| 102 | + add_comma: false |
| 103 | + ), |
| 104 | + an_object_having_attributes( |
| 105 | + value: "}>", |
| 106 | + collection_bookend: :close |
| 107 | + ) |
| 108 | + ] |
| 109 | + ) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context "given a Data object that defines #attributes_for_super_diff" do |
| 114 | + let(:klass) do |
| 115 | + Data.define(:x, :y) do |
| 116 | + def attributes_for_super_diff |
| 117 | + { beep: :boop } |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | + let(:object) { klass.new(1, 2) } |
| 122 | + |
| 123 | + it "uses the custom attributes" do |
| 124 | + expect(tiered_lines).to match( |
| 125 | + [ |
| 126 | + an_object_having_attributes( |
| 127 | + value: /\A#<#<Class:0x.*> {/, |
| 128 | + collection_bookend: :open |
| 129 | + ), |
| 130 | + an_object_having_attributes( |
| 131 | + prefix: "beep: ", |
| 132 | + value: ":boop", |
| 133 | + add_comma: false |
| 134 | + ), |
| 135 | + an_object_having_attributes( |
| 136 | + value: "}>", |
| 137 | + collection_bookend: :close |
| 138 | + ) |
| 139 | + ] |
| 140 | + ) |
| 141 | + end |
| 142 | + end |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments