use crate::{Datum, Expr, InfixOp};

#[test]
fn empty_array_parses() {
    assert_eq!(Expr::try_from("array[]"), Ok(Expr::Array(vec![])));
}

#[test]
fn array_of_literals_parses() {
    assert_eq!(
        Expr::try_from("array[1, 2, 3]"),
        Ok(Expr::Array(vec![
            Expr::Literal(Datum::Numeric(Some(1.into()))),
            Expr::Literal(Datum::Numeric(Some(2.into()))),
            Expr::Literal(Datum::Numeric(Some(3.into()))),
        ])),
    );
}

#[test]
fn array_of_exprs_parses() {
    assert_eq!(
        Expr::try_from("array[(1), 2 + 3]"),
        Ok(Expr::Array(vec![
            Expr::Literal(Datum::Numeric(Some(1.into()))),
            Expr::Infix {
                lhs: Box::new(Expr::Literal(Datum::Numeric(Some(2.into())))),
                op: InfixOp::Add,
                rhs: Box::new(Expr::Literal(Datum::Numeric(Some(3.into())))),
            },
        ])),
    );
}

#[test]
fn array_cmp_modifier_parses() {
    assert_eq!(
        Expr::try_from("3 = any(array[3])"),
        Ok(Expr::Infix {
            lhs: Box::new(Expr::Literal(Datum::Numeric(Some(3.into())))),
            op: InfixOp::WithCmpModifierAny(Box::new(InfixOp::Eq)),
            rhs: Box::new(Expr::Array(vec![Expr::Literal(Datum::Numeric(Some(
                3.into()
            )))]))
        }),
    );
}

#[test]
fn non_parenthesized_array_cmp_modifier_fails() {
    assert!(Expr::try_from("3 = any array[3]").is_err());
}
