Skip to content

Commit 30328e0

Browse files
committed
fix: remove incorrect warning messages about matching rules being ignored
Fixes: pact-foundation/pact-js#205
1 parent e658c1a commit 30328e0

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

lib/pact/matching_rules/merge.rb

+16-17
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ def standardise_paths matching_rules
3131
end
3232

3333
def recurse expected, path
34-
case expected
34+
recursed = case expected
3535
when Hash then recurse_hash(expected, path)
3636
when Array then recurse_array(expected, path)
3737
else
3838
expected
3939
end
40+
41+
wrap(recursed, path)
4042
end
4143

4244
def recurse_hash hash, path
43-
hash.each_with_object({}) do | (k, v), new_hash |
45+
recursed = hash.each_with_object({}) do | (k, v), new_hash |
4446
new_path = path + "['#{k}']"
45-
new_hash[k] = recurse(wrap(v, new_path), new_path)
47+
new_hash[k] = recurse(v, new_path)
4648
end
4749
end
4850

@@ -64,7 +66,7 @@ def recurse_array array, path
6466
new_array = []
6567
array.each_with_index do | item, index |
6668
new_path = path + "[#{index}]"
67-
new_array << recurse(wrap(item, new_path), new_path)
69+
new_array << recurse(item, new_path)
6870
end
6971
new_array
7072
end
@@ -77,28 +79,25 @@ def warn_when_not_one_example_item array, path
7779
end
7880

7981
def wrap object, path
80-
rules = @matching_rules[path]
81-
array_rules = @matching_rules["#{path}[*]*"]
82-
return object unless rules || array_rules
83-
84-
if rules['match'] == 'type' && !rules.has_key?('min')
85-
handle_match_type(object, path, rules)
86-
elsif rules['regex']
87-
handle_regex(object, path, rules)
82+
if find_rule(path, 'match') == 'type' && !find_rule(path, 'min')
83+
handle_match_type(object, path)
84+
elsif find_rule(path, 'regex')
85+
handle_regex(object, path)
8886
else
8987
object
9088
end
9189
end
9290

93-
def handle_match_type object, path, rules
91+
def handle_match_type object, path
9492
log_used_rule(path, 'match', 'type')
95-
Pact::SomethingLike.new(recurse(object, path))
93+
Pact::SomethingLike.new(object)
9694
end
9795

98-
def handle_regex object, path, rules
96+
def handle_regex object, path
97+
regex = find_rule(path, 'regex')
9998
log_used_rule(path, 'match', 'regex') # assumed to be present
100-
log_used_rule(path, 'regex', rules['regex'])
101-
Pact::Term.new(generate: object, matcher: Regexp.new(rules['regex']))
99+
log_used_rule(path, 'regex', regex)
100+
Pact::Term.new(generate: object, matcher: Regexp.new(regex))
102101
end
103102

104103
def log_ignored_rules

spec/lib/pact/matching_rules/merge_spec.rb

+57-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module MatchingRules
7070

7171
describe "type based matching" do
7272
before do
73-
allow($stderr).to receive(:puts)
73+
allow($stderr).to receive(:puts).and_call_original
7474
end
7575

7676
let(:expected) do
@@ -90,7 +90,7 @@ module MatchingRules
9090
end
9191

9292
it "it logs the rules it has ignored" do
93-
expect($stderr).to receive(:puts).with(/ignored.*matchingrule/)
93+
expect($stderr).to receive(:puts).once.with(/ignored.*matchingrule/)
9494
subject
9595
end
9696

@@ -356,6 +356,61 @@ module MatchingRules
356356
expect(subject['@name']).to be_instance_of(Pact::SomethingLike)
357357
end
358358
end
359+
360+
describe "when a Pact.like is nested inside a Pact.each_like which is nested inside a Pact.like" do
361+
let(:original_definition) do
362+
Pact.like('foos' => Pact.each_like(Pact.like('name' => "foo1")))
363+
end
364+
365+
let(:expected) do
366+
Pact::Reification.from_term(original_definition)
367+
end
368+
369+
let(:matching_rules) do
370+
Extract.call(body: original_definition)
371+
end
372+
373+
it "creates a Pact::SomethingLike containing a Pact::ArrayLike containing a Pact::SomethingLike" do
374+
expect(subject.to_hash).to eq original_definition.to_hash
375+
end
376+
end
377+
378+
describe "when a Pact.array_like is the top level object" do
379+
let(:original_definition) do
380+
Pact.each_like('foos')
381+
end
382+
383+
let(:expected) do
384+
Pact::Reification.from_term(original_definition)
385+
end
386+
387+
let(:matching_rules) do
388+
Extract.call(body: original_definition)
389+
end
390+
391+
it "creates a Pact::ArrayLike" do
392+
expect(subject.to_hash).to eq original_definition.to_hash
393+
end
394+
end
395+
396+
describe "when a Pact.like containing an array is the top level object" do
397+
let(:original_definition) do
398+
Pact.like(['foos'])
399+
end
400+
401+
let(:expected) do
402+
Pact::Reification.from_term(original_definition)
403+
end
404+
405+
let(:matching_rules) do
406+
Extract.call(body: original_definition)
407+
end
408+
409+
it "creates a Pact::SomethingLike" do
410+
expect(subject).to be_a(Pact::SomethingLike)
411+
expect(subject.to_hash).to eq original_definition.to_hash
412+
end
413+
end
359414
end
360415
end
361416
end

0 commit comments

Comments
 (0)