Skip to content

Commit 8f47e11

Browse files
committed
Add test for diffing complex hashes
1 parent 39bb97f commit 8f47e11

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

lib/super_diff/helpers.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,21 @@ def self.inspect_object(value_to_inspect, single_line: true)
3333
end
3434
end
3535

36-
["{", contents.join(", "), "}"].join(" ")
36+
if single_line
37+
["{", contents.join(", "), "}"].join(" ")
38+
else
39+
ValueInspection.new(
40+
beginning: "{",
41+
middle: contents.map.with_index do |line, index|
42+
if index < contents.size - 1
43+
line + ","
44+
else
45+
line
46+
end
47+
end,
48+
end: "}"
49+
)
50+
end
3751
when String
3852
string = value_to_inspect
3953
newline = "⏎"

spec/unit/equality_matcher_spec.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,95 @@
11041104
end
11051105
end
11061106

1107+
context "given two hashes which contain all different kinds of values, some which differ" do
1108+
it "returns a message along with the diff" do
1109+
actual_output = described_class.call(
1110+
expected: {
1111+
customer: {
1112+
name: "Marty McFly",
1113+
shipping_address: {
1114+
line_1: "123 Main St.",
1115+
city: "Hill Valley",
1116+
state: "CA",
1117+
zip: "90382"
1118+
}
1119+
},
1120+
items: [
1121+
{
1122+
name: "Fender Stratocaster",
1123+
cost: 100_000,
1124+
options: ["red", "blue", "green"]
1125+
},
1126+
{ name: "Chevy 4x4" }
1127+
]
1128+
},
1129+
actual: {
1130+
customer: {
1131+
name: "Marty McFly, Jr.",
1132+
shipping_address: {
1133+
line_1: "456 Ponderosa Ct.",
1134+
city: "Hill Valley",
1135+
state: "CA",
1136+
zip: "90382"
1137+
}
1138+
},
1139+
items: [
1140+
{
1141+
name: "Fender Stratocaster",
1142+
cost: 100_000,
1143+
options: ["red", "blue", "green"]
1144+
},
1145+
{ name: "Mattel Hoverboard" }
1146+
]
1147+
}
1148+
)
1149+
1150+
expected_output = <<~STR.strip
1151+
Differing hashes.
1152+
1153+
#{
1154+
colored do
1155+
red_line %(Expected: { customer: { name: "Marty McFly", shipping_address: { line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" } }, items: [{ name: "Fender Stratocaster", cost: 100000, options: ["red", "blue", "green"] }, { name: "Chevy 4x4" }] })
1156+
green_line %( Actual: { customer: { name: "Marty McFly, Jr.", shipping_address: { line_1: "456 Ponderosa Ct.", city: "Hill Valley", state: "CA", zip: "90382" } }, items: [{ name: "Fender Stratocaster", cost: 100000, options: ["red", "blue", "green"] }, { name: "Mattel Hoverboard" }] })
1157+
end
1158+
}
1159+
1160+
Diff:
1161+
1162+
#{
1163+
colored do
1164+
plain_line %( {)
1165+
plain_line %( customer: {)
1166+
red_line %(- name: "Marty McFly",)
1167+
green_line %(+ name: "Marty McFly, Jr.",)
1168+
plain_line %( shipping_address: {)
1169+
red_line %(- line_1: "123 Main St.",)
1170+
green_line %(+ line_1: "456 Ponderosa Ct.",)
1171+
plain_line %( city: "Hill Valley",)
1172+
plain_line %( state: "CA",)
1173+
plain_line %( zip: "90382")
1174+
plain_line %( })
1175+
plain_line %( },)
1176+
plain_line %( items: [)
1177+
plain_line %( {)
1178+
plain_line %( name: "Fender Stratocaster",)
1179+
plain_line %( cost: 100000,)
1180+
plain_line %( options: ["red", "blue", "green"])
1181+
plain_line %( },)
1182+
plain_line %( {)
1183+
red_line %(- name: "Chevy 4x4")
1184+
green_line %(+ name: "Mattel Hoverboard")
1185+
plain_line %( })
1186+
plain_line %( ])
1187+
plain_line %( })
1188+
end
1189+
}
1190+
STR
1191+
1192+
expect(actual_output).to eq(expected_output)
1193+
end
1194+
end
1195+
11071196
context "given two objects which == each other" do
11081197
it "returns an empty string" do
11091198
expected = SuperDiff::Test::Person.new(name: "Marty")

0 commit comments

Comments
 (0)