luau-syntax 0.732.0

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

#[cfg(test)]
mod luau_comment_tests {
    use super::*;

    // Parser.test.cpp: mode_is_unset_if_no_hot_comment
    #[test]
    fn mode_is_unset_if_no_hot_comment() {
        with_parse("print('Hello World!')", ParseOptions::default(), |result| {
            let result = result.unwrap();

            assert!(result.metadata.hotcomments.is_empty());
        });
    }

    // Parser.test.cpp: sense_hot_comment_on_first_line
    #[test]
    fn sense_hot_comment_on_first_line() {
        with_parse(
            "   --!strict ",
            ParseOptions::default().with_comment_capture(true),
            |result| {
                let result = result.unwrap();

                assert_eq!(result.metadata.mode(), Some(Mode::Strict));
            },
        );
    }

    // Parser.test.cpp: non_header_hot_comments
    #[test]
    fn non_header_hot_comments() {
        with_parse(
            "do end --!strict",
            ParseOptions::default().with_comment_capture(true),
            |result| {
                let result = result.unwrap();

                assert_eq!(result.metadata.mode(), None);
            },
        );
    }

    // Parser.test.cpp: nonstrict_mode
    #[test]
    fn nonstrict_mode() {
        with_parse(
            "--!nonstrict",
            ParseOptions::default().with_comment_capture(true),
            |result| {
                let result = result.unwrap();

                assert!(result.metadata.errors.is_empty());
                assert_eq!(result.metadata.mode(), Some(Mode::Nonstrict));
            },
        );
    }

    // Parser.test.cpp: nocheck_mode
    #[test]
    fn nocheck_mode() {
        with_parse(
            "--!nocheck",
            ParseOptions::default().with_comment_capture(true),
            |result| {
                let result = result.unwrap();

                assert!(result.metadata.errors.is_empty());
                assert_eq!(result.metadata.mode(), Some(Mode::NoCheck));
            },
        );
    }

    // Parser.test.cpp: vertical_space
    #[test]
    fn vertical_space() {
        with_parse("a()\u{000b}b()", ParseOptions::default(), |result| {
            result.unwrap();
        });
    }

    // Parser.test.cpp: capture_comments
    #[test]
    fn capture_comments() {
        with_parse(
            r#"
        --!strict

        local a = 5 -- comment one
        local b = 8 -- comment two
        --[[
            Multi line comment
        ]]
        local c = 'see'
    "#,
            ParseOptions::default().with_comment_capture(true),
            |result| {
                let result = result.unwrap();

                assert_eq!(result.metadata.comment_locations.len(), 4);
                assert_eq!(
                    result.metadata.comment_locations[0].location,
                    loc!(pos!(1, 8), pos!(1, 17))
                );
                assert_eq!(
                    result.metadata.comment_locations[1].location,
                    loc!(pos!(3, 20), pos!(3, 34))
                );
                assert_eq!(
                    result.metadata.comment_locations[2].location,
                    loc!(pos!(4, 20), pos!(4, 34))
                );
                assert_eq!(
                    result.metadata.comment_locations[3].location,
                    loc!(pos!(5, 8), pos!(7, 10))
                );
            },
        );
    }

    // Parser.test.cpp: capture_broken_comment_at_the_start_of_the_file
    #[test]
    fn capture_broken_comment_at_the_start_of_the_file() {
        with_parse(
            r#"
        --[[
    "#,
            ParseOptions::default().with_comment_capture(true),
            |result| {
                let result = result.unwrap();

                assert_eq!(result.metadata.comment_locations.len(), 1);
                assert_eq!(
                    result.metadata.comment_locations[0].location,
                    loc!(pos!(1, 8), pos!(2, 4))
                );
            },
        );
    }

    // Parser.test.cpp: capture_broken_comment
    #[test]
    fn capture_broken_comment() {
        with_parse(
            r#"
        local a = "test"

        --[[broken!
    "#,
            ParseOptions::default().with_comment_capture(true),
            |result| {
                let result = result.unwrap();

                assert_eq!(result.metadata.comment_locations.len(), 1);
                assert_eq!(
                    result.metadata.comment_locations[0].location,
                    loc!(pos!(3, 8), pos!(4, 4))
                );
            },
        );
    }
}