use json_eval_rs::*;
use serde_json::json;
#[cfg(test)]
mod string_tests {
use super::*;
#[test]
fn test_string_concatenation() {
let mut engine = RLogic::new();
let data = json!({"first": "Hello", "last": "World"});
let logic_id = engine
.compile(&json!({"cat": [{"var": "first"}, " ", {"var": "last"}]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("Hello World"));
let logic_id = engine.compile(&json!({"cat": ["Value: ", 42]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("Value: 42"));
let logic_id = engine.compile(&json!({"cat": []})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(""));
}
#[test]
fn test_string_substr() {
let mut engine = RLogic::new();
let data = json!({"text": "Hello World"});
let logic_id = engine
.compile(&json!({"substr": [{"var": "text"}, 6, 5]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("World"));
let logic_id = engine
.compile(&json!({"substr": [{"var": "text"}, 0, 5]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("Hello"));
let logic_id = engine
.compile(&json!({"substr": [{"var": "text"}, 6]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("World"));
}
#[test]
fn test_string_length() {
let mut engine = RLogic::new();
let data = json!({
"array": [1, 2, 3, 4, 5],
"object": {"a": 1, "b": 2, "c": 3}
});
let logic_id = engine.compile(&json!({"length": "hello"})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(5));
let logic_id = engine.compile(&json!({"len": "hello world"})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(11));
let logic_id = engine
.compile(&json!({"length": {"var": "array"}}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(5));
let logic_id = engine
.compile(&json!({"length": {"var": "object"}}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(3));
}
#[test]
fn test_string_search() {
let mut engine = RLogic::new();
let data = json!({"text": "Hello World, hello universe"});
let logic_id = engine
.compile(&json!({"search": ["World", {"var": "text"}]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(7));
let logic_id = engine
.compile(&json!({"search": ["hello", {"var": "text"}, 8]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(14));
let logic_id = engine
.compile(&json!({"search": ["HELLO", {"var": "text"}]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(1));
let logic_id = engine
.compile(&json!({"search": ["notfound", {"var": "text"}]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(null));
}
#[test]
fn test_string_extraction() {
let mut engine = RLogic::new();
let data = json!({"text": "Hello World"});
let logic_id = engine
.compile(&json!({"left": [{"var": "text"}, 5]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("Hello"));
let logic_id = engine.compile(&json!({"left": [{"var": "text"}]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("H"));
let logic_id = engine
.compile(&json!({"right": [{"var": "text"}, 5]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("World"));
let logic_id = engine
.compile(&json!({"right": [{"var": "text"}]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("d"));
}
#[test]
fn test_string_mid() {
let mut engine = RLogic::new();
let data = json!({"text": "Hello World"});
let logic_id = engine
.compile(&json!({"mid": [{"var": "text"}, 6, 5]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(" Worl"));
let logic_id = engine
.compile(&json!({"mid": [{"var": "text"}, 1, 4]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("Hell"));
}
#[test]
fn test_string_split() {
let mut engine = RLogic::new();
let data = json!({"csv": "a,b,c,d,e"});
let logic_id = engine
.compile(&json!({"splitvalue": [{"var": "csv"}, ","]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(["a", "b", "c", "d", "e"]));
let logic_id = engine
.compile(&json!({"splittext": [{"var": "csv"}, ",", 2]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("c"));
}
#[test]
fn test_string_operations_edge_cases() {
let mut engine = RLogic::new();
let data = json!({});
let logic_id = engine.compile(&json!({"length": ""})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(0));
let logic_id = engine.compile(&json!({"cat": ["", ""]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(""));
let logic_id = engine
.compile(&json!({"substr": ["hello", 10, 5]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(""));
let logic_id = engine
.compile(&json!({"substr": ["hello", -1, 5]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("o"));
let logic_id = engine.compile(&json!({"search": ["", "hello"]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(1));
let logic_id = engine.compile(&json!({"search": ["x", ""]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(null));
let logic_id = engine.compile(&json!({"left": ["hi", 10]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("hi"));
let logic_id = engine.compile(&json!({"right": ["hi", 10]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("hi"));
}
#[test]
fn test_string_operations_with_non_strings() {
let mut engine = RLogic::new();
let data = json!({"number": 42, "bool": true, "null": null, "array": [1,2,3]});
let logic_id = engine
.compile(&json!({"cat": [{"var": "number"}, "-", {"var": "bool"}]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("42-true"));
let logic_id = engine
.compile(&json!({"length": {"var": "array"}}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(3));
let logic_id = engine
.compile(&json!({"search": ["2", {"var": "array"}]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(null)); }
#[test]
fn test_string_operations_errors() {
let mut engine = RLogic::new();
let data = json!({});
let logic_id = engine.compile(&json!({"substr": [null, 0, 5]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(""));
let logic_id = engine.compile(&json!({"search": ["test", null]})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(null));
let logic_id = engine.compile(&json!({"length": null})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(0));
}
#[test]
fn test_string_unicode() {
let mut engine = RLogic::new();
let data = json!({"text": "Hello δΈη π"});
let logic_id = engine.compile(&json!({"length": {"var": "text"}})).unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!(17));
let logic_id = engine
.compile(&json!({"cat": ["Hello ", "δΈη π"]}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("Hello δΈη π"));
}
#[test]
fn test_string_complex_operations() {
let mut engine = RLogic::new();
let data = json!({
"sentence": "The quick brown fox jumps over the lazy dog",
"words": ["The", "quick", "brown", "fox"]
});
let logic_id = engine
.compile(&json!({
"mid": [
{"var": "sentence"},
{"search": ["fox", {"var": "sentence"}]},
3
]
}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("fox"));
let logic_id = engine
.compile(&json!({
"cat": [
{"var": "words.0"}, " ",
{"var": "words.1"}, " ",
{"var": "words.2"}, " ",
{"var": "words.3"}
]
}))
.unwrap();
let result = engine.run(&logic_id, &data).unwrap();
assert_eq!(result, json!("The quick brown fox"));
}
}