From 5d38945a83231c98265e29284b490901e318b413 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 6 Sep 2024 11:13:43 +0700 Subject: [PATCH 1/2] fix: Fix can't match array of numbers --- rust/pact_matching/src/matchingrules.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/pact_matching/src/matchingrules.rs b/rust/pact_matching/src/matchingrules.rs index c1bb2eb0a..b9ded906a 100644 --- a/rust/pact_matching/src/matchingrules.rs +++ b/rust/pact_matching/src/matchingrules.rs @@ -79,6 +79,7 @@ impl Matches<&[T]> for &[T] { MatchingRule::EachKey(_) => Ok(()), MatchingRule::EachValue(_) => Ok(()), MatchingRule::Values => Ok(()), + MatchingRule::Number | MatchingRule::Decimal | MatchingRule::Integer => Ok(()), _ => Err(anyhow!("Unable to match {} using {:?}", self.for_mismatch(), matcher)) }; debug!("Comparing '{:?}' to '{:?}' using {:?} -> {:?}", self, actual, matcher, result); From 61c68b8c71d2da52006a7c4e86a22dd6280ecd0b Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 6 Sep 2024 15:55:39 +0700 Subject: [PATCH 2/2] test: Test values using number, integer, decimal matchers --- rust/pact_matching/src/query.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/rust/pact_matching/src/query.rs b/rust/pact_matching/src/query.rs index ece09faff..514903792 100644 --- a/rust/pact_matching/src/query.rs +++ b/rust/pact_matching/src/query.rs @@ -164,6 +164,7 @@ mod tests { use expectest::prelude::*; use maplit::hashmap; use pact_models::matchingrules; + use rstest::rstest; use crate::{CoreMatchingContext, DiffConfig, MatchingRule}; @@ -183,4 +184,32 @@ mod tests { expect!(super::match_query_values("id", &expected, &actual, &context)) .to(be_ok()); } + + #[rstest] + #[case(["abc".to_string()], ["def".to_string()], MatchingRule::Number, false)] + #[case(["abc".to_string()], ["def".to_string()], MatchingRule::Integer, false)] + #[case(["abc".to_string()], ["def".to_string()], MatchingRule::Decimal, false)] + #[case(["100".to_string()], ["101".to_string()], MatchingRule::Number, true)] + #[case(["100".to_string()], ["101".to_string()], MatchingRule::Integer, true)] + #[case(["100".to_string()], ["101".to_string()], MatchingRule::Decimal, true)] + #[case(["100.01".to_string()], ["101.02".to_string()], MatchingRule::Number, true)] + #[case(["100.01".to_string()], ["101.02".to_string()], MatchingRule::Integer, false)] + #[case(["100.01".to_string()], ["101.02".to_string()], MatchingRule::Decimal, true)] + fn compare_values_with_number_matchers(#[case] expected: [String; 1], #[case] actual: [String; 1], #[case] matcher: MatchingRule, #[case] matched: bool) { + let rules = matchingrules! { + "query" => { "number" => [ matcher ] } + }; + let context = CoreMatchingContext::new( + DiffConfig::AllowUnexpectedKeys, + &rules.rules_for_category("query").unwrap_or_default(), + &hashmap!{} + ); + + let result = super::match_query_values("number", &expected, &actual, &context); + if matched { + expect!(result).to(be_ok()); + } else { + expect!(result).to(be_err()); + } + } }