Skip to content

Commit f3918b6

Browse files
authored
Merge pull request #464 from tienvx/fix-cant-compare-each-key-each-value-matching-rules
fix: Fix can't compare 2 eachKey matching rules, 2 eachValue matching rules
2 parents 9c3bfb6 + c279376 commit f3918b6

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

rust/pact_consumer/src/patterns/special_rules.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ fn object_matching_test() {
567567
MatchingRule::EachKey(MatchingRuleDefinition::new("key1".to_string(), ValueType::String,
568568
MatchingRule::Regex("[a-z]{3}[0-9]".to_string()), None)),
569569
MatchingRule::EachValue(MatchingRuleDefinition::new("\"value1\"".to_string(),
570-
ValueType::Unknown, MatchingRule::Type, None))
570+
ValueType::String, MatchingRule::Type, None))
571571
]
572572
}, rules);
573573
}
@@ -786,7 +786,7 @@ fn each_value_is_pattern() {
786786
matchable.extract_matching_rules(DocPath::root(), &mut rules);
787787
expect!(rules).to(be_equal_to(matchingrules_list! {
788788
"body"; "$" => [
789-
MatchingRule::EachValue(MatchingRuleDefinition::new("100".to_string(), ValueType::String,
789+
MatchingRule::EachValue(MatchingRuleDefinition::new("\"100\"".to_string(), ValueType::String,
790790
MatchingRule::Regex("\\d+".to_string()), None))
791791
]
792792
}));
@@ -820,7 +820,7 @@ fn each_value_test() {
820820
result.extract_matching_rules(DocPath::root(), &mut rules);
821821
expect!(rules).to(be_equal_to(matchingrules_list! {
822822
"body"; "$" => [
823-
MatchingRule::EachValue(MatchingRuleDefinition::new("value1".to_string(), ValueType::Unknown,
823+
MatchingRule::EachValue(MatchingRuleDefinition::new("\"value1\"".to_string(), ValueType::String,
824824
MatchingRule::Regex("[a-z]{5}[0-9]".to_string()), None))
825825
]
826826
}));

rust/pact_models/src/matchingrules/expressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ mod test {
16761676
value_type: ValueType::Unknown,
16771677
rules: vec![
16781678
Either::Left(MatchingRule::EachKey(MatchingRuleDefinition { value: "$.test.one".to_string(), value_type: ValueType::String, rules: vec![Either::Left(MatchingRule::Regex("\\$(\\.\\w+)+".to_string()))], generator: None } )),
1679-
Either::Left(MatchingRule::EachValue(MatchingRuleDefinition { value: "".to_string(), value_type: ValueType::Unknown, rules: vec![Either::Left(MatchingRule::Type)], generator: None } ))
1679+
Either::Left(MatchingRule::EachValue(MatchingRuleDefinition { value: "".to_string(), value_type: ValueType::String, rules: vec![Either::Left(MatchingRule::Type)], generator: None } ))
16801680
],
16811681
generator: None
16821682
}));

rust/pact_models/src/matchingrules/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ impl PartialEq for MatchingRule {
537537
(MatchingRule::Include(str1), MatchingRule::Include(str2)) => str1 == str2,
538538
(MatchingRule::ContentType(str1), MatchingRule::ContentType(str2)) => str1 == str2,
539539
(MatchingRule::ArrayContains(variants1), MatchingRule::ArrayContains(variants2)) => variants1 == variants2,
540+
(MatchingRule::EachKey(definition1), MatchingRule::EachKey(definition2)) => definition1 == definition2,
541+
(MatchingRule::EachValue(definition1), MatchingRule::EachValue(definition2)) => definition1 == definition2,
540542
_ => mem::discriminant(self) == mem::discriminant(other)
541543
}
542544
}
@@ -2158,7 +2160,7 @@ mod tests {
21582160
]
21592161
});
21602162
expect!(MatchingRule::from_json(&json)).to(be_ok().value(
2161-
MatchingRule::EachValue(MatchingRuleDefinition::new("{\"price\": 1.23}".to_string(),
2163+
MatchingRule::EachValue(MatchingRuleDefinition::new("{\"price\":1.23}".to_string(),
21622164
ValueType::Unknown, MatchingRule::Decimal, None)))
21632165
);
21642166
}
@@ -2633,4 +2635,22 @@ mod tests {
26332635
}
26342636
)
26352637
}
2638+
2639+
#[test]
2640+
#[should_panic]
2641+
fn each_value_matching_rule_comparation_test() {
2642+
assert_eq!(
2643+
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachValue(MatchingRuleDefinition::new("[\"string value\"]".to_string(), ValueType::Unknown, MatchingRule::Type, None))]},
2644+
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachValue(MatchingRuleDefinition::new("[\"something else\"]".to_string(), ValueType::Unknown, MatchingRule::Type, None))]}
2645+
)
2646+
}
2647+
2648+
#[test]
2649+
#[should_panic]
2650+
fn each_key_matching_rule_comparation_test() {
2651+
assert_eq!(
2652+
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachKey(MatchingRuleDefinition::new("a_key".to_string(), ValueType::Unknown, MatchingRule::Type, None))]},
2653+
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachKey(MatchingRuleDefinition::new("another_key".to_string(), ValueType::Unknown, MatchingRule::Type, None))]}
2654+
)
2655+
}
26362656
}

0 commit comments

Comments
 (0)