use super::test_schema;
use crate::engine::simplify::Simplify;
use toasty_core::stmt::{self, Expr, ExprMatch, MatchArm, Projection, Value};
#[test]
fn project_non_constant_not_simplified() {
let schema = test_schema();
let mut simplify = Simplify::new(&schema);
let mut expr = stmt::ExprProject {
base: Box::new(Expr::arg(0)),
projection: Projection::from(0),
};
let result = simplify.simplify_expr_project(&mut expr);
assert!(result.is_none());
}
#[test]
fn project_identity_path() {
let schema = test_schema();
let mut simplify = Simplify::new(&schema);
let mut expr = stmt::ExprProject {
base: Box::new(Expr::from(42i64)),
projection: Projection::identity(),
};
let result = simplify.simplify_expr_project(&mut expr);
assert!(matches!(result, Some(Expr::Value(Value::I64(42)))));
}
#[test]
fn project_into_match_distributes() {
let schema = test_schema();
let mut simplify = Simplify::new(&schema);
let mut expr = stmt::ExprProject {
base: Box::new(Expr::Match(ExprMatch {
subject: Box::new(Expr::arg(0)),
arms: vec![
MatchArm {
pattern: Value::from(1i64),
expr: Expr::record([Expr::arg(0), Expr::arg(1)]),
},
MatchArm {
pattern: Value::from(2i64),
expr: Expr::record([Expr::arg(0), Expr::arg(2)]),
},
],
else_expr: Box::new(Expr::record([Expr::arg(0), Expr::error("unreachable")])),
})),
projection: Projection::from(0),
};
let result = simplify.simplify_expr_project(&mut expr);
assert!(result.is_some());
let result = result.unwrap();
if let Expr::Match(m) = result {
assert_eq!(m.arms.len(), 2);
assert!(m.arms[0].expr.is_project());
assert!(m.arms[1].expr.is_project());
assert!(m.else_expr.is_project());
} else {
panic!("expected Match, got {:?}", result);
}
}