feat: 添加字符串工具函数(split, trim, substring, replace, contains, upper, lower, starts_with, ends_with)及相关测试用例
This commit is contained in:
@@ -1259,3 +1259,163 @@ fn error_forin_non_iterable() {
|
||||
});
|
||||
assert!(result.is_err(), "Expected error for for-in on non-iterable");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// String builtins
|
||||
// =========================================================================
|
||||
|
||||
#[test]
|
||||
fn eval_split_by_comma() {
|
||||
match eval_expr("split(\"a,b,c\", \",\")") {
|
||||
Value::Array(arr) => {
|
||||
let arr = arr.borrow();
|
||||
assert_eq!(arr.len(), 3);
|
||||
match &arr[0] { Value::String(s) => assert_eq!(s, "a"), v => panic!("{:?}", v) }
|
||||
}
|
||||
v => panic!("Expected Array, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_split_by_empty_delim_returns_chars() {
|
||||
match eval_expr("split(\"ab\", \"\")") {
|
||||
Value::Array(arr) => {
|
||||
let arr = arr.borrow();
|
||||
assert_eq!(arr.len(), 2);
|
||||
}
|
||||
v => panic!("Expected Array, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_split_no_match_returns_whole_string() {
|
||||
match eval_expr("split(\"hello\", \",\")") {
|
||||
Value::Array(arr) => {
|
||||
let arr = arr.borrow();
|
||||
assert_eq!(arr.len(), 1);
|
||||
}
|
||||
v => panic!("Expected Array, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_trim_spaces() {
|
||||
match eval_expr("trim(\" hello \")") {
|
||||
Value::String(s) => assert_eq!(s, "hello"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_trim_no_whitespace() {
|
||||
match eval_expr("trim(\"hello\")") {
|
||||
Value::String(s) => assert_eq!(s, "hello"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_substring_start() {
|
||||
match eval_expr("substring(\"hello\", 1)") {
|
||||
Value::String(s) => assert_eq!(s, "ello"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_substring_start_and_length() {
|
||||
match eval_expr("substring(\"hello\", 1, 3)") {
|
||||
Value::String(s) => assert_eq!(s, "ell"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_substring_start_beyond_length() {
|
||||
match eval_expr("substring(\"hi\", 10)") {
|
||||
Value::String(s) => assert_eq!(s, ""),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_replace_single() {
|
||||
match eval_expr("replace(\"hello world\", \"world\", \"aster\")") {
|
||||
Value::String(s) => assert_eq!(s, "hello aster"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_replace_multiple() {
|
||||
match eval_expr("replace(\"a,a,a\", \"a\", \"b\")") {
|
||||
Value::String(s) => assert_eq!(s, "b,b,b"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_replace_no_match() {
|
||||
match eval_expr("replace(\"hello\", \"x\", \"y\")") {
|
||||
Value::String(s) => assert_eq!(s, "hello"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_contains_true() {
|
||||
assert_bool(&eval_expr("contains(\"hello\", \"ell\")"), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_contains_false() {
|
||||
assert_bool(&eval_expr("contains(\"hello\", \"xyz\")"), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_upper() {
|
||||
match eval_expr("upper(\"hello\")") {
|
||||
Value::String(s) => assert_eq!(s, "HELLO"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_lower() {
|
||||
match eval_expr("lower(\"HELLO\")") {
|
||||
Value::String(s) => assert_eq!(s, "hello"),
|
||||
v => panic!("Expected String, got {:?}", v),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_starts_with_true() {
|
||||
assert_bool(&eval_expr("starts_with(\"hello\", \"he\")"), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_starts_with_false() {
|
||||
assert_bool(&eval_expr("starts_with(\"hello\", \"lo\")"), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_ends_with_true() {
|
||||
assert_bool(&eval_expr("ends_with(\"hello\", \"lo\")"), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_ends_with_false() {
|
||||
assert_bool(&eval_expr("ends_with(\"hello\", \"he\")"), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_split_wrong_arg_count() {
|
||||
let result = std::panic::catch_unwind(|| { eval_expr("split(\"a\")"); });
|
||||
assert!(result.is_err(), "Expected error for split with 1 arg");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_trim_non_string() {
|
||||
let result = std::panic::catch_unwind(|| { eval_expr("trim(42)"); });
|
||||
assert!(result.is_err(), "Expected error for trim(42)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user