use super::*;
fn for_parses_as_unwind(query: &str, alias: &str) {
let stmt = parse(query).unwrap();
match &stmt.clauses[0] {
Clause::Unwind(u) => {
assert_eq!(u.alias, alias, "{query}");
assert!(u.for_syntax, "{query}: expected the FOR spelling flag");
}
other => panic!("{query}: expected a normalized FOR clause, got {other:?}"),
}
}
#[test]
fn test_parse_for_accept_shapes() {
for_parses_as_unwind("FOR x IN [1, 2, 3] RETURN x", "x");
for_parses_as_unwind("FOR x IN null RETURN x", "x");
for_parses_as_unwind("FOR x IN 42 RETURN x", "x");
for_parses_as_unwind("FOR friend IN n.friends RETURN friend", "friend");
let stmt = parse("WITH 3 AS n FOR x IN range(1, n) RETURN x").unwrap();
assert!(matches!(&stmt.clauses[1], Clause::Unwind(u) if u.for_syntax && u.alias == "x"));
for_parses_as_unwind("FOR x IN [[1, 2], [3, 4]] FOR y IN x RETURN y", "x");
let stmt = parse("MATCH (n:Person) FOR tag IN n.tags RETURN tag").unwrap();
assert!(matches!(&stmt.clauses[1], Clause::Unwind(u) if u.for_syntax));
let stmt = parse("WITH [1, 2, 3] AS list FOR x IN list RETURN x").unwrap();
assert!(matches!(&stmt.clauses[1], Clause::Unwind(u) if u.for_syntax));
let stmt = parse("CALL { FOR x IN [1, 2, 3] RETURN x } RETURN x").unwrap();
assert!(matches!(&stmt.clauses[0], Clause::CallSubquery(_)));
let stmt = parse("FOREACH (i IN [1] | FOR x IN [1, 2] CREATE (:N {v: x}))").unwrap();
assert!(matches!(&stmt.clauses[0], Clause::Foreach(_)));
}
#[test]
fn test_parse_for_reject_shapes() {
for query in [
"FOR x [1, 2, 3] RETURN x",
"FOR IN [1, 2, 3] RETURN x",
"FOR [1, 2, 3] AS x RETURN x",
"FOR x AS [1, 2, 3] RETURN x",
] {
let err = parse(query).unwrap_err();
assert!(!err.message.is_empty(), "{query} parsed");
}
}
#[test]
fn test_parse_for_normalizes_to_the_unwind_ast() {
let for_stmt = parse("FOR x IN [1, 2, 3] RETURN x").unwrap();
let unwind_stmt = parse("UNWIND [1, 2, 3] AS x RETURN x").unwrap();
let Clause::Unwind(for_clause) = &for_stmt.clauses[0] else {
panic!("expected a normalized FOR clause");
};
let Clause::Unwind(unwind_clause) = &unwind_stmt.clauses[0] else {
panic!("expected an UNWIND clause");
};
assert_eq!(for_clause.expr, unwind_clause.expr);
assert_eq!(for_clause.alias, unwind_clause.alias);
assert!(for_clause.for_syntax);
assert!(!unwind_clause.for_syntax);
assert_eq!(for_stmt.clauses[1], unwind_stmt.clauses[1]);
}
#[test]
fn test_parse_unwind_keeps_its_own_spelling() {
let stmt = parse("UNWIND [1, 2, 3] AS x RETURN x").unwrap();
match &stmt.clauses[0] {
Clause::Unwind(u) => {
assert!(!u.for_syntax, "UNWIND must not gain the FOR flag");
}
_ => panic!("expected unwind"),
}
}
#[test]
fn test_parse_for_binding_variable_accepts_keyword_spellings() {
for_parses_as_unwind("FOR for IN [1, 2] RETURN for", "for");
for_parses_as_unwind("FOR end IN [1, 2] RETURN end", "end");
}
#[test]
fn test_parse_for_terminates_a_group_by_key_list() {
let stmt =
parse("UNWIND [1, 1, 2] AS x RETURN x, count(*) AS c GROUP BY (x) FOR y IN [1] RETURN y")
.unwrap();
assert!(matches!(&stmt.clauses[2], Clause::Unwind(u) if u.for_syntax));
}