Sha256: f78b72a04a62e3b1d68602df9365ffe6e262bd15a37d5ca446c91a124a18568f

Contents?: true

Size: 1.67 KB

Versions: 20

Compression:

Stored size: 1.67 KB

Contents

module Pact
  module Matchers
    class ExtractDiffMessages

      attr_reader :diff

      def initialize diff, options = {}
        @diff = diff
      end

      def self.call diff, options = {}
        new(diff, options).call
      end

      def to_hash
        diff
      end

      def call
        to_s
      end

      def to_s
        diff_messages(diff).join("\n")
      end

      def diff_messages obj, path = [], messages = []
        case obj
        when Hash then handle_hash obj, path, messages
        when Array then handle_array obj, path, messages
        when BaseDifference then handle_difference obj, path, messages
        when NoDiffAtIndex then nil
        else
          raise "Invalid diff, expected Hash, Array, NoDiffAtIndex or BaseDifference, found #{obj.class}"
        end
        messages
      end

      def handle_hash hash, path, messages
        hash.each_pair do | key, value |
          next_part = key =~ /\s/ ? key.inspect : key
          diff_messages value, path + [".#{next_part}"], messages
        end
      end

      def handle_array array, path, messages
        array.each_with_index do | obj, index |
          diff_messages obj, path + ["[#{index}]"], messages
        end
      end

      def handle_difference difference, path, messages
        if difference.message
          message = "* #{difference.message}"
          message = message.gsub("<path>", path_to_s(path))
          message = message.gsub("<parent_path>", parent_path_to_s(path))
          messages << message
        end
      end

      def path_to_s path
        "$" + path.join
      end

      def parent_path_to_s path
        path_to_s(path[0..-2])
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
pact-support-1.3.1 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.3.0 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.2.99.alpha.1 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.3.0.alpha.2 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.3.0.alpha.1 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.2.5 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.2.4 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.2.3 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.2.2 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.2.1 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.2.0 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.1.8 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.1.7 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.1.6 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.1.5 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.1.4 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.1.3 lib/pact/matchers/extract_diff_messages.rb
pact-support-1.1.2 lib/pact/matchers/extract_messages.rb
pact-support-1.1.1 lib/pact/matchers/extract_messages.rb
pact-support-1.1.0 lib/pact/matchers/extract_messages.rb