use super::labels::LabelSpec;
use super::*;
#[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_real_label_disjunction_still_parses_with_no_map() {
let stmt = parse("MATCH (n) RETURN [x IN [n] WHERE x:A|B] 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) => match &hl.label_expr {
LabelExpr::Or(parts) => assert_eq!(parts.len(), 2),
other => panic!("expected Or, got {other:?}"),
},
other => panic!("expected HasLabel, got {other:?}"),
},
other => panic!("expected ListComprehension, got {other:?}"),
},
other => panic!("expected return, got {other:?}"),
}
}
#[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_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
);
}
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(BracketProof::RightBracketClosesWithTheMapOmitted),
|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 a_probe_verdict_does_not_cross_feed_across_comprehension_contexts() {
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(BracketProof::RightBracketClosesTheComprehension),
|p| p.node_pattern_precedes_relationship(),
);
assert!(
!under_map_required,
"under the map-required kind the inline WHERE's pipe rolls back and the pattern fails"
);
let plain = parser.node_pattern_precedes_relationship();
assert!(
plain,
"outside any context the same position's verdict is its own"
);
}