use crate::run;
#[test]
fn test_builtin_substr() {
assert_eq!(run("substr('hello', 0, 2)").expect("run").to_string(), "he");
assert_eq!(run("substr('hello', 1)").expect("run").to_string(), "ello");
assert_eq!(run("substr('hello', -2)").expect("run").to_string(), "lo");
assert_eq!(
run("substr('hello', -3, 2)").expect("run").to_string(),
"ll"
);
assert_eq!(
run("substr('hello', 1, -1)").expect("run").to_string(),
"ell"
);
assert_eq!(run("substr('hi', 10)").expect("run").to_string(), "");
}
#[test]
fn test_builtin_split() {
assert_eq!(
run("join(':', split(',', 'a,b,c'))")
.expect("run")
.to_string(),
"a:b:c"
);
assert_eq!(
run("join(':', split('\\\\s+', 'a b c'))")
.expect("run")
.to_string(),
"a:b:c"
);
assert_eq!(run("scalar split(',', '')").expect("run").to_int(), 1);
assert_eq!(
run("join(':', split(',', 'a,b,c,d', 2))")
.expect("run")
.to_string(),
"a:b,c,d"
);
}
#[test]
fn test_builtin_join() {
assert_eq!(run("join('-', 1, 2, 3)").expect("run").to_string(), "1-2-3");
assert_eq!(
run("join('-', (1, 2), 3)").expect("run").to_string(),
"1-2-3"
);
let res = run("join('-', [1, 2])").expect("run").to_string();
assert!(res.contains("ARRAY("));
}
#[test]
fn test_builtin_index_rindex() {
assert_eq!(run("index('hello', 'e')").expect("run").to_int(), 1);
assert_eq!(run("index('hello', 'x')").expect("run").to_int(), -1);
assert_eq!(run("rindex('hello', 'l')").expect("run").to_int(), 3);
}
#[test]
fn test_builtin_sprintf() {
assert_eq!(run("sprintf('%03d', 5)").expect("run").to_string(), "005");
assert_eq!(
run("sprintf('%.2f', 3.14159)").expect("run").to_string(),
"3.14"
);
assert_eq!(
run("sprintf('%s-%d', 'hi', 42)").expect("run").to_string(),
"hi-42"
);
}
#[test]
fn test_builtin_hex_oct() {
assert_eq!(run("hex('0xFF')").expect("run").to_int(), 255);
assert_eq!(run("hex('FF')").expect("run").to_int(), 255);
assert_eq!(run("oct('077')").expect("run").to_int(), 63);
assert_eq!(run("oct('77')").expect("run").to_int(), 63);
assert_eq!(run("oct('0xff')").expect("run").to_int(), 255);
}