320 lines
7.6 KiB
Rust
320 lines
7.6 KiB
Rust
mod utils;
|
|
|
|
use nix_js::value::{List, Value};
|
|
use utils::eval;
|
|
|
|
use crate::utils::eval_result;
|
|
|
|
#[test]
|
|
fn match_exact_full_string() {
|
|
assert_eq!(
|
|
eval(r#"builtins.match "foobar" "foobar""#),
|
|
Value::List(List::new(vec![]))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn match_partial_returns_null() {
|
|
assert_eq!(eval(r#"builtins.match "foo" "foobar""#), Value::Null);
|
|
}
|
|
|
|
#[test]
|
|
fn match_with_capture_groups() {
|
|
assert_eq!(
|
|
eval(r#"builtins.match "(.*)\\.nix" "foobar.nix""#),
|
|
Value::List(List::new(vec![Value::String("foobar".into())]))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn 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 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 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 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 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 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 split_no_match() {
|
|
assert_eq!(
|
|
eval(r#"builtins.split "fo+" "f""#),
|
|
Value::List(List::new(vec![Value::String("f".into())]))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn 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 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 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 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 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 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 replace_basic() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings ["o"] ["a"] "foobar""#),
|
|
Value::String("faabar".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_with_empty() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings ["o"] [""] "foobar""#),
|
|
Value::String("fbar".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_multiple_patterns() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings ["oo" "a"] ["a" "oo"] "foobar""#),
|
|
Value::String("faboor".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_first_match_wins() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings ["oo" "oo"] ["u" "i"] "foobar""#),
|
|
Value::String("fubar".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_empty_pattern() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings [""] ["X"] "abc""#),
|
|
Value::String("XaXbXcX".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_empty_pattern_empty_string() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings [""] ["X"] """#),
|
|
Value::String("X".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_simple_char() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings ["-"] ["_"] "a-b""#),
|
|
Value::String("a_b".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_longer_pattern() {
|
|
assert_eq!(
|
|
eval(r#"builtins.replaceStrings ["oo"] ["u"] "foobar""#),
|
|
Value::String("fubar".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn replace_different_lengths() {
|
|
let result = eval_result(r#"builtins.replaceStrings ["a" "b"] ["x"] "test""#);
|
|
assert!(result.is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn 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 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 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 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 split_version_empty() {
|
|
assert_eq!(
|
|
eval(r#"builtins.splitVersion """#),
|
|
Value::List(List::new(vec![]))
|
|
);
|
|
}
|