Sha256: 89b17b7cefae171eb63e35fd1a45b7b9ff674b14cf6543d6a65e65d269b6f2b5

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 KB

Contents

require 'pact/shared/active_support_support'
require 'rainbow'

module Pact
  module Matchers
    class EmbeddedDiffFormatter

      include Pact::ActiveSupportSupport

      EXPECTED = /"EXPECTED([A-Z_]*)":/

      ACTUAL = /"ACTUAL([A-Z_]*)":/

      attr_reader :diff, :colour

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

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

      def call
        to_s
      end

      def to_hash
        diff
      end

      def to_s
        colourise_message_if_configured fix_json_formatting(diff.to_json)
      end

      def colourise_message_if_configured message
        if colour
          colourise_message message
        else
          message
        end
      end

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

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

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

    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pact-support-1.21.1 lib/pact/matchers/embedded_diff_formatter.rb
pact-support-1.21.0 lib/pact/matchers/embedded_diff_formatter.rb
pact-support-1.20.2 lib/pact/matchers/embedded_diff_formatter.rb
pact-support-1.20.1 lib/pact/matchers/embedded_diff_formatter.rb
pact-support-1.20.0 lib/pact/matchers/embedded_diff_formatter.rb
pact-support-1.19.0 lib/pact/matchers/embedded_diff_formatter.rb
pact-support-1.18.1 lib/pact/matchers/embedded_diff_formatter.rb
pact-support-1.18.0 lib/pact/matchers/embedded_diff_formatter.rb