use super::labels::LabelSpec;
use super::*;
use crate::parser::comprehension_where::ComprehensionWhereContext;
#[test]
fn test_list_comprehension_dynamic_label_where_then_map_pipe_not_swallowed() {
let stmt = parse("MATCH (n) RETURN [x IN [n] WHERE x:$('A') | x.name] AS filtered").unwrap();
match &stmt.clauses[1] {
Clause::Return(rc) => match &rc.items[0].expr {
Expr::ListComprehension(lc) => {
match lc.where_expr.as_ref().unwrap() {
Expr::HasLabel(hl) => {
assert!(matches!(hl.label_expr, LabelExpr::Dynamic(_)))
}
other => panic!("expected HasLabel, got {other:?}"),
}
assert!(matches!(lc.map_expr.as_ref().unwrap(), Expr::Property(_)));
}
other => panic!("expected ListComprehension, got {other:?}"),
},
other => panic!("expected return, got {other:?}"),
}
}
#[test]
fn test_list_comprehension_single_pipe_yields_to_the_map_separator() {
for (q, want_or) in [
("MATCH (n) RETURN [x IN [n] WHERE x:A|B] AS filtered", false),
(
"MATCH (n) RETURN [x IN [n] WHERE x:A|B AND x.p1 IS NOT NULL] AS filtered",
false,
),
(
"MATCH (n) RETURN [x IN [n] WHERE x:A|B | x.p1] AS filtered",
true,
),
] {
let stmt = parse_ok(q);
let Expr::ListComprehension(lc) = return_expr(&stmt) else {
panic!("{q}: expected a list comprehension");
};
let Some(where_expr) = lc.where_expr.as_ref() else {
panic!("{q}: expected a WHERE");
};
let Expr::HasLabel(hl) = where_expr else {
panic!("{q}: expected a HasLabel WHERE");
};
let is_or = matches!(&hl.label_expr, LabelExpr::Or(parts) if parts.len() == 2);
assert_eq!(is_or, want_or, "{q}: WHERE is {:#?}", hl.label_expr);
assert!(lc.map_expr.is_some(), "{q}: the map expression exists");
}
}
#[test]
fn comprehension_where_label_predicate_yields_the_pipe_to_a_map_projection() {
for query in [
"MATCH p=(a:L1)-[:R]->(b:L2) RETURN [n IN nodes(p) WHERE n:L1 | n{.p1}] AS x",
"MATCH (n) RETURN [m IN [n] WHERE m:L1 | m {}] AS x",
] {
let stmt = parse_ok(query);
match return_expr(&stmt) {
Expr::ListComprehension(lc) => {
assert!(
is_plain_label(&lc.where_expr),
"{query}: the WHERE stayed the label predicate"
);
assert!(
lc.map_expr.as_ref().is_some_and(is_map_projection),
"{query}: the map expression is the projection"
);
}
other => panic!("{query}: expected a list comprehension, got {other:?}"),
}
}
}
#[test]
fn pattern_comprehension_where_yields_the_pipe_to_a_map_projection() {
let stmt = parse_ok("MATCH (a) RETURN [(a)-[:R]->(b) WHERE a:L1 | b{.p1}] AS x");
match return_expr(&stmt) {
Expr::PatternComprehension(pc) => {
assert!(
is_plain_label(&pc.where_expr),
"the WHERE stayed the label predicate"
);
assert!(
is_map_projection(&pc.map_expr),
"the map expression is the projection"
);
}
other => panic!("expected PatternComprehension, got {other:?}"),
}
}
#[test]
fn a_property_access_map_expression_still_takes_the_pipe() {
let stmt = parse_ok("MATCH (n) RETURN [m IN [n] WHERE m:L1 | m.p1] AS x");
match return_expr(&stmt) {
Expr::ListComprehension(lc) => {
assert!(matches!(lc.map_expr.as_ref(), Some(Expr::Property(_))));
assert!(is_plain_label(&lc.where_expr));
}
other => panic!("expected ListComprehension, got {other:?}"),
}
}
#[test]
fn a_function_call_map_expression_takes_the_pipe_after_a_label_filter() {
for query in [
"MATCH (n) RETURN [m IN [n] WHERE m:L1 | substring(m.p1, 0, 1)] AS x",
"MATCH (n) RETURN [m IN [n] WHERE m IS L1 | substring(m.p1, 0, 1)] AS x",
"MATCH (n) RETURN [m IN [n] WHERE m:L1 | toString(m)] AS x",
"MATCH (n) RETURN [m IN [n] WHERE m:L1 | substring(toString(m.p1), 0, 1)] AS x",
] {
let stmt = parse_ok(query);
match return_expr(&stmt) {
Expr::ListComprehension(lc) => {
assert!(
is_plain_label(&lc.where_expr),
"{query}: the WHERE stayed the label predicate"
);
assert!(
matches!(lc.map_expr.as_ref(), Some(Expr::FunctionCall(_))),
"{query}: the map expression is the function call"
);
}
other => panic!("{query}: expected ListComprehension, got {other:?}"),
}
}
}
#[test]
fn a_pattern_comprehension_function_call_map_expression_takes_the_pipe() {
let stmt = parse_ok("MATCH (a) RETURN [(a)-[:R]->(b) WHERE a:L1 | toString(b.p1)] AS x");
match return_expr(&stmt) {
Expr::PatternComprehension(pc) => {
assert!(is_plain_label(&pc.where_expr));
assert!(matches!(pc.map_expr, Expr::FunctionCall(_)));
}
other => panic!("expected PatternComprehension, got {other:?}"),
}
}
#[test]
fn a_label_disjunction_still_commits_before_a_map_expression_tail() {
let stmt = parse_ok("MATCH (n) RETURN [m IN [n] WHERE m:A|B AND m.p1 IS NOT NULL | m.p1] AS x");
match return_expr(&stmt) {
Expr::ListComprehension(lc) => {
assert!(!is_plain_label(&lc.where_expr));
assert!(matches!(lc.map_expr.as_ref(), Some(Expr::Property(_))));
}
other => panic!("expected ListComprehension, got {other:?}"),
}
}
#[test]
fn a_list_comprehension_property_map_after_a_disjunction_keeps_the_disjunction() {
let stmt = parse_ok("MATCH (a) RETURN [x IN [a] WHERE (x)-[:S|T {w: 1}]->() | x] AS out");
let Expr::ListComprehension(lc) = return_expr(&stmt) else {
panic!("expected a list comprehension");
};
assert!(
matches!(lc.map_expr.as_ref(), Some(Expr::Variable(_))),
"the map expression survived: {:?}",
lc.map_expr
);
}
#[test]
fn a_pattern_comprehension_property_map_after_a_disjunction_keeps_the_disjunction() {
let stmt = parse_ok("MATCH (a) RETURN [(x)-[:S|T {w: 1}]->() | x] AS out");
let Expr::PatternComprehension(pc) = return_expr(&stmt) else {
panic!("expected a pattern comprehension");
};
assert!(
matches!(&pc.map_expr, Expr::Variable(_)),
"the map expression survived: {:?}",
pc.map_expr
);
}
#[test]
fn a_comprehension_inside_a_dynamic_label_atom_takes_its_own_pipe() {
let stmt = parse_ok("MATCH (n:$([x IN [n] WHERE x:L1 | x{.p1}])) RETURN n");
let Clause::Match(m) = &stmt.clauses[0] else {
panic!("expected a match");
};
let Some(PatternElement::Node(node)) = m.pattern[0].elements.first() else {
panic!("expected a leading node");
};
let Some(LabelExpr::Dynamic(DynamicLabelExpr { expr, .. })) = &node.label_expr else {
panic!("expected a dynamic label atom, got {:?}", node.label_expr);
};
let Expr::ListComprehension(lc) = expr.as_ref() else {
panic!("expected the atom to hold a list comprehension");
};
assert!(is_plain_label(&lc.where_expr));
assert!(lc.map_expr.as_ref().is_some_and(is_map_projection));
}
#[test]
fn a_type_disjunction_in_a_comprehension_where_keeps_the_pipe() {
let stmt = parse_ok("MATCH (a) RETURN [x IN [a] WHERE (x)-[:S|T]->(y) | x] AS out");
let Expr::ListComprehension(lc) = return_expr(&stmt) else {
panic!("expected a list comprehension");
};
assert!(is_plain_label(&lc.where_expr) || lc.where_expr.is_some());
let pattern_stmt = parse_ok("MATCH (a) RETURN [(x)-[:S|T]->(y) | y] AS out");
let Expr::PatternComprehension(pc) = return_expr(&pattern_stmt) else {
panic!("expected a pattern comprehension");
};
let Some(PatternElement::Relationship(rel)) = pc.pattern.elements.get(1) else {
panic!("expected the relationship element");
};
assert!(
matches!(&rel.type_expr, Some(LabelExpr::Or(parts)) if parts.len() == 2),
"the type disjunction survived: {:?}",
rel.type_expr
);
}
#[test]
fn an_unparseable_separator_reading_keeps_the_chain_reading() {
for (query, disjunct_shape) in [
(
"RETURN [x IN [1] WHERE x:A|B&C] AS r",
"Or([A, And([B, C])])",
),
("RETURN [x IN [1] WHERE x:A|!B] AS r", "Or([A, Not(B)])"),
(
"RETURN [x IN [1] WHERE x:A|$(x.p)] AS r",
"Or([A, Dynamic])",
),
] {
let stmt = parse_ok(query);
let Expr::ListComprehension(lc) = return_expr(&stmt) else {
panic!("{query}: expected a list comprehension");
};
let Some(where_expr) = lc.where_expr.as_ref() else {
panic!("{query}: expected a WHERE");
};
let Expr::HasLabel(hl) = where_expr else {
panic!("{query}: expected a HasLabel WHERE, got {where_expr:?}");
};
assert!(
matches!(&hl.label_expr, LabelExpr::Or(parts) if parts.len() == 2),
"{query}: the disjunction survived ({disjunct_shape}), got {:#?}",
hl.label_expr
);
assert!(
lc.map_expr.is_none(),
"{query}: no map expression — the pipe stayed in the chain"
);
}
}
fn label_or_in_comprehension_where(source: &str) -> (Parser<'_>, LabelSpec) {
let mut parser = Parser::new(source, "query text", crate::lexer::ParseOptions::default())
.expect("the scaffold's source always tokenizes");
let spec = parser
.with_comprehension_where(
Some(ComprehensionWhereContext {
closer: TokenType::RightBracket,
opener: 0,
}),
|p| p.parse_label_or(true),
)
.expect("the scaffold's sources always parse");
(parser, spec)
}
#[test]
fn a_rolled_back_legacy_pipe_colon_continuation_leaves_no_fact_behind() {
let (parser, spec) = label_or_in_comprehension_where("S |: T.foo]");
assert!(
!spec.saw_legacy_pipe_colon,
"the rolled-back `|:` stays unseen"
);
assert!(matches!(spec.expr, LabelExpr::Label(_)));
assert_eq!(
parser.peek_type(),
TokenType::Pipe,
"the `|` was left for the comprehension's separator"
);
let (_, spec) = label_or_in_comprehension_where("S |: T");
assert!(spec.saw_legacy_pipe_colon);
assert!(matches!(spec.expr, LabelExpr::Or(_)));
}
#[test]
fn a_rolled_back_pipe_keeps_its_gql_symbol_note() {
let stmt = parse_ok("MATCH (a) WHERE [x IN [a] WHERE x IS L1 | a.name] IS NOT NULL RETURN a");
assert!(matches!(&stmt.clauses[0], Clause::Match(_)));
let err = parse("MATCH (a:A:B) WHERE [x IN [a] WHERE x IS L1 | a.name] IS NOT NULL RETURN a")
.unwrap_err();
assert!(
err.to_string().contains("InvalidLabelExpression"),
"the 42I10 mixing rejection fired, got {err}"
);
let err =
parse("MATCH (a:A:B) WHERE [x IN [a] WHERE x IS L1 | L2 | a.name] IS NOT NULL RETURN a")
.unwrap_err();
assert!(
err.to_string().contains("InvalidLabelExpression"),
"the committed disjunction's note rejected it, got {err}"
);
}
#[test]
fn an_inline_node_pattern_where_pipe_is_nesting_owned_in_every_context() {
let mut parser = Parser::new(
"(n WHERE [x IS L1 | q] = z)-[:R]->()",
"query text",
crate::lexer::ParseOptions::default(),
)
.unwrap();
let under_map_required = parser.with_comprehension_where(
Some(ComprehensionWhereContext {
closer: TokenType::RightBracket,
opener: 0,
}),
|p| p.node_pattern_precedes_relationship(),
);
assert!(
under_map_required,
"under the map-required kind the inline WHERE's pipe is still the inner grammar's"
);
let plain = parser.node_pattern_precedes_relationship();
assert!(
plain,
"outside any context the same position's verdict is its own — and equal"
);
}