From 81860e07d925f7efe6f202d3140fd9bd7dd61922 Mon Sep 17 00:00:00 2001 From: KavinduZoysa Date: Wed, 11 Sep 2024 13:13:41 +0530 Subject: [PATCH] Add more tests --- .../compiler/parser/test/ParserTestUtils.java | 12 ++++++++ .../nodeparser/ParseBlockStatementTest.java | 30 +++++++++++++++++++ .../nodeparser/ParseFunctionBodyBlock.java | 28 +++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/ParserTestUtils.java b/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/ParserTestUtils.java index 6dc3df46bfd5..b9cfe29df20c 100644 --- a/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/ParserTestUtils.java +++ b/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/ParserTestUtils.java @@ -32,6 +32,7 @@ import io.ballerina.compiler.internal.parser.tree.STNodeList; import io.ballerina.compiler.internal.parser.tree.STToken; import io.ballerina.compiler.internal.syntax.SyntaxUtils; +import io.ballerina.compiler.syntax.tree.CommentNode; import io.ballerina.compiler.syntax.tree.Node; import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.SyntaxTree; @@ -47,6 +48,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collection; +import java.util.List; import static io.ballerina.compiler.internal.syntax.SyntaxUtils.isSTNodePresent; import static io.ballerinalang.compiler.parser.test.ParserTestConstants.CHILDREN_FIELD; @@ -1476,4 +1478,14 @@ private static SyntaxKind getDocumentationKind(String kind) { throw new UnsupportedOperationException("cannot find syntax kind: " + kind); } } + + public static void assertCommentNode(Node node, List comments) { + Assert.assertTrue(node instanceof CommentNode); + CommentNode commentNode = (CommentNode) node; + List commentLines = commentNode.getCommentLines(); + Assert.assertEquals(commentLines.size(), comments.size()); + for (int i = 0; i < comments.size(); i++) { + Assert.assertEquals(commentLines.get(i), comments.get(i)); + } + } } diff --git a/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseBlockStatementTest.java b/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseBlockStatementTest.java index f665793167a0..d246eff02732 100644 --- a/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseBlockStatementTest.java +++ b/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseBlockStatementTest.java @@ -18,6 +18,8 @@ package io.ballerinalang.compiler.parser.test.tree.nodeparser; import io.ballerina.compiler.syntax.tree.BlockStatementNode; +import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.compiler.syntax.tree.NodeAndCommentList; import io.ballerina.compiler.syntax.tree.NodeList; import io.ballerina.compiler.syntax.tree.NodeParser; import io.ballerina.compiler.syntax.tree.StatementNode; @@ -28,6 +30,8 @@ import java.util.List; +import static io.ballerinalang.compiler.parser.test.ParserTestUtils.assertCommentNode; + /** * Test {@code parseBlockStatement} method. * @@ -177,4 +181,30 @@ public void testBlockStmtRecovery() { Assert.assertEquals(blockStmtNode.toString(), " INVALID[%]{ int a; INVALID[;] } INVALID[;] INVALID[;]"); } + + @Test + public void testCommentInBlockStatementBody() { + String blockStatement = """ + { + // Initialize x + int x = 1; + // Initialize y + int y = 1; + + // new comment + // another new comment + }"""; + BlockStatementNode blockStmtNode = NodeParser.parseBlockStatement(blockStatement); + Assert.assertEquals(blockStmtNode.kind(), SyntaxKind.BLOCK_STATEMENT); + Assert.assertFalse(blockStmtNode.hasDiagnostics()); + + NodeAndCommentList nodes = blockStmtNode.statementsWithComments(); + Assert.assertEquals(nodes.size(), 5); + + assertCommentNode(nodes.get(0), List.of("Initialize x")); + assertCommentNode(nodes.get(2), List.of("Initialize y")); + assertCommentNode(nodes.get(4), List.of("new comment", "another new comment")); + } + + } diff --git a/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseFunctionBodyBlock.java b/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseFunctionBodyBlock.java index 926702dab247..6d5ea710a004 100644 --- a/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseFunctionBodyBlock.java +++ b/compiler/ballerina-parser/src/test/java/io/ballerinalang/compiler/parser/test/tree/nodeparser/ParseFunctionBodyBlock.java @@ -20,6 +20,8 @@ import io.ballerina.compiler.syntax.tree.FunctionBodyBlockNode; import io.ballerina.compiler.syntax.tree.NamedWorkerDeclarationNode; import io.ballerina.compiler.syntax.tree.NamedWorkerDeclarator; +import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.compiler.syntax.tree.NodeAndCommentList; import io.ballerina.compiler.syntax.tree.NodeList; import io.ballerina.compiler.syntax.tree.NodeParser; import io.ballerina.compiler.syntax.tree.StatementNode; @@ -31,6 +33,8 @@ import java.util.List; import java.util.Optional; +import static io.ballerinalang.compiler.parser.test.ParserTestUtils.assertCommentNode; + /** * Test {@code parseFunctionBodyBlock} method. * @@ -255,4 +259,28 @@ public void testFuncBodyBlockRecovery() { Assert.assertEquals(funcBodyBlockNode.toString(), " INVALID[%]{ int a; INVALID[;] }; INVALID[;]"); } + + @Test + public void testCommentInFunctionBody() { + String funcBodyBlock = """ + { + // Initialize x + int x = 1; + // Initialize y + int y = 1; + + // new comment + // another new comment + }"""; + FunctionBodyBlockNode funcBodyBlockNode = NodeParser.parseFunctionBodyBlock(funcBodyBlock); + Assert.assertEquals(funcBodyBlockNode.kind(), SyntaxKind.FUNCTION_BODY_BLOCK); + Assert.assertFalse(funcBodyBlockNode.hasDiagnostics()); + + NodeAndCommentList nodes = funcBodyBlockNode.statementsWithComments(); + Assert.assertEquals(nodes.size(), 5); + + assertCommentNode(nodes.get(0), List.of("Initialize x")); + assertCommentNode(nodes.get(2), List.of("Initialize y")); + assertCommentNode(nodes.get(4), List.of("new comment", "another new comment")); + } }