Skip to content
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

Don't disregard trailing newlines in Differ input. #70 #71

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/rspec/support/encoded_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def <<(string)
@string << matching_encoding(string)
end

def split(regex_or_string)
@string.split(matching_encoding(regex_or_string))
def split(regex_or_string, limit=0)
@string.split(matching_encoding(regex_or_string), limit)
end

def to_s
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/support/hunk_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def diffs
end

def expected_lines
@expected.split("\n").map! { |e| e.chomp }
@expected.split("\n", -1).map! { |e| e.chomp }
end

def actual_lines
@actual.split("\n").map! { |e| e.chomp }
@actual.split("\n", -1).map! { |e| e.chomp }
end

def build_hunk(piece)
Expand Down
41 changes: 34 additions & 7 deletions spec/rspec/support/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ module Support
this
is
soo
@@ -9,6 +9,5 @@
@@ -9,7 +9,6 @@
equal
insert
a
-another
line

EOD

diff = differ.diff(actual, expected)
Expand All @@ -49,22 +50,23 @@ module Support
actual = "Tu avec carté {count} itém has\n".encode('UTF-16LE')
expect(differ.diff(actual, expected)).to eql(<<-EOD.encode('UTF-16LE'))

@@ -1,2 +1,2 @@
@@ -1,3 +1,3 @@
-Tu avec carte {count} item has
+Tu avec carté {count} itém has

EOD
end

it 'handles differently encoded strings that are compatible' do
expected = "abc\n".encode('us-ascii')
actual = "강인철\n".encode('UTF-8')
expect(differ.diff(actual, expected)).to eql "\n@@ -1,2 +1,2 @@\n-abc\n+강인철\n"
expect(differ.diff(actual, expected)).to eql "\n@@ -1,3 +1,3 @@\n-abc\n+강인철\n \n"
end

it 'uses the default external encoding when the two strings have incompatible encodings' do
expected = "Tu avec carte {count} item has\n"
actual = "Tu avec carté {count} itém has\n".encode('UTF-16LE')
expect(differ.diff(actual, expected)).to eq("\n@@ -1,2 +1,2 @@\n-Tu avec carte {count} item has\n+Tu avec carté {count} itém has\n")
expect(differ.diff(actual, expected)).to eq("\n@@ -1,3 +1,3 @@\n-Tu avec carte {count} item has\n+Tu avec carté {count} itém has\n \n")
expect(differ.diff(actual, expected).encoding).to eq(Encoding.default_external)
end

Expand Down Expand Up @@ -101,12 +103,14 @@ def inspect

expected_diff = <<'EOD'

@@ -1,5 +1,5 @@
@@ -1,6 +1,6 @@
<Animal
name=bob,
- species=tortoise
+ species=giraffe
>


EOD

diff = differ.diff(expected,actual)
Expand All @@ -120,7 +124,7 @@ def inspect
expected_diff = <<'EOD'


@@ -5,7 +5,7 @@
@@ -5,8 +5,8 @@
:metasyntactic,
"variable",
:delta,
Expand All @@ -129,6 +133,7 @@ def inspect
:width,
- "very wide"]
+ "quite wide"]

EOD

diff = differ.diff(expected,actual)
Expand Down Expand Up @@ -203,6 +208,22 @@ def inspect
expect(diff).to eq expected_diff
end

it "diffs strings identical except for newlines" do
expected = "this is:\n one string\n"
actual = "this is:\n one string"

expected_diff = <<'EOD'

@@ -1,3 +1,4 @@
this is:
one string
+
EOD

diff = differ.diff(expected,actual)
expect(diff).to eq expected_diff
end

it "splits items with newlines" do
expected_diff = <<'EOD'

Expand Down Expand Up @@ -251,6 +272,12 @@ def inspect
expect(diff).to be_empty
end

it "returns an empty string with two single line strings" do
diff = differ.diff "abc", "def"

expect(diff).to be_empty
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 This wasn't really related to this issue, but it was a missing test case so I threw it in just for completeness's sake.


it "returns a String if no diff is returned" do
diff = differ.diff 1, 2
expect(diff).to be_a(String)
Expand Down Expand Up @@ -289,7 +316,7 @@ def inspect
it "outputs colored diffs" do
expected = "foo bar baz\n"
actual = "foo bang baz\n"
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"
expected_diff = "\e[0m\n\e[0m\e[34m@@ -1,3 +1,3 @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m\e[0m \n\e[0m"

diff = differ.diff(expected,actual)
expect(diff).to eq expected_diff
Expand Down