luau-syntax 0.732.0

Luau lexer, parser, AST, CST, and source utilities
Documentation
use super::super::common::*;

// Parser.test.cpp: basic_parse
#[test]
fn basic_parse() {
    parse_ok(r#"print("Hello World!")"#);
}
// Parser.test.cpp: export_is_an_identifier_only_when_followed_by_type
#[test]
fn export_is_an_identifier_only_when_followed_by_type() {
    parse_errors(
        r#"
            export function a() end
        "#,
    )
    .assert_first_message("Incomplete statement: expected assignment or a function call");
}

// Parser.test.cpp: incomplete_statement_error
#[test]
fn incomplete_statement_error() {
    parse_errors("fiddlesticks")
        .assert_first_message("Incomplete statement: expected assignment or a function call");
}
// Parser.test.cpp: stop_if_line_ends_with_hyphen
#[test]
fn stop_if_line_ends_with_hyphen() {
    with_parse("   -", ParseOptions::default(), |result| {
        let result = result.unwrap();
        assert!(!result.metadata.errors.is_empty());
    });
}
// Parser.test.cpp: for_loop_with_single_var_has_comma_positions_of_size_zero
#[test]
fn for_loop_with_single_var_has_comma_positions_of_size_zero() {
    with_parse(
        "for value in tbl do\nend\n",
        ParseOptions::default().with_cst_data(true),
        |result| {
            let result = result.unwrap();

            let [statement] = result.root.as_slice() else {
                panic!("expected for-in loop");
            };
            assert_eq!(statement.tag, StatementTag::GenericFor);

            let cst = result
                .metadata
                .cst_nodes
                .get_statement(*statement)
                .and_then(|node| match node {
                    CstNode::StatForIn(for_in) => Some(for_in),
                    _ => None,
                })
                .expect("expected for-in CST node");
            assert_eq!(cst.variable_commas.len(), 0);
        },
    );
}
// Parser.test.cpp: string_literal_call
#[test]
fn string_literal_call() {
    with_parse("do foo 'bar' end", ParseOptions::default(), |result| {
        let result = result.unwrap();

        let [statement] = result.root.as_slice() else {
            panic!("expected a single do block");
        };
        let body = statement.as_block().expect("expected a single do block");
        let body_statements = block_statement_kinds(body);
        let [statement] = body_statements.exact();
        let expression = statement
            .as_expression()
            .expect("expected a single expression statement in do block")
            .expr;
        let ExpressionKind::Call { args, .. } = expression.kind() else {
            panic!("expected string literal call");
        };
        assert_eq!(args.len(), 1);
        assert!(matches!(
            args[0].kind(),
            ExpressionKind::String { value, .. } if value.as_bytes() == b"bar"
        ));
    });
}