use serde_json::json;
use xqpath::{evaluate_path_expression, parse_path_expression};
#[test]
fn test_try_catch_basic() {
let data = json!({
"user": {
"name": "Alice",
"age": 30
}
});
let expr = parse_path_expression("try .user.name").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!("Alice")]);
let expr = parse_path_expression("try .user.nonexistent").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
let expected: Vec<serde_json::Value> = vec![];
assert_eq!(result, expected); }
#[test]
fn test_try_catch_with_fallback() {
let data = json!({
"user": {
"name": "Alice",
"age": 30
}
});
let expr =
parse_path_expression("try unknown_function() catch \"default\"")
.unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!("default")]);
let expr =
parse_path_expression("try .user.email catch .user.name").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
let expected: Vec<serde_json::Value> = vec![]; assert_eq!(result, expected);
}
#[test]
fn test_optional_operator_basic() {
let data = json!({
"user": {
"name": "Alice",
"age": 30
}
});
let expr = parse_path_expression(".user.name?").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!("Alice")]);
let expr = parse_path_expression(".user.nonexistent?").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!(null)]);
}
#[test]
fn test_optional_operator_with_functions() {
let data = json!({
"numbers": [1, 2, 3, 4, 5]
});
let expr = parse_path_expression(".numbers | length()?").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!(5)]);
let expr = parse_path_expression(".nonexistent | length()?").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
let expected: Vec<serde_json::Value> = vec![]; assert_eq!(result, expected);
}
#[test]
fn test_nested_try_catch() {
let data = json!({
"config": {
"fallback": "backup_value"
}
});
let expr = parse_path_expression("try (try unknown_function() catch .config.fallback) catch \"ultimate_fallback\"").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!("backup_value")]);
}
#[test]
fn test_optional_with_pipe() {
let data = json!({
"users": [
{"name": "Alice", "email": "alice@example.com"},
{"name": "Bob"}
]
});
let expr = parse_path_expression(".users[1]?").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!({"name": "Bob"})]);
let expr = parse_path_expression(".users[10]?").unwrap(); let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!(null)]); }
#[test]
fn test_try_catch_with_conditions() {
let data = json!({
"user": {
"age": 25
}
});
let expr = parse_path_expression("try (if .user.age > 30 then \"senior\" else \"junior\" end) catch \"unknown\"").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!("junior")]);
let expr = parse_path_expression("if (try .user.email catch null) != null then \"has_email\" else \"no_email\" end").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!("no_email")]);
}
#[test]
fn test_error_handling_combinations() {
let data = json!({
"data": [1, 2, 3]
});
let expr =
parse_path_expression("try (.data | length()?) catch 0").unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!(3)]);
let expr =
parse_path_expression("try .nonexistent? catch (.data | length())")
.unwrap();
let result = evaluate_path_expression(&expr, &data).unwrap();
assert_eq!(result, vec![json!(null)]); }
#[test]
fn test_expression_display() {
let expr =
parse_path_expression("try .user.name catch \"default\"").unwrap();
assert_eq!(expr.as_string(), "try .user.name catch \"default\"");
let expr = parse_path_expression(".user.name?").unwrap();
assert_eq!(expr.as_string(), ".user.name?");
let expr = parse_path_expression("try .field").unwrap();
assert_eq!(expr.as_string(), "try .field");
}