class RSpec::Support::Differ

rubocop:disable ClassLength

Public Class Methods

new(opts={}) click to toggle source
# File lib/rspec/support/differ.rb, line 67
def initialize(opts={})
  @color = opts.fetch(:color, false)
  @object_preparer = opts.fetch(:object_preparer, lambda { |string| string })
end

Public Instance Methods

color?() click to toggle source
# File lib/rspec/support/differ.rb, line 63
def color?
  @color
end
diff(actual, expected) click to toggle source
# File lib/rspec/support/differ.rb, line 11
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.to_s
end
diff_as_object(actual, expected) click to toggle source

rubocop:enable MethodLength

# File lib/rspec/support/differ.rb, line 57
def diff_as_object(actual, expected)
  actual_as_string = object_to_string(actual)
  expected_as_string = object_to_string(expected)
  diff_as_string(actual_as_string, expected_as_string)
end
diff_as_string(actual, expected) click to toggle source

rubocop:disable MethodLength

# File lib/rspec/support/differ.rb, line 28
def diff_as_string(actual, expected)
  encoding = EncodedString.pick_encoding(actual, expected)

  actual   = EncodedString.new(actual, encoding)
  expected = EncodedString.new(expected, encoding)

  output = EncodedString.new("\n", encoding)
  hunks = build_hunks(actual, expected)

  hunks.each_cons(2) do |prev_hunk, current_hunk|
    begin
      if current_hunk.overlaps?(prev_hunk)
        add_old_hunk_to_hunk(current_hunk, prev_hunk)
      else
        add_to_output(output, prev_hunk.diff(format_type).to_s)
      end
    ensure
      add_to_output(output, "\n")
    end
  end

  finalize_output(output, hunks.last.diff(format_type).to_s) if hunks.last

  color_diff output
rescue Encoding::CompatibilityError
  handle_encoding_errors(actual, expected)
end