Skip to content

Commit

Permalink
fix: remove incorrect warning messages about matching rules being ign…
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 9, 2018
1 parent e658c1a commit 30328e0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
33 changes: 16 additions & 17 deletions lib/pact/matching_rules/merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ def standardise_paths matching_rules
end

def recurse expected, path
case expected
recursed = case expected
when Hash then recurse_hash(expected, path)
when Array then recurse_array(expected, path)
else
expected
end

wrap(recursed, path)
end

def recurse_hash hash, path
hash.each_with_object({}) do | (k, v), new_hash |
recursed = hash.each_with_object({}) do | (k, v), new_hash |
new_path = path + "['#{k}']"
new_hash[k] = recurse(wrap(v, new_path), new_path)
new_hash[k] = recurse(v, new_path)
end
end

Expand All @@ -64,7 +66,7 @@ def recurse_array array, path
new_array = []
array.each_with_index do | item, index |
new_path = path + "[#{index}]"
new_array << recurse(wrap(item, new_path), new_path)
new_array << recurse(item, new_path)
end
new_array
end
Expand All @@ -77,28 +79,25 @@ def warn_when_not_one_example_item array, path
end

def wrap object, path
rules = @matching_rules[path]
array_rules = @matching_rules["#{path}[*]*"]
return object unless rules || array_rules

if rules['match'] == 'type' && !rules.has_key?('min')
handle_match_type(object, path, rules)
elsif rules['regex']
handle_regex(object, path, rules)
if find_rule(path, 'match') == 'type' && !find_rule(path, 'min')
handle_match_type(object, path)
elsif find_rule(path, 'regex')
handle_regex(object, path)
else
object
end
end

def handle_match_type object, path, rules
def handle_match_type object, path
log_used_rule(path, 'match', 'type')
Pact::SomethingLike.new(recurse(object, path))
Pact::SomethingLike.new(object)
end

def handle_regex object, path, rules
def handle_regex object, path
regex = find_rule(path, 'regex')
log_used_rule(path, 'match', 'regex') # assumed to be present
log_used_rule(path, 'regex', rules['regex'])
Pact::Term.new(generate: object, matcher: Regexp.new(rules['regex']))
log_used_rule(path, 'regex', regex)
Pact::Term.new(generate: object, matcher: Regexp.new(regex))
end

def log_ignored_rules
Expand Down
59 changes: 57 additions & 2 deletions spec/lib/pact/matching_rules/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module MatchingRules

describe "type based matching" do
before do
allow($stderr).to receive(:puts)
allow($stderr).to receive(:puts).and_call_original
end

let(:expected) do
Expand All @@ -90,7 +90,7 @@ module MatchingRules
end

it "it logs the rules it has ignored" do
expect($stderr).to receive(:puts).with(/ignored.*matchingrule/)
expect($stderr).to receive(:puts).once.with(/ignored.*matchingrule/)
subject
end

Expand Down Expand Up @@ -356,6 +356,61 @@ module MatchingRules
expect(subject['@name']).to be_instance_of(Pact::SomethingLike)
end
end

describe "when a Pact.like is nested inside a Pact.each_like which is nested inside a Pact.like" do
let(:original_definition) do
Pact.like('foos' => Pact.each_like(Pact.like('name' => "foo1")))
end

let(:expected) do
Pact::Reification.from_term(original_definition)
end

let(:matching_rules) do
Extract.call(body: original_definition)
end

it "creates a Pact::SomethingLike containing a Pact::ArrayLike containing a Pact::SomethingLike" do
expect(subject.to_hash).to eq original_definition.to_hash
end
end

describe "when a Pact.array_like is the top level object" do
let(:original_definition) do
Pact.each_like('foos')
end

let(:expected) do
Pact::Reification.from_term(original_definition)
end

let(:matching_rules) do
Extract.call(body: original_definition)
end

it "creates a Pact::ArrayLike" do
expect(subject.to_hash).to eq original_definition.to_hash
end
end

describe "when a Pact.like containing an array is the top level object" do
let(:original_definition) do
Pact.like(['foos'])
end

let(:expected) do
Pact::Reification.from_term(original_definition)
end

let(:matching_rules) do
Extract.call(body: original_definition)
end

it "creates a Pact::SomethingLike" do
expect(subject).to be_a(Pact::SomethingLike)
expect(subject.to_hash).to eq original_definition.to_hash
end
end
end
end
end

0 comments on commit 30328e0

Please sign in to comment.