Skip to content

Commit ba1ba2c

Browse files
lucaserasjas14
authored andcommitted
Exclude more un-diffable objects (#273)
Closes #268. SuperDiff already skips diff output for incomparable types, such as `true`, `false`, and `nil`. We will now also skip diff output when comparing the following types (for which we can't produce a meaningful diff): * `Symbol` * `Numeric` * `Regexp` * `Class` * `Module`
1 parent 2234d03 commit ba1ba2c

File tree

3 files changed

+317
-3
lines changed

3 files changed

+317
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Improve inspection of Module. [#263](https://github.com/splitwise/super_diff/pull/263) by [@phorsuedzie](https://github.com/phorsuedzie)
88
- Fix multiline string diff with blank lines. [#266](https://github.com/splitwise/super_diff/pull/263)
99
- Improve inspection of Range objects. [#267](https://github.com/splitwise/super_diff/pull/267) by [@lucaseras](https://github.com/lucaseras)
10+
- Skip diffing of more un-diffable types. [#273](https://github.com/splitwise/super_diff/pull/273) by [@lucaseras](https://github.com/lucaseras)
1011

1112
### Other changes
1213

lib/super_diff/rspec/differ.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ def diff
2222

2323
def worth_diffing?
2424
comparing_inequal_values? && !comparing_primitive_values? &&
25-
!comparing_singleline_strings?
25+
!comparing_proc_values? && !comparing_singleline_strings?
2626
end
2727

2828
def comparing_inequal_values?
2929
!helpers.values_match?(expected, actual)
3030
end
3131

3232
def comparing_primitive_values?
33-
expected.is_a?(Symbol) || expected.is_a?(Integer) ||
34-
[true, false, nil].include?(expected)
33+
# strings are indeed primitives, but we still may want to diff them if
34+
# they are multiline strings (see #comparing_singleline_strings?)
35+
return false if expected.is_a?(String)
36+
37+
SuperDiff.primitive?(expected)
38+
end
39+
40+
def comparing_proc_values?
41+
expected.is_a?(Proc)
3542
end
3643

3744
def comparing_singleline_strings?

spec/integration/rspec/eq_matcher_spec.rb

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,312 @@
10891089
end
10901090
end
10911091

1092+
context 'when comparing procs (a kind of primitive)' do
1093+
it 'generates the correct expectation and does not generate a diff' do
1094+
as_both_colored_and_uncolored do |color_enabled|
1095+
snippet = <<~TEST.strip
1096+
actual = Proc.new { 'hello' }
1097+
expected = Proc.new { 'oh, hi!' }
1098+
expect(actual).to eq(expected)
1099+
TEST
1100+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1101+
1102+
expected_output =
1103+
build_expected_output(
1104+
color_enabled: color_enabled,
1105+
snippet: 'expect(actual).to eq(expected)',
1106+
newline_before_expectation: false,
1107+
expectation: proc do
1108+
line do
1109+
plain 'Expected '
1110+
actual '#<Proc>'
1111+
plain ' to eq '
1112+
expected '#<Proc>'
1113+
plain '.'
1114+
end
1115+
end,
1116+
diff: nil
1117+
)
1118+
1119+
expect(program).to produce_output_when_run(expected_output).in_color(
1120+
color_enabled
1121+
).removing_object_ids
1122+
end
1123+
end
1124+
end
1125+
1126+
context 'when comparing booleans (a kind of primitive)' do
1127+
it 'generates the correct expectation and does not generate a diff' do
1128+
as_both_colored_and_uncolored do |color_enabled|
1129+
snippet = <<~TEST.strip
1130+
actual = true
1131+
expected = false
1132+
expect(actual).to eq(expected)
1133+
TEST
1134+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1135+
1136+
expected_output =
1137+
build_expected_output(
1138+
color_enabled: color_enabled,
1139+
snippet: 'expect(actual).to eq(expected)',
1140+
newline_before_expectation: false,
1141+
expectation: proc do
1142+
line do
1143+
plain 'Expected '
1144+
actual 'true'
1145+
plain ' to eq '
1146+
expected 'false'
1147+
plain '.'
1148+
end
1149+
end,
1150+
diff: nil
1151+
)
1152+
1153+
expect(program).to produce_output_when_run(expected_output).in_color(
1154+
color_enabled
1155+
).removing_object_ids
1156+
end
1157+
end
1158+
end
1159+
1160+
context 'when comparing nils (a kind of primitive)' do
1161+
it 'generates the correct expectation and does not generate a diff' do
1162+
as_both_colored_and_uncolored do |color_enabled|
1163+
snippet = <<~TEST.strip
1164+
actual = nil
1165+
expected = 'something else'
1166+
expect(actual).to eq(expected)
1167+
TEST
1168+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1169+
1170+
expected_output =
1171+
build_expected_output(
1172+
color_enabled: color_enabled,
1173+
snippet: 'expect(actual).to eq(expected)',
1174+
newline_before_expectation: false,
1175+
expectation: proc do
1176+
line do
1177+
plain 'Expected '
1178+
actual 'nil'
1179+
plain ' to eq '
1180+
expected '"something else"'
1181+
plain '.'
1182+
end
1183+
end,
1184+
diff: nil
1185+
)
1186+
1187+
expect(program).to produce_output_when_run(expected_output).in_color(
1188+
color_enabled
1189+
).removing_object_ids
1190+
end
1191+
end
1192+
end
1193+
1194+
context 'when comparing symbols (a kind of primitive)' do
1195+
it 'generates the correct expectation and does not generate a diff' do
1196+
as_both_colored_and_uncolored do |color_enabled|
1197+
snippet = <<~TEST.strip
1198+
actual = :test
1199+
expected = :different_symbol
1200+
expect(actual).to eq(expected)
1201+
TEST
1202+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1203+
1204+
expected_output =
1205+
build_expected_output(
1206+
color_enabled: color_enabled,
1207+
snippet: 'expect(actual).to eq(expected)',
1208+
newline_before_expectation: false,
1209+
expectation: proc do
1210+
line do
1211+
plain 'Expected '
1212+
actual ':test'
1213+
plain ' to eq '
1214+
expected ':different_symbol'
1215+
plain '.'
1216+
end
1217+
end,
1218+
diff: nil
1219+
)
1220+
1221+
expect(program).to produce_output_when_run(expected_output).in_color(
1222+
color_enabled
1223+
).removing_object_ids
1224+
end
1225+
end
1226+
end
1227+
1228+
context 'when comparing numerics (a kind of primitive)' do
1229+
it 'generates the correct expectation and does not generate a diff' do
1230+
as_both_colored_and_uncolored do |color_enabled|
1231+
snippet = <<~TEST.strip
1232+
actual = 4
1233+
expected = 2.5
1234+
expect(actual).to eq(expected)
1235+
TEST
1236+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1237+
1238+
expected_output =
1239+
build_expected_output(
1240+
color_enabled: color_enabled,
1241+
snippet: 'expect(actual).to eq(expected)',
1242+
newline_before_expectation: false,
1243+
expectation: proc do
1244+
line do
1245+
plain 'Expected '
1246+
actual '4'
1247+
plain ' to eq '
1248+
expected '2.5'
1249+
plain '.'
1250+
end
1251+
end,
1252+
diff: nil
1253+
)
1254+
1255+
expect(program).to produce_output_when_run(expected_output).in_color(
1256+
color_enabled
1257+
).removing_object_ids
1258+
end
1259+
end
1260+
end
1261+
1262+
context 'when comparing regexps (a kind of primitive)' do
1263+
it 'generates the correct expectation and does not generate a diff' do
1264+
as_both_colored_and_uncolored do |color_enabled|
1265+
snippet = <<~TEST.strip
1266+
actual = /test/
1267+
expected = /another regexp/
1268+
expect(actual).to eq(expected)
1269+
TEST
1270+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1271+
1272+
expected_output =
1273+
build_expected_output(
1274+
color_enabled: color_enabled,
1275+
snippet: 'expect(actual).to eq(expected)',
1276+
newline_before_expectation: false,
1277+
expectation: proc do
1278+
line do
1279+
plain 'Expected '
1280+
actual '/test/'
1281+
plain ' to eq '
1282+
expected '/another regexp/'
1283+
plain '.'
1284+
end
1285+
end,
1286+
diff: nil
1287+
)
1288+
1289+
expect(program).to produce_output_when_run(expected_output).in_color(
1290+
color_enabled
1291+
).removing_object_ids
1292+
end
1293+
end
1294+
end
1295+
1296+
context 'when comparing classes (a kind of primitive)' do
1297+
it 'generates the correct expectation and does not generate a diff' do
1298+
as_both_colored_and_uncolored do |color_enabled|
1299+
snippet = <<~TEST.strip
1300+
class Test; end
1301+
class AnotherClass; end
1302+
expect(Test.new).to eq(AnotherClass.new)
1303+
TEST
1304+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1305+
1306+
expected_output =
1307+
build_expected_output(
1308+
color_enabled: color_enabled,
1309+
snippet: 'expect(Test.new).to eq(AnotherClass.new)',
1310+
newline_before_expectation: false,
1311+
expectation: proc do
1312+
line do
1313+
plain 'Expected '
1314+
actual '#<Test>'
1315+
plain ' to eq '
1316+
expected '#<AnotherClass>'
1317+
plain '.'
1318+
end
1319+
end,
1320+
diff: nil
1321+
)
1322+
1323+
expect(program).to produce_output_when_run(expected_output).in_color(
1324+
color_enabled
1325+
).removing_object_ids
1326+
end
1327+
end
1328+
end
1329+
1330+
context 'when comparing Modules (a kind of primitive)' do
1331+
it 'generates the correct expectation and does not generate a diff' do
1332+
as_both_colored_and_uncolored do |color_enabled|
1333+
snippet = <<~TEST.strip
1334+
module Test; end
1335+
module AnotherClass; end
1336+
expect(Test).to eq(AnotherClass)
1337+
TEST
1338+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1339+
1340+
expected_output =
1341+
build_expected_output(
1342+
color_enabled: color_enabled,
1343+
snippet: 'expect(Test).to eq(AnotherClass)',
1344+
newline_before_expectation: false,
1345+
expectation: proc do
1346+
line do
1347+
plain 'Expected '
1348+
actual 'Test'
1349+
plain ' to eq '
1350+
expected 'AnotherClass'
1351+
plain '.'
1352+
end
1353+
end,
1354+
diff: nil
1355+
)
1356+
1357+
expect(program).to produce_output_when_run(expected_output).in_color(
1358+
color_enabled
1359+
).removing_object_ids
1360+
end
1361+
end
1362+
end
1363+
1364+
context 'when comparing strings (a kind of primitive)' do
1365+
it 'generates the correct expectation and does not generate a diff' do
1366+
as_both_colored_and_uncolored do |color_enabled|
1367+
snippet = <<~TEST.strip
1368+
actual = 'test'
1369+
expected = 'another string'
1370+
expect(actual).to eq(expected)
1371+
TEST
1372+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1373+
1374+
expected_output =
1375+
build_expected_output(
1376+
color_enabled: color_enabled,
1377+
snippet: 'expect(actual).to eq(expected)',
1378+
newline_before_expectation: false,
1379+
expectation: proc do
1380+
line do
1381+
plain 'Expected '
1382+
actual '"test"'
1383+
plain ' to eq '
1384+
expected '"another string"'
1385+
plain '.'
1386+
end
1387+
end,
1388+
diff: nil
1389+
)
1390+
1391+
expect(program).to produce_output_when_run(expected_output).in_color(
1392+
color_enabled
1393+
).removing_object_ids
1394+
end
1395+
end
1396+
end
1397+
10921398
it_behaves_like 'a matcher that supports elided diffs' do
10931399
let(:matcher) { :eq }
10941400
end

0 commit comments

Comments
 (0)