From 30328e07aad63676926069a6e7c9000c76517224 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 9 Aug 2018 10:01:27 +1000 Subject: [PATCH] fix: remove incorrect warning messages about matching rules being ignored Fixes: https://github.com/pact-foundation/pact-js/issues/205 --- lib/pact/matching_rules/merge.rb | 33 ++++++------ spec/lib/pact/matching_rules/merge_spec.rb | 59 +++++++++++++++++++++- 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/lib/pact/matching_rules/merge.rb b/lib/pact/matching_rules/merge.rb index 0487b2a..472c83d 100644 --- a/lib/pact/matching_rules/merge.rb +++ b/lib/pact/matching_rules/merge.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/lib/pact/matching_rules/merge_spec.rb b/spec/lib/pact/matching_rules/merge_spec.rb index 2b652af..006b6a8 100644 --- a/spec/lib/pact/matching_rules/merge_spec.rb +++ b/spec/lib/pact/matching_rules/merge_spec.rb @@ -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 @@ -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 @@ -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