Skip to content

Fix multiline string diff with blank lines #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

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

## 0.13.0 - 2024-09-22

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def build_operation_tree
attr_reader :sequence_matcher, :original_expected, :original_actual

def split_into_lines(string)
string.scan(/.+(?:\r|\n|\r\n|\Z)/)
string.scan(/.*(?:\r|\n|\r\n|\Z)/)
end

def opcodes
Expand Down
117 changes: 117 additions & 0 deletions spec/unit/basic/operation_tree_builders/multiline_string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require "spec_helper"

RSpec.describe SuperDiff, type: :unit do
describe ".diff" do
subject(:diff) { SuperDiff.diff(expected, actual) }

let(:expected) { <<~STRING }
This here is a string.
It contains separate lines.
What else can I say?
STRING
let(:actual) { <<~STRING }
This here is a string.
It contains separate lines.
What else can I say?
STRING

context "with extra blank lines in the middle of expected" do
let(:expected) { <<~STRING }
This here is a string.

It contains separate lines.


What else can I say?
STRING

it "removes the extra blank lines" do
expected_output =
SuperDiff::Core::Helpers
.style(color_enabled: true) do
plain_line " This here is a string.\\n"
expected_line "- \\n"
plain_line " It contains separate lines.\\n"
expected_line "- \\n"
expected_line "- \\n"
plain_line " What else can I say?\\n"
end
.to_s
.chomp
expect(diff).to eq(expected_output)
end
end

context "with extra blank lines in the middle of actual" do
let(:actual) { <<~STRING }
This here is a string.

It contains separate lines.


What else can I say?
STRING

it "adds the extra blank lines" do
expected_output =
SuperDiff::Core::Helpers
.style(color_enabled: true) do
plain_line " This here is a string.\\n"
actual_line "+ \\n"
plain_line " It contains separate lines.\\n"
actual_line "+ \\n"
actual_line "+ \\n"
plain_line " What else can I say?\\n"
end
.to_s
.chomp
expect(diff).to eq(expected_output)
end
end

context "with two trailing newlines in expected but only one in actual" do
let(:expected) { <<~STRING }
This here is a string.
It contains separate lines.
What else can I say?

STRING

it "removes the trailing newline" do
expected_output =
SuperDiff::Core::Helpers
.style(color_enabled: true) do
plain_line " This here is a string.\\n"
plain_line " It contains separate lines.\\n"
plain_line " What else can I say?\\n"
expected_line "- \\n"
end
.to_s
.chomp
expect(diff).to eq(expected_output)
end
end

context "with one trailing newline in expected but none in actual" do
let(:actual) { <<~STRING.chomp }
This here is a string.
It contains separate lines.
What else can I say?
STRING

it "removes the trailing newline" do
expected_output =
SuperDiff::Core::Helpers
.style(color_enabled: true) do
plain_line " This here is a string.\\n"
plain_line " It contains separate lines.\\n"
expected_line "- What else can I say?\\n"
actual_line "+ What else can I say?"
end
.to_s
.chomp
expect(diff).to eq(expected_output)
end
end
end
end