feat: regex related builtins
This commit is contained in:
316
nix-js/tests/regex.rs
Normal file
316
nix-js/tests/regex.rs
Normal file
@@ -0,0 +1,316 @@
|
||||
mod utils;
|
||||
|
||||
use nix_js::value::{List, Value};
|
||||
use utils::eval;
|
||||
|
||||
#[test]
|
||||
fn test_match_exact_full_string() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "foobar" "foobar""#),
|
||||
Value::List(List::new(vec![]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_partial_returns_null() {
|
||||
assert_eq!(eval(r#"builtins.match "foo" "foobar""#), Value::Null);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_with_capture_groups() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "(.*)\\.nix" "foobar.nix""#),
|
||||
Value::List(List::new(vec![Value::String("foobar".into())]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_multiple_capture_groups() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "((.*)/)?([^/]*)\\.nix" "foobar.nix""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::Null,
|
||||
Value::Null,
|
||||
Value::String("foobar".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_with_path() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "((.*)/)?([^/]*)\\.nix" "/path/to/foobar.nix""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("/path/to/".into()),
|
||||
Value::String("/path/to".into()),
|
||||
Value::String("foobar".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_posix_space_class() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "[[:space:]]+([^[:space:]]+)[[:space:]]+" " foo ""#),
|
||||
Value::List(List::new(vec![Value::String("foo".into())]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_posix_upper_class() {
|
||||
assert_eq!(eval(r#"builtins.match "[[:space:]]+([[:upper:]]+)[[:space:]]+" " foo ""#), Value::Null);
|
||||
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "[[:space:]]+([[:upper:]]+)[[:space:]]+" " FOO ""#),
|
||||
Value::List(List::new(vec![Value::String("FOO".into())]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_quantifiers() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "fo*" "f""#),
|
||||
Value::List(List::new(vec![]))
|
||||
);
|
||||
|
||||
assert_eq!(eval(r#"builtins.match "fo+" "f""#), Value::Null);
|
||||
|
||||
assert_eq!(
|
||||
eval(r#"builtins.match "fo{1,2}" "foo""#),
|
||||
Value::List(List::new(vec![]))
|
||||
);
|
||||
|
||||
assert_eq!(eval(r#"builtins.match "fo{1,2}" "fooo""#), Value::Null);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_non_capturing() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "foobar" "foobar""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("".into()),
|
||||
Value::List(List::new(vec![])),
|
||||
Value::String("".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_no_match() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "fo+" "f""#),
|
||||
Value::List(List::new(vec![Value::String("f".into())]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_with_capture_group() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "(fo*)" "foobar""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("".into()),
|
||||
Value::List(List::new(vec![Value::String("foo".into())])),
|
||||
Value::String("bar".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_multiple_matches() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "(b)" "foobarbaz""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("foo".into()),
|
||||
Value::List(List::new(vec![Value::String("b".into())])),
|
||||
Value::String("ar".into()),
|
||||
Value::List(List::new(vec![Value::String("b".into())])),
|
||||
Value::String("az".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_with_multiple_groups() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "(f)(o*)" "foo""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("".into()),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("f".into()),
|
||||
Value::String("oo".into())
|
||||
])),
|
||||
Value::String("".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_with_optional_groups() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "(a)|(c)" "abc""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("".into()),
|
||||
Value::List(List::new(vec![Value::String("a".into()), Value::Null])),
|
||||
Value::String("b".into()),
|
||||
Value::List(List::new(vec![Value::Null, Value::String("c".into())])),
|
||||
Value::String("".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_greedy_matching() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "(o+)" "oooofoooo""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("".into()),
|
||||
Value::List(List::new(vec![Value::String("oooo".into())])),
|
||||
Value::String("f".into()),
|
||||
Value::List(List::new(vec![Value::String("oooo".into())])),
|
||||
Value::String("".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_posix_classes() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.split "([[:upper:]]+)" " FOO ""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String(" ".into()),
|
||||
Value::List(List::new(vec![Value::String("FOO".into())])),
|
||||
Value::String(" ".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_basic() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings ["o"] ["a"] "foobar""#),
|
||||
Value::String("faabar".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_with_empty() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings ["o"] [""] "foobar""#),
|
||||
Value::String("fbar".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_multiple_patterns() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings ["oo" "a"] ["a" "oo"] "foobar""#),
|
||||
Value::String("faboor".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_first_match_wins() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings ["oo" "oo"] ["u" "i"] "foobar""#),
|
||||
Value::String("fubar".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_empty_pattern() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings [""] ["X"] "abc""#),
|
||||
Value::String("XaXbXcX".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_empty_pattern_empty_string() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings [""] ["X"] """#),
|
||||
Value::String("X".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_simple_char() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings ["-"] ["_"] "a-b""#),
|
||||
Value::String("a_b".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_longer_pattern() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.replaceStrings ["oo"] ["u"] "foobar""#),
|
||||
Value::String("fubar".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_different_lengths() {
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
eval(r#"builtins.replaceStrings ["a" "b"] ["x"] "test""#)
|
||||
});
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_version_simple() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.splitVersion "1.2.3""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("1".into()),
|
||||
Value::String("2".into()),
|
||||
Value::String("3".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_version_with_pre() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.splitVersion "2.3.0pre1234""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("2".into()),
|
||||
Value::String("3".into()),
|
||||
Value::String("0".into()),
|
||||
Value::String("pre".into()),
|
||||
Value::String("1234".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_version_with_letters() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.splitVersion "2.3a""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("2".into()),
|
||||
Value::String("3".into()),
|
||||
Value::String("a".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_version_with_dashes() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.splitVersion "2.3-beta1""#),
|
||||
Value::List(List::new(vec![
|
||||
Value::String("2".into()),
|
||||
Value::String("3".into()),
|
||||
Value::String("beta".into()),
|
||||
Value::String("1".into())
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_version_empty() {
|
||||
assert_eq!(
|
||||
eval(r#"builtins.splitVersion """#),
|
||||
Value::List(List::new(vec![]))
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user