use chumsky::{text::newline, IterParser, Parser};

use crate::{
    __internal__parse_include_str, __internal__parser, __internal__span_macro_include_str,
    ast::{Command, Expression, Literal, StringLiteralComponent},
    parse::source_file::{command::command_or_comment, expression::expression},
};

use pretty_assertions::assert_eq;

#[test]
fn test_parse_commands() {
    __internal__span_macro_include_str!(
        crate::Span,
        "../../../../../testdata/partial/commands.cctmpl"
    );

    __internal__parser! { p, Vec<Option<Command<'s>>>, {
        command_or_comment(expression())
            .separated_by(newline())
            .allow_trailing()
            .collect::<Vec<Option<Command>>>()
            .with_ctx((1, s!(0..0)))

    }}
    assert_eq!(
        __internal__parse_include_str!(p(), "../../../../../testdata/partial/commands.cctmpl")
            .unwrap(),
        vec![
            Some(Command::Assignment {
                variable: ("some_var", s!(1..9)),
                expression: (
                    Expression::Literal(Literal::String(vec![(
                        StringLiteralComponent::Literal("Hello World!"),
                        s!(13..25)
                    )])),
                    s!(12..26)
                )
            }),
            Some(Command::Assignment {
                variable: ("some_arr", s!(77..85)),
                expression: (
                    Expression::Literal(Literal::Array(vec![
                        (
                            Expression::Literal(Literal::String(vec![(
                                StringLiteralComponent::Literal("Peter"),
                                s!(90..95)
                            )])),
                            s!(89..96)
                        ),
                        (
                            Expression::Literal(Literal::String(vec![(
                                StringLiteralComponent::Literal("Tim"),
                                s!(99..102)
                            )])),
                            s!(98..103)
                        )
                    ])),
                    s!(88..104)
                )
            }),
            Some(Command::Expression(
                Expression::Literal(Literal::Int(1)),
                s!(135..136)
            )),
            None,
        ]
    );
}
