Skip to content

Commit

Permalink
test: Add test for eachKey and eachValue matching rules comparation
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Sep 6, 2024
1 parent 691ddae commit c279376
Show file tree
Hide file tree
Showing 3 changed files with 23 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
20 changes: 19 additions & 1 deletion rust/pact_models/src/matchingrules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2160,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 @@ -2635,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 c279376

Please sign in to comment.