Skip to content

Commit

Permalink
Merge pull request #463 from tienvx/fix-cant-match-array-of-numbers
Browse files Browse the repository at this point in the history
fix: Fix can't match array of numbers
  • Loading branch information
rholshausen authored Nov 13, 2024
2 parents 420c654 + 61c68b8 commit 9c3bfb6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions rust/pact_matching/src/matchingrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl <T: Debug + Display + PartialEq + Clone> 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);
Expand Down
29 changes: 29 additions & 0 deletions rust/pact_matching/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ mod tests {
use expectest::prelude::*;
use maplit::hashmap;
use pact_models::matchingrules;
use rstest::rstest;

use crate::{CoreMatchingContext, DiffConfig, MatchingRule};

Expand All @@ -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());
}
}
}

0 comments on commit 9c3bfb6

Please sign in to comment.