Class: Pact::Matchers::UnixDiffFormatter

Inherits:
Object
  • Object
show all
Includes:
JRubySupport
Defined in:
lib/pact/matchers/unix_diff_formatter.rb

Defined Under Namespace

Classes: NoDifferenceDecorator, RegexpDecorator

Constant Summary collapse

MESSAGES_TITLE =
"\n\nDescription of differences\n--------------------------------------"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JRubySupport

#fix_blank_lines_in_empty_hashes

Constructor Details

#initialize(diff, options = {}) ⇒ UnixDiffFormatter

Returns a new instance of UnixDiffFormatter.



14
15
16
17
18
19
20
21
# File 'lib/pact/matchers/unix_diff_formatter.rb', line 14

def initialize diff, options = {}
  @diff = diff
  @colour = options.fetch(:colour, false)
  @actual = options.fetch(:actual, {})
  @include_explanation = options.fetch(:include_explanation, true)
  @differ = Pact::Matchers::Differ.new(@colour)
  @messages = Pact::Matchers::ExtractDiffMessages.call(diff).collect{ | message| "* #{message}" }.join("\n")
end

Class Method Details

.call(diff, options = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/pact/matchers/unix_diff_formatter.rb', line 23

def self.call diff, options = {}
  # require stops circular dependency from pact/configuration <-> pact/matchers/unix_diff_formatter
  require 'pact/configuration'
  default_options = {colour: Pact.configuration.color_enabled}
  new(diff, default_options.merge(options)).call
end

Instance Method Details

#callObject



30
31
32
# File 'lib/pact/matchers/unix_diff_formatter.rb', line 30

def call
  to_s
end

#to_sObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pact/matchers/unix_diff_formatter.rb', line 34

def to_s

  expected = generate_string(diff, :expected)
  actual = generate_string(diff, :actual)
  suffix = @include_explanation ?  key + "\n" : ''
  messages = @include_explanation ? "#{MESSAGES_TITLE}\n#{@messages}\n" : ''
  string_diff = @differ.diff_as_string(actual, expected).lstrip
  string_diff = remove_first_line(string_diff)
  string_diff = remove_comma_from_end_of_arrays(string_diff)
  suffix + string_diff + messages
end