Skip to content

Commit

Permalink
chore(pact-models): Upgraded rand_regex to 0.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Nov 12, 2024
1 parent 04f5d1a commit 234cbaf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
16 changes: 13 additions & 3 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions rust/pact_models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ maplit = "1.0.2"
mime = "0.3.17"
nom = "7.1.3"
rand = "0.8.5"
rand_regex = "0.15.1"
rand_regex = "0.17.0"
regex = "1.8.4"
regex-syntax = "0.6.27"
regex-syntax = "0.8.5"
semver = "1.0.17"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
Expand Down
13 changes: 4 additions & 9 deletions rust/pact_models/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,10 +1069,10 @@ impl GenerateValue<Value> for Generator {
Generator::RandomString(size) => Ok(json!(generate_ascii_string(*size as usize))),
Generator::Regex(ref regex) => {
let mut parser = regex_syntax::ParserBuilder::new().unicode(false).build();
match parser.parse(regex) {
match parser.parse(strip_anchors(regex)) {
Ok(hir) => {
match rand_regex::Regex::with_hir(hir, 20) {
Ok(gen) => Ok(json!(rand::thread_rng().sample::<String, _>(gen))),
Ok(gen) => Ok(json!(thread_rng().sample::<String, _>(gen))),
Err(err) => {
warn!("Failed to generate a value from regular expression - {}", err);
Err(anyhow!("Failed to generate a value from regular expression - {}", err))
Expand Down Expand Up @@ -1362,7 +1362,6 @@ mod tests {
use pretty_assertions::assert_eq;
use test_log::test;

use crate::contain;
use crate::generators::Generator::{RandomDecimal, RandomInt, Regex};

use super::*;
Expand Down Expand Up @@ -2103,14 +2102,10 @@ mod tests {
let generator = Generator::Regex(r"^\/api\/families\/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$".into());

let generated = generator.generate_value(&"".to_string(), &hashmap!{}, &NoopVariantMatcher.boxed());
let err = generated.unwrap_err().to_string();
expect!(&err).to(contain("'^\\/api\\/families\\/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$' is not a valid regular expression"));
expect!(&err).to(contain("error: unrecognized escape sequence"));
assert_that!(generated.unwrap(), matches_regex(r"\/api\/families\/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}"));

let generated = generator.generate_value(&json!(""), &hashmap!{}, &NoopVariantMatcher.boxed());
let err = generated.unwrap_err().to_string();
expect!(&err).to(contain("'^\\/api\\/families\\/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$' is not a valid regular expression"));
expect!(&err).to(contain("error: unrecognized escape sequence"));
assert_that!(generated.unwrap().as_str().unwrap(), matches_regex(r"\/api\/families\/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}"));
}

#[test]
Expand Down

0 comments on commit 234cbaf

Please sign in to comment.