Class: Pact::Matchers::EmbeddedDiffFormatter

Inherits:
Object
  • Object
show all
Includes:
ActiveSupportSupport
Defined in:
lib/pact/matchers/embedded_diff_formatter.rb

Constant Summary collapse

C =
::Term::ANSIColor
EXPECTED =
/"EXPECTED([A-Z_]*)":/
ACTUAL =
/"ACTUAL([A-Z_]*)":/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupportSupport

#fix_all_the_things, #fix_json_formatting, #fix_regexp, #remove_unicode

Constructor Details

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

Returns a new instance of EmbeddedDiffFormatter.



18
19
20
21
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 18

def initialize diff, options = {}
  @diff = diff
  @colour = options.fetch(:colour, false)
end

Instance Attribute Details

#colourObject (readonly)



16
17
18
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 16

def colour
  @colour
end

#diffObject (readonly)



16
17
18
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 16

def diff
  @diff
end

Class Method Details

.call(diff, options = {colour: Pact.configuration.color_enabled}) ⇒ Object



23
24
25
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 23

def self.call diff, options = {colour: Pact.configuration.color_enabled}
  new(diff, options).call
end

Instance Method Details

#callObject



27
28
29
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 27

def call
  to_s
end

#coloured_key(match, colour) ⇒ Object



55
56
57
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 55

def coloured_key match, colour
  '"' + C.color(colour, match.downcase.gsub(/^"|":$/,'')) + '":'
end

#colourise(line) ⇒ Object



51
52
53
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 51

def colourise line
  line.gsub(EXPECTED){|match| coloured_key match, :red }.gsub(ACTUAL){ | match | coloured_key match, :green }
end

#colourise_message(message) ⇒ Object



47
48
49
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 47

def colourise_message message
  message.split("\n").collect{| line | colourise(line) }.join("\n")
end

#colourise_message_if_configured(message) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 39

def colourise_message_if_configured message
  if colour
    colourise_message message
  else
    message
  end
end

#to_hashObject



31
32
33
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 31

def to_hash
  diff
end

#to_sObject



35
36
37
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 35

def to_s
  colourise_message_if_configured fix_json_formatting(diff.to_json)
end