diff --git a/Cargo.lock b/Cargo.lock index 84eca69..603da24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,9 +31,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "base-traits" -version = "0.0.8" +version = "0.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "556031355dea4fc81a56aa4850032c9b433c926e7296c30780f63ece19ecf082" +checksum = "a9c676f0beeccff874a11e4853c9b6f254cdc64ab1135a63b2968602820067e5" [[package]] name = "bumpalo" @@ -423,6 +423,7 @@ dependencies = [ name = "shwild" version = "0.1.2" dependencies = [ + "base-traits", "collect-rs", "criterion", "regex", diff --git a/Cargo.toml b/Cargo.toml index 1354bc8..c49d00e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,7 +95,13 @@ test-regex = [ [dependencies] -collect-rs = { version = "0.2", optional = true } +base-traits = "*" + +# base-traits = { version = "0", default-features = false, features = [ +# "implement-ASI64-for-built_ins", +# ]} +collect-rs = { version = "0.2", optional = true, default-features = false, features = [ +]} regex = { version = "1.11", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 3812d38..c09760c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ use std::{ error as std_error, fmt as std_fmt, + matches as std_matches, result as std_result, }; @@ -1480,7 +1481,7 @@ impl CompiledMatcher { let mut num_bytes = 0; for c in pattern.chars() { - debug_assert!(continuum_prior.is_none() || matches!(state, ParseState::InNotRange | ParseState::InRange)); + debug_assert!(continuum_prior.is_none() || std_matches!(state, ParseState::InNotRange | ParseState::InRange)); if escaped { match c { @@ -1501,7 +1502,7 @@ impl CompiledMatcher { escaped = false; - if matches!(state, ParseState::None) { + if std_matches!(state, ParseState::None) { state = ParseState::InLiteral; } } else { @@ -1575,7 +1576,7 @@ impl CompiledMatcher { }, }; - minimum_required = if matches!(state, ParseState::InRange) { + minimum_required = if std_matches!(state, ParseState::InRange) { matchers.prepend_Range(character_range, flags, minimum_required) } else { matchers.prepend_NotRange(character_range, flags, minimum_required) @@ -1883,13 +1884,62 @@ macro_rules! shwild_matches { }; } +#[macro_export] +macro_rules! assert_shwild_matches { + ($expected_pattern:expr, $actual:expr) => { + + assert_shwild_matches!($expected_pattern, $actual, 0i64); + }; + ($expected_pattern:expr, $actual:expr, $flags:expr) => { + let expected_pattern : &str = &$expected_pattern; + let actual = &$actual; + let flags : &dyn base_traits::AsI64 = &$flags; + let flags = flags.as_i64(); + + match $crate::matches(expected_pattern, actual, flags) { + Err(e) => { + panic!("could not evaluate actual value due to a failure in the parsing of expected pattern '{}': {}", expected_pattern, e); + }, + Ok(b) => { + assert!(b, "assertion failed: actual value '{}' does not match the expected pattern '{}'", actual, expected_pattern); + } + } + }; +} + +#[macro_export] +macro_rules! assert_shwild_not_matches { + ($expected_pattern:expr, $actual:expr) => { + + assert_shwild_not_matches!($expected_pattern, $actual, 0i64); + }; + ($expected_pattern:expr, $actual:expr, $flags:expr) => { + let expected_pattern : &str = &$expected_pattern; + let actual = &$actual; + let flags : &dyn base_traits::AsI64 = &$flags; + let flags = flags.as_i64(); + + match $crate::matches(expected_pattern, actual, flags) { + Err(e) => { + panic!("could not evaluate actual value due to a failure in the parsing of expected pattern '{}': {}", expected_pattern, e); + }, + Ok(b) => { + assert!(!b, "assertion failed: actual value '{}' match unexpectedly with the pattern '{}'", actual, expected_pattern); + } + } + }; +} + #[cfg(test)] mod tests { #![allow(non_snake_case)] - use crate as shwild; - use crate::shwild_matches; + use crate::{ + assert_shwild_matches, + self as shwild, + shwild_matches, + }; mod TEST_CompiledMatcher_PARSING { @@ -3307,6 +3357,40 @@ mod tests { } } } + + + mod TEST_ASSERTION_MATCHES { + #![allow(non_snake_case)] + + use super::*; + + + #[test] + fn TEST_assert_shwild_matches_1() { + assert_shwild_matches!("[a-d]", "a"); + assert_shwild_matches!("[a-d]", "b"); + assert_shwild_matches!("[a-d]", "c"); + + assert_shwild_matches!("?", "a"); + assert_shwild_matches!("?", "z"); + } + + #[test] + #[should_panic(expected = "could not evaluate actual value due to a failure in the parsing of expected pattern '[a-d': pattern syntax error (at 0:4): incomplete range")] + fn TEST_assert_shwild_matches_2() { + assert_shwild_matches!("[a-d", "a"); + } + + #[test] + fn TEST_assert_shwild_not_matches_1() { + assert_shwild_not_matches!("[a-d]", "e"); + assert_shwild_not_matches!("[a-d]", "f"); + assert_shwild_not_matches!("[a-d]", "D"); + + assert_shwild_not_matches!("??", "a"); + assert_shwild_not_matches!("??", "z"); + } + } }