From 4a9426f221fcdccd5ad7ea97e7501f3d323ad808 Mon Sep 17 00:00:00 2001 From: Joe Stein <569991+jas14@users.noreply.github.com> Date: Sun, 22 Sep 2024 20:36:47 -0700 Subject: [PATCH] Fix blank line multiline string diff --- .../multiline_string.rb | 2 +- .../multiline_string_spec.rb | 117 ++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 spec/unit/basic/operation_tree_builders/multiline_string_spec.rb diff --git a/lib/super_diff/basic/operation_tree_builders/multiline_string.rb b/lib/super_diff/basic/operation_tree_builders/multiline_string.rb index d6f1d9e2..a0583e1e 100644 --- a/lib/super_diff/basic/operation_tree_builders/multiline_string.rb +++ b/lib/super_diff/basic/operation_tree_builders/multiline_string.rb @@ -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 diff --git a/spec/unit/basic/operation_tree_builders/multiline_string_spec.rb b/spec/unit/basic/operation_tree_builders/multiline_string_spec.rb new file mode 100644 index 00000000..fa82ddc2 --- /dev/null +++ b/spec/unit/basic/operation_tree_builders/multiline_string_spec.rb @@ -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