Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KavinduZoysa committed Sep 11, 2024
1 parent 410ecfa commit 81860e0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> comments) {
Assert.assertTrue(node instanceof CommentNode);
CommentNode commentNode = (CommentNode) node;
List<String> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,8 @@

import java.util.List;

import static io.ballerinalang.compiler.parser.test.ParserTestUtils.assertCommentNode;

/**
* Test {@code parseBlockStatement} method.
*
Expand Down Expand Up @@ -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<Node> 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"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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<Node> 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"));
}
}

0 comments on commit 81860e0

Please sign in to comment.