Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 357cc8b

Browse files
committed
In version 1.4 diff-lcs has minorly changed its format
This commit deals with both versions, as its just header notation thats changed. I'm not sure either is more correct than the other but its harmless to our diffing process. Some additional `\n` changes are due to the string type change, `"\\n"` is the same as `'\n'`.
1 parent 47c9ea9 commit 357cc8b

File tree

1 file changed

+94
-43
lines changed

1 file changed

+94
-43
lines changed

spec/rspec/support/differ_spec.rb

Lines changed: 94 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@
77
module RSpec
88
module Support
99
describe Differ do
10+
11+
# In the updated version of diff-lcs several diff headers change format slightly
12+
# compensate for this and change minimum version in RSpec 4
13+
if Diff::LCS::VERSION.to_f < 1.4
14+
one_line_header = "-1,2 +1,2"
15+
removing_two_line_header = "-1,3 +1"
16+
else
17+
one_line_header = "-1 +1"
18+
removing_two_line_header = "-1,3 +1,5"
19+
end
20+
1021
describe '#diff' do
1122
let(:differ) { RSpec::Support::Differ.new }
1223

1324
it "outputs unified diff of two strings" do
1425
expected = "foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
1526
actual = "foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
1627

17-
expected_diff = <<-'EOD'
28+
if Diff::LCS::VERSION.to_f < 1.4
29+
expected_diff = <<-'EOD'
1830
1931
2032
@@ -1,6 +1,6 @@
@@ -32,6 +44,25 @@ module Support
3244
-another
3345
line
3446
EOD
47+
else
48+
expected_diff = <<-'EOD'
49+
50+
51+
@@ -1,4 +1,6 @@
52+
foo
53+
-zap
54+
bar
55+
+zap
56+
this
57+
@@ -9,6 +11,7 @@
58+
equal
59+
insert
60+
a
61+
-another
62+
line
63+
EOD
64+
end
65+
3566

3667
diff = differ.diff(actual, expected)
3768
expect(diff).to be_diffed_as(expected_diff)
@@ -41,25 +72,45 @@ module Support
4172
expected = "foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
4273
actual = "foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
4374

44-
expected_diff = dedent(<<-'EOS')
45-
|
46-
|
47-
|@@ -1,6 +1,6 @@
48-
| foo
49-
|-zap
50-
| bar
51-
|+zap
52-
| this
53-
| is
54-
| soo
55-
|@@ -9,6 +9,5 @@
56-
| equal
57-
| insert
58-
| a
59-
|-another
60-
| line
61-
|
62-
EOS
75+
if Diff::LCS::VERSION.to_f < 1.4
76+
expected_diff = dedent(<<-'EOS')
77+
|
78+
|
79+
|@@ -1,6 +1,6 @@
80+
| foo
81+
|-zap
82+
| bar
83+
|+zap
84+
| this
85+
| is
86+
| soo
87+
|@@ -9,6 +9,5 @@
88+
| equal
89+
| insert
90+
| a
91+
|-another
92+
| line
93+
|
94+
EOS
95+
else
96+
expected_diff = dedent(<<-'EOS')
97+
|
98+
|
99+
|@@ -1,4 +1,6 @@
100+
| foo
101+
|-zap
102+
| bar
103+
|+zap
104+
| this
105+
|@@ -9,6 +11,7 @@
106+
| equal
107+
| insert
108+
| a
109+
|-another
110+
| line
111+
|
112+
EOS
113+
end
63114

64115
diff = differ.diff(actual, expected)
65116
expect(diff).to be_diffed_as(expected_diff)
@@ -94,7 +145,7 @@ def differ_ivars
94145
actual = "Tu avec carté {count} itém has\n".encode('UTF-16LE')
95146
expected_diff = <<-EOD.encode('UTF-16LE')
96147
97-
@@ -1,2 +1,2 @@
148+
@@ #{one_line_header} @@
98149
-Tu avec carte {count} item has
99150
+Tu avec carté {count} itém has
100151
EOD
@@ -106,15 +157,15 @@ def differ_ivars
106157
it 'handles differently encoded strings that are compatible' do
107158
expected = "abc\n".encode('us-ascii')
108159
actual = "강인철\n".encode('UTF-8')
109-
expected_diff = "\n@@ -1,2 +1,2 @@\n-abc\n+강인철\n"
160+
expected_diff = "\n@@ #{one_line_header} @@\n-abc\n+강인철\n"
110161
diff = differ.diff(actual, expected)
111162
expect(diff).to be_diffed_as(expected_diff)
112163
end
113164

114165
it 'uses the default external encoding when the two strings have incompatible encodings', :failing_on_appveyor do
115166
expected = "Tu avec carte {count} item has\n"
116167
actual = "Tu avec carté {count} itém has\n".encode('UTF-16LE')
117-
expected_diff = "\n@@ -1,2 +1,2 @@\n-Tu avec carte {count} item has\n+Tu avec carté {count} itém has\n"
168+
expected_diff = "\n@@ #{one_line_header} @@\n-Tu avec carte {count} item has\n+Tu avec carté {count} itém has\n"
118169

119170
diff = differ.diff(actual, expected)
120171
expect(diff).to be_diffed_as(expected_diff)
@@ -202,7 +253,7 @@ def inspect; "<BrokenObject>"; end
202253

203254
expected_diff = <<-EOD
204255
205-
@@ -1,2 +1,2 @@
256+
@@ #{one_line_header} @@
206257
-[]
207258
+[<BrokenObject>]
208259
EOD
@@ -213,7 +264,7 @@ def inspect; "<BrokenObject>"; end
213264
diff = differ.diff(["a\r\nb"], ["a\r\nc"])
214265
expected_diff = <<-EOD
215266
216-
@@ -1,2 +1,2 @@
267+
@@ #{one_line_header} @@
217268
-a\\r\\nc
218269
+a\\r\\nb
219270
EOD
@@ -262,7 +313,7 @@ def inspect; "<BrokenObject>"; end
262313

