Created
March 25, 2014 05:55
-
-
Save phiggins/9755950 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/rspec-support/spec/rspec/support/differ_spec.rb b/rspec-expectations/spec/rspec/expectations/diff_presenter_spec.rb | |
index 69b1237..0c19de4 100644 | |
--- a/rspec-support/spec/rspec/support/differ_spec.rb | |
+++ b/rspec-expectations/spec/rspec/expectations/diff_presenter_spec.rb | |
@@ -1,18 +1,21 @@ | |
# encoding: utf-8 | |
-require 'spec_helper' | |
require 'ostruct' | |
module RSpec | |
- module Support | |
- describe Differ do | |
- describe '#diff' do | |
- let(:differ) { RSpec::Support::Differ.new } | |
+ module Expectations | |
+ describe DiffPresenter do | |
+ let(:differ) { RSpec::Expectations::DiffPresenter.new } | |
+ context "without --color" do | |
- it "outputs unified diff of two strings" do | |
- expected = "foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n" | |
- actual = "foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n" | |
+ before { allow(RSpec::Matchers.configuration).to receive_messages(:color? => false) } | |
+ | |
+ describe '#diff_as_string' do | |
+ subject { differ.diff_as_string(@actual, @expected) } | |
- expected_diff = <<-'EOD' | |
+ it "outputs unified diff of two strings" do | |
+ @expected = "foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n" | |
+ @actual = "foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n" | |
+ expect(subject).to eq(<<-'EOD') | |
@@ -1,6 +1,6 @@ | |
@@ -31,22 +34,13 @@ module RSpec | |
line | |
EOD | |
- diff = differ.diff(actual, expected) | |
- expect(diff).to eql(expected_diff) | |
end | |
if String.method_defined?(:encoding) | |
- it "returns an empty string if strings are not multiline" do | |
- expected = "Tu avec carte {count} item has".encode('UTF-16LE') | |
- actual = "Tu avec carté {count} itém has".encode('UTF-16LE') | |
- | |
- expect(differ.diff(actual, expected)).to be_empty | |
- end | |
- | |
it 'copes with encoded strings' do | |
- expected = "Tu avec carte {count} item has\n".encode('UTF-16LE') | |
- actual = "Tu avec carté {count} itém has\n".encode('UTF-16LE') | |
- expect(differ.diff(actual, expected)).to eql(<<-EOD.encode('UTF-16LE')) | |
+ @expected = "Tu avec carte {count} item has".encode('UTF-16LE') | |
+ @actual = "Tu avec carté {count} itém has".encode('UTF-16LE') | |
+ expect(subject).to eql(<<-EOD.encode('UTF-16LE')) | |
@@ -1,2 +1,2 @@ | |
-Tu avec carte {count} item has | |
@@ -55,30 +49,30 @@ 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" | |
+ @expected = "abc".encode('us-ascii') | |
+ @actual = "강인철".encode('UTF-8') | |
+ expect(subject).to eql "\n@@ -1,2 +1,2 @@\n-abc\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).encoding).to eq(Encoding.default_external) | |
+ @expected = "Tu avec carte {count} item has" | |
+ @actual = "Tu avec carté {count} itém has".encode('UTF-16LE') | |
+ expect(subject).to eq("\n@@ -1,2 +1,2 @@\n-Tu avec carte {count} item has\n+Tu avec carté {count} itém has\n") | |
+ expect(subject.encoding).to eq(Encoding.default_external) | |
end | |
it 'handles any encoding error that occurs with a helpful error message' do | |
- expect(RSpec::Support::HunkGenerator).to receive(:new). | |
- and_raise(Encoding::CompatibilityError) | |
- expected = "Tu avec carte {count} item has\n".encode('us-ascii') | |
- actual = "Tu avec carté {count} itém has\n" | |
- diff = differ.diff(actual, expected) | |
- expect(diff).to match(/Could not produce a diff/) | |
- expect(diff).to match(/actual string \(UTF-8\)/) | |
- expect(diff).to match(/expected string \(US-ASCII\)/) | |
+ expect(Differ).to receive(:new).and_raise(Encoding::CompatibilityError) | |
+ @expected = "Tu avec carte {count} item has".encode('us-ascii') | |
+ @actual = "Tu avec carté {count} itém has" | |
+ expect(subject).to match(/Could not produce a diff/) | |
+ expect(subject).to match(/actual string \(UTF-8\)/) | |
+ expect(subject).to match(/expected string \(US-ASCII\)/) | |
end | |
end | |
+ end | |
+ describe '#diff_as_object' do | |
it "outputs unified diff message of two objects" do | |
animal_class = Class.new do | |
def initialize(name, species) | |
@@ -108,7 +102,7 @@ EOD | |
> | |
EOD | |
- diff = differ.diff(expected,actual) | |
+ diff = differ.diff_as_object(expected,actual) | |
expect(diff).to eq expected_diff | |
end | |
@@ -130,7 +124,7 @@ EOD | |
+ "quite wide"] | |
EOD | |
- diff = differ.diff(expected,actual) | |
+ diff = differ.diff_as_object(expected,actual) | |
expect(diff).to eq expected_diff | |
end | |
@@ -149,7 +143,7 @@ EOD | |
:width => "quite wide", | |
EOD | |
- diff = differ.diff(expected,actual) | |
+ diff = differ.diff_as_object(expected,actual) | |
expect(diff).to eq expected_diff | |
end | |
@@ -160,7 +154,7 @@ EOD | |
#{ (RUBY_VERSION.to_f > 1.8) ? %Q{+"ö" => "ö"} : '+"\303\266" => "\303\266"' }, | |
} | |
- diff = differ.diff({'ö' => 'ö'}, {'a' => 'a'}) | |
+ diff = differ.diff_as_object({'ö' => 'ö'}, {'a' => 'a'}) | |
expect(diff).to eq expected_diff | |
end | |
@@ -171,7 +165,7 @@ EOD | |
#{ (RUBY_VERSION.to_f > 1.8) ? %Q{+\"한글\" => \"한글2\"} : '+"\355\225\234\352\270\200" => "\355\225\234\352\270\2002"' }, | |
} | |
- diff = differ.diff({ "한글" => "한글2"}, { :a => "a"}) | |
+ diff = differ.diff_as_object({ "한글" => "한글2"}, { :a => "a"}) | |
expect(diff).to eq expected_diff | |
end | |
@@ -182,109 +176,73 @@ EOD | |
+["d", "c"] => "b", | |
} | |
- diff = differ.diff({ ['d','c'] => 'b'}, { ['a','c'] => 'b' }) | |
+ diff = differ.diff_as_object({ ['d','c'] => 'b'}, { ['a','c'] => 'b' }) | |
expect(diff).to eq expected_diff | |
end | |
- it "outputs unified diff of multi line strings" do | |
- expected = "this is:\n one string" | |
- actual = "this is:\n another string" | |
+ it "outputs unified diff of single line strings" do | |
+ expected = "this is one string" | |
+ actual = "this is another string" | |
expected_diff = <<'EOD' | |
-@@ -1,3 +1,3 @@ | |
- this is: | |
-- another string | |
-+ one string | |
+@@ -1,2 +1,2 @@ | |
+-"this is another string" | |
++"this is one string" | |
EOD | |
- diff = differ.diff(expected,actual) | |
+ diff = differ.diff_as_object(expected,actual) | |
expect(diff).to eq expected_diff | |
end | |
- it "splits items with newlines" do | |
- expected_diff = <<'EOD' | |
- | |
-@@ -1,3 +1 @@ | |
--a\nb | |
--c\nd | |
-EOD | |
- | |
- diff = differ.diff [], ["a\nb", "c\nd"] | |
- expect(diff).to eql expected_diff | |
- end | |
+ it "outputs unified diff of multi line strings" do | |
+ expected = "this is:\n one string" | |
+ actual = "this is:\n another string" | |
- it "shows inner arrays on a single line" do | |
expected_diff = <<'EOD' | |
-@@ -1,3 +1 @@ | |
--a\nb | |
--["c\nd"] | |
+@@ -1,3 +1,3 @@ | |
+ this is: | |
+- another string | |
++ one string | |
EOD | |
- diff = differ.diff [], ["a\nb", ["c\nd"]] | |
- expect(diff).to eql expected_diff | |
- end | |
- | |
- it "returns an empty string if no expected or actual" do | |
- diff = differ.diff nil, nil | |
- | |
- expect(diff).to be_empty | |
- end | |
- | |
- it "returns an empty string if expected is Numeric" do | |
- diff = differ.diff 1, "2" | |
- | |
- expect(diff).to be_empty | |
+ diff = differ.diff_as_object(expected,actual) | |
+ expect(diff).to eq expected_diff | |
end | |
- it "returns an empty string if actual is Numeric" do | |
- diff = differ.diff "1", 2 | |
+ it "uses matcher descriptions in place of matchers in diffs" do | |
+ expected = [a_string_matching(/foo/), a_string_matching(/bar/)] | |
+ actual = ["poo", "car"] | |
- expect(diff).to be_empty | |
- end | |
- | |
- it "returns an empty string if expected or actual are procs" do | |
- diff = differ.diff lambda {}, lambda {} | |
+ expected_diff = dedent(<<-EOS) | |
+ | | |
+ |@@ -1,2 +1,2 @@ | |
+ |-["poo", "car"] | |
+ |+[(a string matching /foo/), (a string matching /bar/)] | |
+ | | |
+ EOS | |
- expect(diff).to be_empty | |
+ diff = differ.diff_as_object(expected,actual) | |
+ expect(diff).to eq expected_diff | |
end | |
+ end | |
+ end | |
- context "with :object_preparer option set" do | |
- let(:differ) do | |
- RSpec::Support::Differ.new(:object_preparer => lambda { |s| s.to_s.reverse }) | |
- end | |
- | |
- it "uses the output of object_preparer for diffing" do | |
- expected = :foo | |
- actual = :poo | |
- | |
- expected_diff = dedent(<<-EOS) | |
- | | |
- |@@ -1,2 +1,2 @@ | |
- |-"oop" | |
- |+"oof" | |
- | | |
- EOS | |
- | |
- diff = differ.diff(expected, actual) | |
- expect(diff).to eq expected_diff | |
- end | |
- end | |
+ context "with --color" do | |
+ before { allow(RSpec::Matchers.configuration).to receive_messages(:color? => true) } | |
- context "with :color option set" do | |
- let(:differ) { RSpec::Support::Differ.new(:color => true) } | |
+ it "outputs colored diffs" do | |
+ expected = "foo bar baz" | |
+ actual = "foo bang baz" | |
+ 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" | |
- 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" | |
- diff = differ.diff(expected,actual) | |
- expect(diff).to eq expected_diff | |
- end | |
- end | |
+ diff = differ.diff_as_string(expected,actual) | |
+ expect(diff).to eq expected_diff | |
end | |
end | |
+ | |
+ end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment