Class: Pact::Matchers::EmbeddedDiffFormatter

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

Constant Summary collapse

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, #warn_about_regexp

Constructor Details

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

Returns a new instance of EmbeddedDiffFormatter.



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

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

Instance Attribute Details

#colourObject (readonly)



14
15
16
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 14

def colour
  @colour
end

#diffObject (readonly)



14
15
16
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 14

def diff
  @diff
end

Class Method Details

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



21
22
23
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 21

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

Instance Method Details

#callObject



25
26
27
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 25

def call
  to_s
end

#coloured_key(match, colour) ⇒ Object



53
54
55
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 53

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

#colourise(line) ⇒ Object



49
50
51
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 49

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

#colourise_message(message) ⇒ Object



45
46
47
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 45

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

#colourise_message_if_configured(message) ⇒ Object



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

def colourise_message_if_configured message
  if colour
    colourise_message message
  else
    message
  end
end

#to_hashObject



29
30
31
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 29

def to_hash
  diff
end

#to_sObject



33
34
35
# File 'lib/pact/matchers/embedded_diff_formatter.rb', line 33

def to_s
  colourise_message_if_configured fix_json_formatting(diff.to_json)
end