Class: RSpec::Support::Differ

### Home Classes Methods

### In Files

- lib/rspec/support/differ.rb

### Parent

Object

### Methods

- ::new
- #diff
- #diff_as_object
- #diff_as_string

### Class/Module Index

Quicksearch

- RSpec
- RSpec::CallerFilter
- RSpec::Support
- RSpec::Support::BlockSignature
- RSpec::Support::Differ
- RSpec::Support::DirectoryMaker
- RSpec::Support::EncodedString
- RSpec::Support::FormattingSupport
- RSpec::Support::FuzzyMatcher
- RSpec::Support::HunkGenerator
- RSpec::Support::InSubProcess
- RSpec::Support::LooseSignatureVerifier
- RSpec::Support::LooseSignatureVerifier::SignatureWithKeywordArgumentsMatcher
- RSpec::Support::MethodSignature
- RSpec::Support::MethodSignatureVerifier
- RSpec::Support::OS
- RSpec::Support::RecursiveConstMethods
- RSpec::Support::Ruby
- RSpec::Support::RubyFeatures
- RSpec::Support::ShellOut
- RSpec::Support::Spec
- RSpec::Support::StdErrSplitter
- RSpec::Support::StrictSignatureVerifier
- RSpec::Support::Version
- RSpec::Support::VersionChecker
- RSpec::Support::Warnings
- RSpec::Support::WarningsPrevention
- RSpec::Support::WithIsolatedStdErr
- Object
- RSpecHelpers

No matching classes.

# RSpec::Support::Differ

rubocop:disable ClassLength

### Attributes

color[R]
color?[R]

### Public Class Methods

new(opts={}) click to toggle source

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

### Public Instance Methods

diff(actual, expected) click to toggle source

    # File lib/rspec/support/differ.rb, line 10
    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 56
    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 27
    def diff_as_string(actual, expected)
      encoding = 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.