Sha256: 4dc8c93a4ac56f9eb5bf1dd67537e8ca3276c9280274b029dc2a77ca2ca1d114

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

module Pact
  class Term

    attr_reader :generate, :matcher

    def self.json_create(obj)
      new(generate: obj['data']['generate'], matcher: obj['data']['matcher'])
    end

    def initialize(attributes = {})
      @generate = attributes[:generate]
      @matcher = attributes[:matcher]
      raise "Please specify a matcher for the Term" unless @matcher != nil
      raise "Please specify a value to generate for the Term" unless @generate != nil
      raise "Value to generate \"#{@generate}\" does not match regular expression #{@matcher}" unless @generate =~ @matcher
    end

    def to_json(options = {})
      { json_class: self.class.name, data: { generate: generate, matcher: matcher} }.to_json(options)
    end

    def match(literal)
      literal.respond_to?(:to_s) ? matcher.match(literal.to_s) : nil
    end

    def ==(other)
      return false unless other.respond_to?(:generate) && other.respond_to?(:matcher)
      generate == other.generate && matcher == other.matcher
    end

    def to_s
      "Pact::Term matcher: #{matcher.to_s}" + (generate.nil? ? "" : " generate: \"#{generate}\"")
    end

    def diff_with_actual(actual)
      match(actual) ? nil : {
        expected: self,
        actual: actual
      }
    end

    def empty?
      false
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pact-1.0.15 lib/pact/term.rb
pact-1.0.13 lib/pact/term.rb
pact-1.0.12 lib/pact/term.rb
pact-1.0.11 lib/pact/term.rb
pact-1.0.10 lib/pact/term.rb
pact-1.0.9 lib/pact/term.rb
pact-1.0.8 lib/pact/term.rb