#![allow(clippy::unwrap_used, clippy::expect_used)]
use kaish_kernel::Kernel;
#[tokio::test]
async fn regex_match_valid_matching_pattern_exits_zero() {
let kernel = Kernel::transient().unwrap();
let result = kernel.execute(r#"[[ "abc" =~ "a.c" ]]"#).await.expect("should succeed");
assert_eq!(result.code, 0, "matching regex should exit 0; stderr: {:?}", result.err);
assert!(result.err.is_empty(), "no error on a valid match: {:?}", result.err);
}
#[tokio::test]
async fn regex_match_valid_nonmatching_pattern_exits_one() {
let kernel = Kernel::transient().unwrap();
let result = kernel.execute(r#"[[ "abc" =~ "xyz" ]]"#).await.expect("should succeed");
assert_eq!(result.code, 1, "non-matching regex should exit 1; stderr: {:?}", result.err);
assert!(result.err.is_empty(), "no error on a valid non-match: {:?}", result.err);
}
#[tokio::test]
async fn regex_match_uncompilable_pattern_is_loud_error() {
let kernel = Kernel::transient().unwrap();
let result = kernel.execute(r#"[[ "abc" =~ "(" ]]"#).await.expect("a fault is a result");
assert_eq!(result.code, 2, "uncompilable regex is a fault, not a false reading");
assert_ne!(result.code, 1, "it must not collapse into the non-match code");
let msg = result.err.to_lowercase();
assert!(
msg.contains("regex") || msg.contains("pattern") || msg.contains("paren"),
"error message must name the regex problem: {:?}", result.err
);
}
#[tokio::test]
async fn regex_notmatch_uncompilable_pattern_is_loud_error() {
let kernel = Kernel::transient().unwrap();
let result = kernel.execute(r#"[[ "abc" !~ "(" ]]"#).await.expect("a fault is a result");
assert_eq!(result.code, 2, "uncompilable regex in !~ is a fault, not a true reading");
assert_ne!(result.code, 0, "it must not collapse into the no-match-is-true code");
let msg = result.err.to_lowercase();
assert!(
msg.contains("regex") || msg.contains("pattern") || msg.contains("paren"),
"error message must name the regex problem: {:?}", result.err
);
}
#[tokio::test]
async fn regex_match_unclosed_bracket_is_loud_error() {
let kernel = Kernel::transient().unwrap();
let result = kernel.execute(r#"[[ "abc" =~ "[" ]]"#).await.expect("a fault is a result");
assert_eq!(result.code, 2, "unclosed bracket is a fault, not a false reading");
assert_ne!(result.code, 1, "it must not collapse into the non-match code");
let msg = result.err.to_lowercase();
assert!(
msg.contains("regex") || msg.contains("pattern") || msg.contains("bracket") || msg.contains("class"),
"error message must name the regex problem: {:?}", result.err
);
}