263314
it 'outputs unified diff message of two hashes with differing encoding', :failing_on_appveyor do
264315
expected_diff = %Q{
265-
@@ -1,2 +1,2 @@
316+
@@ #{one_line_header} @@
266317
-"a" => "a",
267318
#{ (RUBY_VERSION.to_f > 1.8) ? %Q{+"ö" => "ö"} : '+"\303\266" => "\303\266"' },
268319
}
@@ -273,7 +324,7 @@ def inspect; "<BrokenObject>"; end
273324

274325
it 'outputs unified diff message of two hashes with encoding different to key encoding', :failing_on_appveyor do
275326
expected_diff = %Q{
276-
@@ -1,2 +1,2 @@
327+
@@ #{one_line_header} @@
277328
-:a => "a",
278329
#{ (RUBY_VERSION.to_f > 1.8) ? %Q{+\"한글\" => \"한글2\"} : '+"\355\225\234\352\270\200" => "\355\225\234\352\270\2002"' },
279330
}
@@ -284,7 +335,7 @@ def inspect; "<BrokenObject>"; end
284335

285336
it "outputs unified diff message of two hashes with object keys" do
286337
expected_diff = %Q{
287-
@@ -1,2 +1,2 @@
338+
@@ #{one_line_header} @@
288339
-["a", "c"] => "b",
289340
+["d", "c"] => "b",
290341
}
@@ -299,7 +350,7 @@ def inspect; "<BrokenObject>"; end
299350

300351
it "outputs unified diff message of two hashes with Time object keys" do
301352
expected_diff = %Q{
302-
@@ -1,2 +1,2 @@
353+
@@ #{one_line_header} @@
303354
-#{formatted_time} => "b",
304355
+#{formatted_time} => "c",
305356
}
@@ -310,7 +361,7 @@ def inspect; "<BrokenObject>"; end
310361

311362
it "outputs unified diff message of two hashes with hashes inside them" do
312363
expected_diff = %Q{
313-
@@ -1,2 +1,2 @@
364+
@@ #{one_line_header} @@
314365
-"b" => {"key_1"=>#{formatted_time}},
315366
+"c" => {"key_1"=>#{formatted_time}},
316367
}
@@ -328,7 +379,7 @@ def inspect; "<BrokenObject>"; end
328379

329380
it "outputs unified diff message of two arrays with Time object keys" do
330381
expected_diff = %Q{
331-
@@ -1,2 +1,2 @@
382+
@@ #{one_line_header} @@
332383
-[#{formatted_time}, "b"]
333384
+[#{formatted_time}, "c"]
334385
}
@@ -339,7 +390,7 @@ def inspect; "<BrokenObject>"; end
339390

340391
it "outputs unified diff message of two arrays with hashes inside them" do
341392
expected_diff = %Q{
342-
@@ -1,2 +1,2 @@
393+
@@ #{one_line_header} @@
343394
-[{"b"=>#{formatted_time}}, "c"]
344395
+[{"a"=>#{formatted_time}}, "c"]
345396
}
@@ -368,23 +419,23 @@ def inspect; "<BrokenObject>"; end
368419
end
369420

370421
it "splits items with newlines" do
371-
expected_diff = <<'EOD'
422+
expected_diff = <<"EOD"
372423
373-
@@ -1,3 +1 @@
374-
-a\nb
375-
-c\nd
424+
@@ #{removing_two_line_header} @@
425+
-a\\nb
426+
-c\\nd
376427
EOD
377428

378429
diff = differ.diff [], ["a\nb", "c\nd"]
379430
expect(diff).to be_diffed_as(expected_diff)
380431
end
381432

382433
it "shows inner arrays on a single line" do
383-
expected_diff = <<'EOD'
434+
expected_diff = <<"EOD"
384435
385-
@@ -1,3 +1 @@
386-
-a\nb
387-
-["c\nd"]
436+
@@ #{removing_two_line_header} @@
437+
-a\\nb
438+
-["c\\nd"]
388439
EOD
389440

390441
diff = differ.diff [], ["a\nb", ["c\nd"]]
@@ -434,7 +485,7 @@ def inspect; "<BrokenObject>"; end
434485

435486
expected_diff = dedent(<<-EOS)
436487
|
437-
|@@ -1,2 +1,2 @@
488+
|@@ #{one_line_header} @@
438489
|-[#<SimpleDelegator(#{object.inspect})>]
439490
|+[#{object.inspect}]
440491
|
@@ -456,7 +507,7 @@ def inspect; "<BrokenObject>"; end
456507

457508
expected_diff = dedent(<<-EOS)
458509
|
459-
|@@ -1,2 +1,2 @@
510+
|@@ #{one_line_header} @@
460511
|-"oop"
461512
|+"oof"
462513
|
@@ -473,7 +524,7 @@ def inspect; "<BrokenObject>"; end
473524
it "outputs colored diffs" do
474525
expected = "foo bar baz\n"
475526
actual = "foo bang baz\n"
476-
expected_diff = "\e[0m\n\e[0m\e[34m@@ -1,2 +1,2 @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m"
527+
expected_diff = "\e[0m\n\e[0m\e[34m@@ #{one_line_header} @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m"
477528

478529
diff = differ.diff(expected,actual)
479530
expect(diff).to be_diffed_as(expected_diff)

0 commit comments

Comments
 (0)