#![allow(missing_docs)]
#[test]
fn test_std_010_is_match_basic() {
let result = ruchy::stdlib::regex::is_match(r"\d+", "123");
assert!(result.is_ok(), "is_match should succeed");
assert!(result.unwrap(), "Should match digits");
}
#[test]
fn test_std_010_is_match_no_match() {
let result = ruchy::stdlib::regex::is_match(r"\d+", "abc");
assert!(result.is_ok(), "is_match should succeed");
assert!(!result.unwrap(), "Should not match");
}
#[test]
fn test_std_010_is_match_invalid_pattern() {
let result = ruchy::stdlib::regex::is_match(r"[", "test");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_find_first_basic() {
let result = ruchy::stdlib::regex::find_first(r"\d+", "abc 123 def 456");
assert!(result.is_ok(), "find_first should succeed");
assert_eq!(result.unwrap(), Some("123".to_string()));
}
#[test]
fn test_std_010_find_first_no_match() {
let result = ruchy::stdlib::regex::find_first(r"\d+", "abc");
assert!(result.is_ok(), "find_first should succeed");
assert_eq!(result.unwrap(), None);
}
#[test]
fn test_std_010_find_first_invalid() {
let result = ruchy::stdlib::regex::find_first(r"[", "test");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_find_all_basic() {
let result = ruchy::stdlib::regex::find_all(r"\d+", "abc 123 def 456");
assert!(result.is_ok(), "find_all should succeed");
let matches = result.unwrap();
assert_eq!(matches.len(), 2);
assert_eq!(matches[0], "123");
assert_eq!(matches[1], "456");
}
#[test]
fn test_std_010_find_all_empty() {
let result = ruchy::stdlib::regex::find_all(r"\d+", "abc");
assert!(result.is_ok(), "find_all should succeed");
assert!(result.unwrap().is_empty(), "Should return empty vector");
}
#[test]
fn test_std_010_find_all_invalid() {
let result = ruchy::stdlib::regex::find_all(r"[", "test");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_replace_first_basic() {
let result = ruchy::stdlib::regex::replace_first(r"\d+", "abc 123 def 456", "X");
assert!(result.is_ok(), "replace_first should succeed");
assert_eq!(result.unwrap(), "abc X def 456");
}
#[test]
fn test_std_010_replace_first_no_match() {
let result = ruchy::stdlib::regex::replace_first(r"\d+", "abc", "X");
assert!(result.is_ok(), "replace_first should succeed");
assert_eq!(result.unwrap(), "abc");
}
#[test]
fn test_std_010_replace_first_invalid() {
let result = ruchy::stdlib::regex::replace_first(r"[", "test", "X");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_replace_all_basic() {
let result = ruchy::stdlib::regex::replace_all(r"\d+", "abc 123 def 456", "X");
assert!(result.is_ok(), "replace_all should succeed");
assert_eq!(result.unwrap(), "abc X def X");
}
#[test]
fn test_std_010_replace_all_no_match() {
let result = ruchy::stdlib::regex::replace_all(r"\d+", "abc", "X");
assert!(result.is_ok(), "replace_all should succeed");
assert_eq!(result.unwrap(), "abc");
}
#[test]
fn test_std_010_replace_all_invalid() {
let result = ruchy::stdlib::regex::replace_all(r"[", "test", "X");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_split_basic() {
let result = ruchy::stdlib::regex::split(r"\s+", "hello world rust");
assert!(result.is_ok(), "split should succeed");
let parts = result.unwrap();
assert_eq!(parts.len(), 3);
assert_eq!(parts, vec!["hello", "world", "rust"]);
}
#[test]
fn test_std_010_split_no_match() {
let result = ruchy::stdlib::regex::split(r"\d+", "hello");
assert!(result.is_ok(), "split should succeed");
assert_eq!(result.unwrap(), vec!["hello"]);
}
#[test]
fn test_std_010_split_invalid() {
let result = ruchy::stdlib::regex::split(r"[", "test");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_capture_first_basic() {
let result = ruchy::stdlib::regex::capture_first(r"(\w+)@(\w+)", "user@example.com");
assert!(result.is_ok(), "capture_first should succeed");
let captures = result.unwrap();
assert!(captures.is_some());
let caps = captures.unwrap();
assert_eq!(caps.len(), 3); assert_eq!(caps[0], "user@example");
assert_eq!(caps[1], "user");
assert_eq!(caps[2], "example");
}
#[test]
fn test_std_010_capture_first_no_match() {
let result = ruchy::stdlib::regex::capture_first(r"(\d+)", "abc");
assert!(result.is_ok(), "capture_first should succeed");
assert_eq!(result.unwrap(), None);
}
#[test]
fn test_std_010_capture_first_invalid() {
let result = ruchy::stdlib::regex::capture_first(r"[", "test");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_capture_all_basic() {
let result = ruchy::stdlib::regex::capture_all(r"(\w+):(\d+)", "name:123 age:45");
assert!(result.is_ok(), "capture_all should succeed");
let all_captures = result.unwrap();
assert_eq!(all_captures.len(), 2);
assert_eq!(all_captures[0], vec!["name:123", "name", "123"]);
assert_eq!(all_captures[1], vec!["age:45", "age", "45"]);
}
#[test]
fn test_std_010_capture_all_empty() {
let result = ruchy::stdlib::regex::capture_all(r"(\d+)", "abc");
assert!(result.is_ok(), "capture_all should succeed");
assert!(result.unwrap().is_empty(), "Should return empty vector");
}
#[test]
fn test_std_010_capture_all_invalid() {
let result = ruchy::stdlib::regex::capture_all(r"[", "test");
assert!(result.is_err(), "Invalid pattern should return error");
}
#[test]
fn test_std_010_is_valid_pattern_valid() {
let result = ruchy::stdlib::regex::is_valid_pattern(r"\d+");
assert!(result.is_ok(), "is_valid_pattern should succeed");
assert!(result.unwrap(), "Should be valid pattern");
}
#[test]
fn test_std_010_is_valid_pattern_invalid() {
let result = ruchy::stdlib::regex::is_valid_pattern(r"[");
assert!(result.is_ok(), "is_valid_pattern should succeed");
assert!(!result.unwrap(), "Should be invalid pattern");
}
#[test]
fn test_std_010_escape_basic() {
let result = ruchy::stdlib::regex::escape("a.b*c?");
assert!(result.is_ok(), "escape should succeed");
assert_eq!(result.unwrap(), r"a\.b\*c\?");
}
#[test]
fn test_std_010_escape_empty() {
let result = ruchy::stdlib::regex::escape("");
assert!(result.is_ok(), "escape should succeed");
assert_eq!(result.unwrap(), "");
}
#[cfg(test)]
mod property_tests {
use proptest::prelude::*;
proptest! {
#![proptest_config(ProptestConfig::with_cases(20))]
#[test]
fn test_std_010_regex_never_panics(text: String) {
let _ = ruchy::stdlib::regex::is_match(r"\d+", &text);
let _ = ruchy::stdlib::regex::find_first(r"\w+", &text);
let _ = ruchy::stdlib::regex::find_all(r"\w+", &text);
let _ = ruchy::stdlib::regex::split(r"\s+", &text);
}
#[test]
fn test_std_010_escape_roundtrip(text: String) {
if let Ok(escaped) = ruchy::stdlib::regex::escape(&text) {
let result = ruchy::stdlib::regex::is_match(&escaped, &text);
prop_assert!(result.is_ok(), "Escaped pattern should be valid");
}
}
#[test]
fn test_std_010_invalid_pattern_consistent(pattern: String) {
let result = ruchy::stdlib::regex::is_match(&pattern, "test");
prop_assert!(result.is_ok() || result.is_err(), "Should not panic");
}
}
}