2019-08-29 07:58:01 +00:00
|
|
|
#![cfg(feature = "regex_support")]
|
|
|
|
|
|
|
|
use evalexpr::*;
|
2021-06-02 15:33:27 +00:00
|
|
|
use std::panic::panic_any;
|
2019-08-29 07:58:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_regex_functions() {
|
|
|
|
assert_eq!(
|
|
|
|
eval("str::regex_matches(\"foobar\", \"[ob]{3}\")"),
|
|
|
|
Ok(Value::Boolean(true))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
eval("str::regex_matches(\"gazonk\", \"[ob]{3}\")"),
|
|
|
|
Ok(Value::Boolean(false))
|
|
|
|
);
|
|
|
|
match eval("str::regex_matches(\"foo\", \"[\")") {
|
|
|
|
Err(EvalexprError::InvalidRegex { regex, message }) => {
|
|
|
|
assert_eq!(regex, "[");
|
|
|
|
assert!(message.contains("unclosed character class"));
|
|
|
|
},
|
2021-06-02 15:33:27 +00:00
|
|
|
v => panic_any!(v),
|
2019-08-29 07:58:01 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
eval("str::regex_replace(\"foobar\", \".*?(o+)\", \"b$1\")"),
|
|
|
|
Ok(Value::String("boobar".to_owned()))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
eval("str::regex_replace(\"foobar\", \".*?(i+)\", \"b$1\")"),
|
|
|
|
Ok(Value::String("foobar".to_owned()))
|
|
|
|
);
|
2019-08-29 08:10:54 +00:00
|
|
|
}
|