use crate::hir::{self, Atom, BinaryOp, Expr};
use crate::syntax::parser;
const SUCCESS_FIXTURES: &[(&str, &str)] = &[
("1 + 2 * 3", "precedence_mul_add"),
("a - b - c", "associativity_left_sub"),
("a / b / c", "associativity_left_div"),
("-5", "unary_minus"),
("!x", "unary_not"),
("-a * b", "unary_minus_mul"),
("foo(a, b)", "call_basic"),
("foo()", "call_empty"),
("foo([])", "call_empty_list"),
("foo({})", "call_empty_map"),
("a.b(c)", "method_call"),
("a.b(c).d", "method_field_chain"),
("foo.bar().baz()", "method_chain"),
("foo.bar().baz().qux()", "long_method_chain"),
("foo.all(test, test.size() > 4)", "nested_method_call"),
("a.b.c", "field_chain"),
("foo.bar.baz", "field_chain_3"),
("list[0]", "index_basic"),
("map[\"key\"]", "index_string_key"),
("a[b][c]", "index_chain"),
("foo[1][2]", "index_chain_numeric"),
("foo.check[0].baz", "field_index_field"),
(
"foo.bar.filter(x, x > 10)[0].id",
"nested_method_index_field",
),
("foo.bar(x, t[x] > 10)", "method_with_index_arg"),
("a ? b : c", "ternary_basic"),
("x ? y ? z : w : v", "ternary_nested"),
("true ? 1 : false ? 2 : 3", "ternary_nested_else"),
("[1, 2, 3]", "list_basic"),
("[]", "list_empty"),
("[{foo: 1}, {bar: 2}]", "list_nested_maps"),
("{\"key\": value}", "map_string_key"),
("{foo: 1, bar: 2}", "map_ident_keys"),
("{}", "map_empty"),
("a && b || c", "logical_precedence"),
("1 < 2 && 3 >= 4 || 5 == 6 && 5 in 6", "relations_complex"),
("(1 + 2) * 3 % 4", "grouping_precedence"),
("has(x.y)", "macro_has_field"),
("all(items, i, i > 0)", "macro_all"),
("exists(items, i, i == target)", "macro_exists"),
("exists_one(items, i, i > 0)", "macro_exists_one"),
("map(items, i, i * 2)", "macro_map_transform"),
("map(items, i, i > 0, i * 2)", "macro_map_filter_transform"),
("filter(items, i, i > 0)", "macro_filter"),
("identifier", "atom_ident"),
("123", "atom_int"),
("123u", "atom_uint"),
("123.456", "atom_double"),
("true", "atom_true"),
("false", "atom_false"),
("\"string\"", "atom_string"),
("null", "atom_null"),
("1 + size(2u)", "inline_function_call"),
("1 + dyn(2u)", "inline_dyn_call"),
("foo.bar() < 4", "method_relation"),
];
const FAILURE_FIXTURES: &[(&str, &str, &str)] = &[
("foo(", "eof_unclosed_call", "continuing argument list"),
("[1, 2", "eof_unclosed_list", "continuing list"),
("{k: v", "eof_unclosed_map", "continuing map"),
("(1 + 2", "eof_unclosed_paren", "closing parenthesis"),
("1 + * 2", "unexpected_operator", "Unexpected token"),
("&", "lexer_invalid_char", "Unexpected character"),
];
fn parse_with_old(input: &str) -> Result<Expr, String> {
let parsed = parser::parse(input).map_err(|err| err.message)?;
hir::lower(parsed).map_err(|err| err.message)
}
fn assert_parity(input: &str, description: &str) {
let old_result = parse_with_old(input);
assert!(
old_result.is_ok(),
"Pipeline failed on fixture '{}' ({}): {:?}",
description,
input,
old_result.unwrap_err()
);
}
fn assert_failure(
input: &str,
description: &str,
expected_message_fragment: &str,
) {
let old_result = parse_with_old(input);
assert!(
old_result.is_err(),
"Pipeline succeeded on failure fixture '{}' ({}), expected error",
description,
input
);
let message = old_result.expect_err("checked is_err");
assert!(
message.contains(expected_message_fragment),
"Error message mismatch for fixture '{}' ({})\nExpected fragment: '{}'\nActual message: '{}'",
description,
input,
expected_message_fragment,
message
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_success_fixtures() {
for (input, description) in SUCCESS_FIXTURES {
assert_parity(input, description);
}
}
#[test]
fn test_failure_fixtures() {
for (input, description, expected_fragment) in FAILURE_FIXTURES {
assert_failure(input, description, expected_fragment);
}
}
#[test]
fn test_precedence_structure() {
let tree = parse_with_old("1 + 2 * 3").expect("should parse");
let Expr::Binary {
op: BinaryOp::Add,
lhs,
rhs,
} = tree
else {
panic!("Expected Add operator at root, got {:?}", tree);
};
assert!(matches!(*lhs, Expr::Atom(Atom::Int(1))));
let Expr::Binary {
op: BinaryOp::Mul,
lhs: mul_lhs,
rhs: mul_rhs,
} = *rhs
else {
panic!("Expected Multiply as rhs of Add");
};
assert!(matches!(*mul_lhs, Expr::Atom(Atom::Int(2))));
assert!(matches!(*mul_rhs, Expr::Atom(Atom::Int(3))));
}
#[test]
fn test_field_chain_structure() {
let tree = parse_with_old("a.b.c").expect("should parse");
let Expr::Field {
target: outer_target,
field: outer_field,
} = tree
else {
panic!("Expected Field node at root, got {:?}", tree);
};
let Expr::Field {
target: inner_target,
field: inner_field,
} = *outer_target
else {
panic!("Expected nested Field node");
};
assert!(
matches!(*inner_target, Expr::Atom(Atom::Ident(ref ident)) if ident == "a")
);
assert_eq!(inner_field, "b");
assert_eq!(outer_field, "c");
}
#[test]
fn test_method_call_structure() {
let tree = parse_with_old("foo.bar(baz)").expect("should parse");
let Expr::Call {
func,
args,
is_method,
} = tree
else {
panic!("Expected Call node, got {:?}", tree);
};
assert!(is_method, "Expected method call");
assert!(
matches!(*func, Expr::Atom(Atom::Ident(ref ident)) if ident == "bar")
);
assert_eq!(args.len(), 2); assert!(
matches!(args[0], Expr::Atom(Atom::Ident(ref ident)) if ident == "foo")
);
assert!(
matches!(args[1], Expr::Atom(Atom::Ident(ref ident)) if ident == "baz")
);
}
#[test]
fn test_ternary_structure() {
let tree = parse_with_old("a ? b : c").expect("should parse");
let Expr::Ternary {
cond,
then_expr,
else_expr,
} = tree
else {
panic!("Expected Ternary node, got {:?}", tree);
};
assert!(
matches!(*cond, Expr::Atom(Atom::Ident(ref ident)) if ident == "a")
);
assert!(
matches!(*then_expr, Expr::Atom(Atom::Ident(ref ident)) if ident == "b")
);
assert!(
matches!(*else_expr, Expr::Atom(Atom::Ident(ref ident)) if ident == "c")
);
}
#[test]
fn test_list_structure() {
let tree = parse_with_old("[1, 2, 3]").expect("should parse");
let Expr::List(items) = tree else {
panic!("Expected List node, got {:?}", tree);
};
assert_eq!(items.len(), 3);
assert!(matches!(items[0], Expr::Atom(Atom::Int(1))));
assert!(matches!(items[1], Expr::Atom(Atom::Int(2))));
assert!(matches!(items[2], Expr::Atom(Atom::Int(3))));
}
#[test]
fn test_map_structure() {
let tree = parse_with_old("{foo: 1, bar: 2}").expect("should parse");
let Expr::Map(entries) = tree else {
panic!("Expected Map node, got {:?}", tree);
};
assert_eq!(entries.len(), 2);
assert!(
matches!(entries[0].0, Expr::Atom(Atom::Ident(ref ident)) if ident == "foo")
);
assert!(matches!(entries[0].1, Expr::Atom(Atom::Int(1))));
assert!(
matches!(entries[1].0, Expr::Atom(Atom::Ident(ref ident)) if ident == "bar")
);
assert!(matches!(entries[1].1, Expr::Atom(Atom::Int(2))));
}
#[test]
fn test_index_structure() {
let tree = parse_with_old("foo[1]").expect("should parse");
let Expr::Index { target, index } = tree else {
panic!("Expected Index node, got {:?}", tree);
};
assert!(
matches!(*target, Expr::Atom(Atom::Ident(ref ident)) if ident == "foo")
);
assert!(matches!(*index, Expr::Atom(Atom::Int(1))));
}
#[test]
fn test_lexer_error_propagation() {
let err = parse_with_old("&").expect_err("should fail");
assert!(err.contains("Unexpected character"));
}
}