Skip to content

Commit

Permalink
Fix blank line multiline string diff
Browse files Browse the repository at this point in the history
  • Loading branch information
jas14 committed Oct 11, 2024
1 parent bb46aec commit 4a9426f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
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

0 comments on commit 4a9426f

Please sign in to comment.