@@ -18,15 +18,15 @@ def initialize expected, matching_rules, root_path
18
18
19
19
def call
20
20
return @expected if @matching_rules . nil? || @matching_rules . empty?
21
- recurse @expected , @root_path
21
+ recurse ( @expected , @root_path ) . tap { log_ignored_rules }
22
22
end
23
23
24
24
private
25
25
26
26
def standardise_paths matching_rules
27
27
return matching_rules if matching_rules . nil? || matching_rules . empty?
28
- matching_rules . each_with_object ( { } ) do | ( path , rule ) , new_matching_rules |
29
- new_matching_rules [ JsonPath . new ( path ) . to_s ] = rule
28
+ matching_rules . each_with_object ( { } ) do | ( path , rules ) , new_matching_rules |
29
+ new_matching_rules [ JsonPath . new ( path ) . to_s ] = Marshal . load ( Marshal . dump ( rules ) ) # simplest way to deep clone
30
30
end
31
31
end
32
32
@@ -47,14 +47,14 @@ def recurse_hash hash, path
47
47
end
48
48
49
49
def recurse_array array , path
50
+
51
+ parent_match_rule = @matching_rules [ path ] && @matching_rules [ path ] [ 'matchers' ] && @matching_rules [ path ] [ 'matchers' ] . first && @matching_rules [ path ] [ 'matchers' ] . first . delete ( 'match' )
50
52
array_like_children_path = "#{ path } [*]*"
51
- parent_match_rule = @matching_rules [ path ] && @matching_rules [ path ] [ 'matchers' ] && @matching_rules [ path ] [ 'matchers' ] . first && @matching_rules [ path ] [ 'matchers' ] . first [ 'match' ]
52
- children_match_rule = @matching_rules [ array_like_children_path ] && @matching_rules [ array_like_children_path ] [ 'matchers' ] && @matching_rules [ array_like_children_path ] [ 'matchers' ] . first && @matching_rules [ array_like_children_path ] [ 'matchers' ] . first [ 'match' ]
53
- min = @matching_rules [ path ] && @matching_rules [ path ] [ 'matchers' ] && @matching_rules [ path ] [ 'matchers' ] . first && @matching_rules [ path ] [ 'matchers' ] . first [ 'min' ]
53
+ children_match_rule = @matching_rules [ array_like_children_path ] && @matching_rules [ array_like_children_path ] [ 'matchers' ] && @matching_rules [ array_like_children_path ] [ 'matchers' ] . first && @matching_rules [ array_like_children_path ] [ 'matchers' ] . first . delete ( 'match' )
54
+ min = @matching_rules [ path ] && @matching_rules [ path ] [ 'matchers' ] && @matching_rules [ path ] [ 'matchers' ] . first && @matching_rules [ path ] [ 'matchers' ] . first . delete ( 'min' )
54
55
55
56
if min && ( children_match_rule == 'type' || ( children_match_rule . nil? && parent_match_rule == 'type' ) )
56
57
warn_when_not_one_example_item ( array , path )
57
- # log_ignored_rules(path, @matching_rules[path], {'min' => min})
58
58
Pact ::ArrayLike . new ( recurse ( array . first , "#{ path } [*]" ) , min : min )
59
59
else
60
60
new_array = [ ]
@@ -82,30 +82,46 @@ def wrap object, path
82
82
elsif rules [ 'regex' ]
83
83
handle_regex ( object , path , rules )
84
84
else
85
- log_ignored_rules ( path , rules , { } )
85
+ # log_ignored_rules(path, rules, {})
86
86
object
87
87
end
88
88
end
89
89
90
90
def handle_match_type object , path , rules
91
- log_ignored_rules ( path , rules , { 'match' => 'type' } )
92
- Pact ::SomethingLike . new ( object )
91
+ rules . delete ( 'match' )
92
+ Pact ::SomethingLike . new ( recurse ( object , path ) )
93
93
end
94
94
95
95
def handle_regex object , path , rules
96
- log_ignored_rules ( path , rules , { 'match' => 'regex' , 'regex' => rules [ 'regex' ] } )
97
- Pact ::Term . new ( generate : object , matcher : Regexp . new ( rules [ 'regex' ] ) )
96
+ rules . delete ( 'match' )
97
+ regex = rules . delete ( 'regex' )
98
+ Pact ::Term . new ( generate : object , matcher : Regexp . new ( regex ) )
98
99
end
99
100
100
- def log_ignored_rules path , rules , used_rules
101
- dup_rules = rules . dup
102
- used_rules . each_pair do | used_key , used_value |
103
- dup_rules . delete ( used_key ) if dup_rules [ used_key ] == used_value
101
+ def log_ignored_rules
102
+ @matching_rules . each do | jsonpath , rules_hash |
103
+ rules_array = rules_hash [ "matchers" ]
104
+ ( ( rules_array . length - 1 ) ..0 ) . each do | index |
105
+ rules_array . delete_at ( index ) if rules_array [ index ] . empty?
106
+ end
104
107
end
105
- if dup_rules . any?
106
- $stderr. puts "WARN: Ignoring unsupported matching rules #{ dup_rules } for path #{ path } "
108
+
109
+ if @matching_rules . any?
110
+ @matching_rules . each do | path , rules_hash |
111
+ rules_hash . each do | key , value |
112
+ $stderr. puts "WARN: Ignoring unsupported #{ key } #{ value } for path #{ path } " if value . any?
113
+ end
114
+ end
107
115
end
108
116
end
117
+
118
+ def find_rule ( path , key )
119
+ @matching_rules [ path ] && @matching_rules [ path ] [ key ]
120
+ end
121
+
122
+ def log_used_rule path , key , value
123
+ @used_rules << [ path , key , value ]
124
+ end
109
125
end
110
126
end
111
127
end
0 commit comments