class Pact::Matchers::UnixDiffFormatter
Constants
- MESSAGES_TITLE
Attributes
diff[R]
Public Class Methods
call(diff, options = {})
click to toggle source
# 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
new(diff, options = {})
click to toggle source
# 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
Public Instance Methods
call()
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 30 def call to_s end
to_s()
click to toggle source
# 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
Private Instance Methods
add_comma_to_end_of_arrays(string)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 122 def add_comma_to_end_of_arrays string string.gsub(/(\n\s*\])/, ',\1') end
copy_array(array, target)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 78 def copy_array array, target array.each_index.each_with_object([]) do | index, new_array | value = handle array[index], target new_array[index] = value unless (UnexpectedIndex === value || IndexNotFound === value) end end
copy_diff(difference, target)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 89 def copy_diff difference, target if target == :actual handle difference.actual, target else handle difference.expected, target end end
copy_hash(hash, target)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 71 def copy_hash hash, target hash.keys.each_with_object({}) do | key, new_hash | value = handle hash[key], target new_hash[key] = value unless (KeyNotFound === value || UnexpectedKey === value) end end
copy_no_diff(thing, target)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 85 def copy_no_diff(thing, target) NoDifferenceDecorator.new end
copy_object(object, target)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 97 def copy_object object, target if Regexp === object RegexpDecorator.new(object) else object end end
generate_string(diff, target)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 60 def generate_string diff, target comparable = handle(diff, target) begin # Can't think of an elegant way to check if we can pretty generate other than to try it and maybe fail json = fix_blank_lines_in_empty_hashes JSON.pretty_generate(comparable) add_comma_to_end_of_arrays json rescue JSON::GeneratorError comparable.to_s end end
handle(thing, target)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 48 def handle thing, target case thing when Hash then copy_hash(thing, target) when Array then copy_array(thing, target) when Difference then copy_diff(thing, target) when TypeDifference then copy_diff(thing, target) when RegexpDifference then copy_diff(thing, target) when NoDiffAtIndex then copy_no_diff(thing, target) else copy_object(thing, target) end end
key()
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 105 def key "Diff\n--------------------------------------\n" + "Key: " + @differ.red("-") + @differ.red(" is expected \n") + @differ.green(" +") + @differ.green(" is actual \n") + "Matching keys and values are not shown\n" end
remove_comma_from_end_of_arrays(string)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 126 def remove_comma_from_end_of_arrays string string.gsub(/,(\n\s*\])/, '\1') end
remove_first_line(string_diff)
click to toggle source
# File lib/pact/matchers/unix_diff_formatter.rb, line 112 def remove_first_line string_diff lines = string_diff.split("\n") if lines[0] =~ /@@/ lines[1..-1].join("\n") else string_diff end end