Skip to content

Commit

Permalink
Merge pull request #464 from tienvx/fix-cant-compare-each-key-each-va…
Browse files Browse the repository at this point in the history
…lue-matching-rules

fix: Fix can't compare 2 eachKey matching rules, 2 eachValue matching rules
  • Loading branch information
rholshausen authored Nov 13, 2024
2 parents 9c3bfb6 + c279376 commit f3918b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 3 additions & 3 deletions rust/pact_consumer/src/patterns/special_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ fn object_matching_test() {
MatchingRule::EachKey(MatchingRuleDefinition::new("key1".to_string(), ValueType::String,
MatchingRule::Regex("[a-z]{3}[0-9]".to_string()), None)),
MatchingRule::EachValue(MatchingRuleDefinition::new("\"value1\"".to_string(),
ValueType::Unknown, MatchingRule::Type, None))
ValueType::String, MatchingRule::Type, None))
]
}, rules);
}
Expand Down Expand Up @@ -786,7 +786,7 @@ fn each_value_is_pattern() {
matchable.extract_matching_rules(DocPath::root(), &mut rules);
expect!(rules).to(be_equal_to(matchingrules_list! {
"body"; "$" => [
MatchingRule::EachValue(MatchingRuleDefinition::new("100".to_string(), ValueType::String,
MatchingRule::EachValue(MatchingRuleDefinition::new("\"100\"".to_string(), ValueType::String,
MatchingRule::Regex("\\d+".to_string()), None))
]
}));
Expand Down Expand Up @@ -820,7 +820,7 @@ fn each_value_test() {
result.extract_matching_rules(DocPath::root(), &mut rules);
expect!(rules).to(be_equal_to(matchingrules_list! {
"body"; "$" => [
MatchingRule::EachValue(MatchingRuleDefinition::new("value1".to_string(), ValueType::Unknown,
MatchingRule::EachValue(MatchingRuleDefinition::new("\"value1\"".to_string(), ValueType::String,
MatchingRule::Regex("[a-z]{5}[0-9]".to_string()), None))
]
}));
Expand Down
2 changes: 1 addition & 1 deletion rust/pact_models/src/matchingrules/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,7 @@ mod test {
value_type: ValueType::Unknown,
rules: vec![
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 } )),
Either::Left(MatchingRule::EachValue(MatchingRuleDefinition { value: "".to_string(), value_type: ValueType::Unknown, rules: vec![Either::Left(MatchingRule::Type)], generator: None } ))
Either::Left(MatchingRule::EachValue(MatchingRuleDefinition { value: "".to_string(), value_type: ValueType::String, rules: vec![Either::Left(MatchingRule::Type)], generator: None } ))
],
generator: None
}));
Expand Down
22 changes: 21 additions & 1 deletion rust/pact_models/src/matchingrules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ impl PartialEq for MatchingRule {
(MatchingRule::Include(str1), MatchingRule::Include(str2)) => str1 == str2,
(MatchingRule::ContentType(str1), MatchingRule::ContentType(str2)) => str1 == str2,
(MatchingRule::ArrayContains(variants1), MatchingRule::ArrayContains(variants2)) => variants1 == variants2,
(MatchingRule::EachKey(definition1), MatchingRule::EachKey(definition2)) => definition1 == definition2,
(MatchingRule::EachValue(definition1), MatchingRule::EachValue(definition2)) => definition1 == definition2,
_ => mem::discriminant(self) == mem::discriminant(other)
}
}
Expand Down Expand Up @@ -2158,7 +2160,7 @@ mod tests {
]
});
expect!(MatchingRule::from_json(&json)).to(be_ok().value(
MatchingRule::EachValue(MatchingRuleDefinition::new("{\"price\": 1.23}".to_string(),
MatchingRule::EachValue(MatchingRuleDefinition::new("{\"price\":1.23}".to_string(),
ValueType::Unknown, MatchingRule::Decimal, None)))
);
}
Expand Down Expand Up @@ -2633,4 +2635,22 @@ mod tests {
}
)
}

#[test]
#[should_panic]
fn each_value_matching_rule_comparation_test() {
assert_eq!(
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachValue(MatchingRuleDefinition::new("[\"string value\"]".to_string(), ValueType::Unknown, MatchingRule::Type, None))]},
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachValue(MatchingRuleDefinition::new("[\"something else\"]".to_string(), ValueType::Unknown, MatchingRule::Type, None))]}
)
}

#[test]
#[should_panic]
fn each_key_matching_rule_comparation_test() {
assert_eq!(
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachKey(MatchingRuleDefinition::new("a_key".to_string(), ValueType::Unknown, MatchingRule::Type, None))]},
matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachKey(MatchingRuleDefinition::new("another_key".to_string(), ValueType::Unknown, MatchingRule::Type, None))]}
)
}
}

0 comments on commit f3918b6

Please sign in to comment.