Skip to content

Commit 94a9e1a

Browse files
committed
Fix multiline string diff with blank lines
1 parent bb46aec commit 94a9e1a

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Improve inspection of Module. [#263](https://github.com/splitwise/super_diff/pull/263) by [@phorsuedzie](https://github.com/phorsuedzie)
8+
- Fix multiline string diff with blank lines. [#266](https://github.com/splitwise/super_diff/pull/263)
89

910
## 0.13.0 - 2024-09-22
1011

lib/super_diff/basic/operation_tree_builders/multiline_string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def build_operation_tree
4242
attr_reader :sequence_matcher, :original_expected, :original_actual
4343

4444
def split_into_lines(string)
45-
string.scan(/.+(?:\r|\n|\r\n|\Z)/)
45+
string.scan(/.*(?:\r|\n|\r\n|\Z)/)
4646
end
4747

4848
def opcodes
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
require "spec_helper"
2+
3+
RSpec.describe SuperDiff, type: :unit do
4+
describe ".diff" do
5+
subject(:diff) { SuperDiff.diff(expected, actual) }
6+
7+
let(:expected) { <<~STRING }
8+
This here is a string.
9+
It contains separate lines.
10+
What else can I say?
11+
STRING
12+
let(:actual) { <<~STRING }
13+
This here is a string.
14+
It contains separate lines.
15+
What else can I say?
16+
STRING
17+
18+
context "with extra blank lines in the middle of expected" do
19+
let(:expected) { <<~STRING }
20+
This here is a string.
21+
22+
It contains separate lines.
23+
24+
25+
What else can I say?
26+
STRING
27+
28+
it "removes the extra blank lines" do
29+
expected_output =
30+
SuperDiff::Core::Helpers
31+
.style(color_enabled: true) do
32+
plain_line " This here is a string.\\n"
33+
expected_line "- \\n"
34+
plain_line " It contains separate lines.\\n"
35+
expected_line "- \\n"
36+
expected_line "- \\n"
37+
plain_line " What else can I say?\\n"
38+
end
39+
.to_s
40+
.chomp
41+
expect(diff).to eq(expected_output)
42+
end
43+
end
44+
45+
context "with extra blank lines in the middle of actual" do
46+
let(:actual) { <<~STRING }
47+
This here is a string.
48+
49+
It contains separate lines.
50+
51+
52+
What else can I say?
53+
STRING
54+
55+
it "adds the extra blank lines" do
56+
expected_output =
57+
SuperDiff::Core::Helpers
58+
.style(color_enabled: true) do
59+
plain_line " This here is a string.\\n"
60+
actual_line "+ \\n"
61+
plain_line " It contains separate lines.\\n"
62+
actual_line "+ \\n"
63+
actual_line "+ \\n"
64+
plain_line " What else can I say?\\n"
65+
end
66+
.to_s
67+
.chomp
68+
expect(diff).to eq(expected_output)
69+
end
70+
end
71+
72+
context "with two trailing newlines in expected but only one in actual" do
73+
let(:expected) { <<~STRING }
74+
This here is a string.
75+
It contains separate lines.
76+
What else can I say?
77+
78+
STRING
79+
80+
it "removes the trailing newline" do
81+
expected_output =
82+
SuperDiff::Core::Helpers
83+
.style(color_enabled: true) do
84+
plain_line " This here is a string.\\n"
85+
plain_line " It contains separate lines.\\n"
86+
plain_line " What else can I say?\\n"
87+
expected_line "- \\n"
88+
end
89+
.to_s
90+
.chomp
91+
expect(diff).to eq(expected_output)
92+
end
93+
end
94+
95+
context "with one trailing newline in expected but none in actual" do
96+
let(:actual) { <<~STRING.chomp }
97+
This here is a string.
98+
It contains separate lines.
99+
What else can I say?
100+
STRING
101+
102+
it "removes the trailing newline" do
103+
expected_output =
104+
SuperDiff::Core::Helpers
105+
.style(color_enabled: true) do
106+
plain_line " This here is a string.\\n"
107+
plain_line " It contains separate lines.\\n"
108+
expected_line "- What else can I say?\\n"
109+
actual_line "+ What else can I say?"
110+
end
111+
.to_s
112+
.chomp
113+
expect(diff).to eq(expected_output)
114+
end
115+
end
116+
end
117+
end

0 commit comments

Comments
 (0)