Created
March 25, 2014 05:57
-
-
Save phiggins/9755957 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/lib/rspec/support/differ.rb b/rspec-expectations/lib/rspec/expectations/diff_presenter.rb | |
index 26da8d8..dd6e95d 100644 | |
--- a/rspec-support/lib/rspec/support/differ.rb | |
+++ b/rspec-expectations/lib/rspec/expectations/diff_presenter.rb | |
@@ -1,27 +1,14 @@ | |
-RSpec::Support.require_rspec_support 'encoded_string' | |
-RSpec::Support.require_rspec_support 'hunk_generator' | |
- | |
+require 'diff/lcs' | |
+require 'diff/lcs/hunk' | |
require 'pp' | |
+RSpec::Support.require_rspec_expectations "encoded_string" | |
+RSpec::Support.require_rspec_expectations "differ" | |
module RSpec | |
- module Support | |
- class Differ | |
- def diff(actual, expected) | |
- diff = "" | |
- | |
- if actual && expected | |
- if all_strings?(actual, expected) | |
- if any_multiline_strings?(actual, expected) | |
- diff = diff_as_string(coerce_to_string(actual), coerce_to_string(expected)) | |
- end | |
- elsif no_procs?(actual, expected) && no_numbers?(actual, expected) | |
- diff = diff_as_object(actual, expected) | |
- end | |
- end | |
- | |
- diff | |
- end | |
- | |
+ module Expectations | |
+ # Produces a diff for the submitted objects. | |
+ # @private | |
+ class DiffPresenter | |
def diff_as_string(actual, expected) | |
@encoding = pick_encoding actual, expected | |
@@ -57,59 +44,10 @@ module RSpec | |
diff_as_string(actual_as_string, expected_as_string) | |
end | |
- attr_reader :color | |
- alias_method :color?, :color | |
- | |
- def initialize(opts={}) | |
- @color = opts.fetch(:color, false) | |
- @object_preparer = opts.fetch(:object_preparer, lambda { |string| string }) | |
- end | |
- | |
private | |
- def no_procs?(*args) | |
- args.flatten.none? { |a| Proc === a} | |
- end | |
- | |
- def all_strings?(*args) | |
- args.flatten.all? { |a| String === a} | |
- end | |
- | |
- def any_multiline_strings?(*args) | |
- all_strings?(*args) && args.flatten.any? { |a| multiline?(a) } | |
- end | |
- | |
- def no_numbers?(*args) | |
- args.flatten.none? { |a| Numeric === a} | |
- end | |
- | |
- def coerce_to_string(string_or_array) | |
- return string_or_array unless Array === string_or_array | |
- diffably_stringify(string_or_array).join("\n") | |
- end | |
- | |
- def diffably_stringify(array) | |
- array.map do |entry| | |
- if Array === entry | |
- entry.inspect | |
- else | |
- entry.to_s.gsub("\n", "\\n") | |
- end | |
- end | |
- end | |
- | |
- if String.method_defined?(:encoding) | |
- def multiline?(string) | |
- string.include?("\n".encode(string.encoding)) | |
- end | |
- else | |
- def multiline?(string) | |
- string.include?("\n") | |
- end | |
- end | |
- | |
def hunks | |
- @hunks ||= HunkGenerator.new(@actual, @expected).hunks | |
+ @hunks ||= Differ.new(@actual, @expected).hunks | |
end | |
def finalize_output(output, final_line) | |
@@ -150,7 +88,7 @@ module RSpec | |
end | |
def color_diff(diff) | |
- return diff unless color? | |
+ return diff unless RSpec::Matchers.configuration.color? | |
diff.lines.map { |line| | |
case line[0].chr | |
@@ -167,7 +105,7 @@ module RSpec | |
end | |
def object_to_string(object) | |
- object = @object_preparer.call(object) | |
+ object = Matchers::Composable.surface_descriptions_in(object) | |
case object | |
when Hash | |
object.keys.sort_by { |k| k.to_s }.map do |key| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment