relate-ir 1.5.0

Planner IR (lowered Cypher AST)
Documentation
//! The shared body-descent skeleton's own tests (ADR-0740): the
//! FINAL-stage cursor, the any-arm fold, the naming verdict's breadth,
//! and the structural invariants the skeleton surfaces. Every subject
//! is built from the shared `body_descent_fixtures` kit — the same
//! constructors the executor verdict's suite builds its corpus from.

use super::body_descent::{projects_columns_concluding, stage_projects_rows};
use super::body_descent_fixtures::{
    body, call_clause, create_clause, next_chain, return_clause, union_arms, when_clause,
};
use super::SingleQuery;

#[test]
fn projects_columns_judges_the_next_chain_by_its_final_stage() {
    // The FINAL-stage cursor: the first stage's answer never reaches
    // the verdict, in either direction.
    let projecting_then_unit = next_chain(return_clause(), body(vec![create_clause()]));
    assert!(!projecting_then_unit.projects_columns());

    let unit_then_projecting = next_chain(create_clause(), body(vec![return_clause()]));
    assert!(unit_then_projecting.projects_columns());
}

#[test]
fn only_the_concluding_clause_of_an_arm_decides() {
    // The skeleton hands the verdict the arm's LAST clause only — a
    // projecting clause mid-arm is not the arm's answer.
    assert!(body(vec![create_clause(), return_clause()]).projects_columns());
    assert!(!body(vec![return_clause(), create_clause()]).projects_columns());
}

#[test]
fn the_skeleton_folds_any_union_arm() {
    // The any-arm breadth: one projecting arm makes the stage project.
    // On a validated body every arm agrees (`UNION requires same
    // columns`), so this pins the skeleton's breadth for the
    // representative-disagreement shape validation refuses.
    assert!(union_arms(create_clause(), return_clause()).projects_columns());
    assert!(union_arms(return_clause(), create_clause()).projects_columns());
    assert!(!union_arms(create_clause(), create_clause()).projects_columns());
}

#[test]
fn the_naming_verdict_stands_on_the_first_branch() {
    // The first-branch stand-in is the naming layer's own breadth: the
    // branch set's order decides, where the executor's any-branch fold
    // would answer `true` on both shapes (its tests pin that side).
    let first_unit = body(vec![when_clause(
        vec![body(vec![create_clause()]), body(vec![return_clause()])],
        None,
    )]);
    assert!(!first_unit.projects_columns());

    let first_projecting = body(vec![when_clause(
        vec![body(vec![return_clause()]), body(vec![create_clause()])],
        None,
    )]);
    assert!(first_projecting.projects_columns());
}

#[test]
fn nested_call_bodies_descend_under_the_naming_verdict() {
    assert!(body(vec![call_clause(body(vec![return_clause()]))]).projects_columns());
    assert!(!body(vec![call_clause(body(vec![create_clause()]))]).projects_columns());
    // The descent is unbounded through nesting: a body ending in a
    // call whose body ends in a call still answers by the innermost
    // concluding clause.
    let nested_unit = call_clause(body(vec![call_clause(body(vec![create_clause()]))]));
    assert!(!body(vec![nested_unit]).projects_columns());
}

#[test]
#[should_panic(expected = "a stage invariantly carries at least one part")]
fn the_skeleton_surfaces_an_empty_parts_list() {
    stage_projects_rows(&[], &projects_columns_concluding);
}

#[test]
#[should_panic(expected = "a part invariantly carries at least one clause")]
fn the_skeleton_surfaces_an_empty_clause_list() {
    stage_projects_rows(
        &[SingleQuery {
            clauses: Vec::new(),
        }],
        &projects_columns_concluding,
    );
}