Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0b18881

Browse files
Add UnaryExpr, BinaryExpr, and some record/enum tests to improve overall test coverage (#4930)
* Add some tests for UnaryExpr and BinaryExpr coverage* Add tests for records and enums named module
1 parent2186f21 commit0b18881

File tree

4 files changed

+349
-0
lines changed

4 files changed

+349
-0
lines changed

‎javaparser-core-testing/src/test/java/com/github/javaparser/ast/body/RecordDeclarationTest.java‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,64 @@ void instanceFieldIsNotAllowedInRecord() {
714714
});
715715
}
716716

717+
/**
718+
* "module" became a keyword in Java 9, but can still be used as an identifier
719+
* in certain contexts. This test verifies the AST for a record named "module"
720+
* that also uses "module" as a type and in object creation.
721+
*/
722+
@Test
723+
voidrecordWithModuleAsName() {
724+
Strings ="record module(String s) {\n"
725+
+" void foo() {\n"
726+
+" module m = new module(\"hello\");\n"
727+
+" }\n"
728+
+"}\n";
729+
730+
CompilationUnitcu =TestParser.parseCompilationUnit(s);
731+
assertOneRecordDeclaration(cu);
732+
733+
RecordDeclarationrecordDeclaration =
734+
cu.findFirst(RecordDeclaration.class).get();
735+
assertEquals("module",recordDeclaration.getNameAsString());
736+
737+
// Verify the record has one parameter named "s" of type String
738+
NodeList<Parameter>parameters =recordDeclaration.getParameters();
739+
assertEquals(1,parameters.size());
740+
Parameterparameter =parameters.get(0);
741+
assertEquals("s",parameter.getNameAsString());
742+
assertEquals("String",parameter.getTypeAsString());
743+
744+
// Verify the record has one method named "foo"
745+
assertEquals(1,recordDeclaration.getMembers().size());
746+
assertTrue(recordDeclaration.getMembers().get(0).isMethodDeclaration());
747+
MethodDeclarationmethod =recordDeclaration.getMembers().get(0).asMethodDeclaration();
748+
assertEquals("foo",method.getNameAsString());
749+
750+
// Verify the method body contains a variable declaration with object creation
751+
assertEquals(1,method.getBody().get().getStatements().size());
752+
assertTrue(method.getBody().get().getStatements().get(0).isExpressionStmt());
753+
754+
// Get the variable declaration expression
755+
com.github.javaparser.ast.stmt.ExpressionStmtexprStmt =
756+
method.getBody().get().getStatements().get(0).asExpressionStmt();
757+
assertTrue(exprStmt.getExpression().isVariableDeclarationExpr());
758+
759+
com.github.javaparser.ast.expr.VariableDeclarationExprvarDecl =
760+
exprStmt.getExpression().asVariableDeclarationExpr();
761+
assertEquals("module",varDecl.getVariable(0).getTypeAsString());
762+
assertEquals("m",varDecl.getVariable(0).getNameAsString());
763+
764+
// Verify the initializer is an object creation expression
765+
assertTrue(varDecl.getVariable(0).getInitializer().isPresent());
766+
assertTrue(varDecl.getVariable(0).getInitializer().get().isObjectCreationExpr());
767+
768+
ObjectCreationExprobjectCreation =
769+
varDecl.getVariable(0).getInitializer().get().asObjectCreationExpr();
770+
assertEquals("module",objectCreation.getTypeAsString());
771+
assertEquals(1,objectCreation.getArguments().size());
772+
assertEquals("\"hello\"",objectCreation.getArguments().get(0).toString());
773+
}
774+
717775
privatevoidassertCompilationFails(Strings) {
718776
assertThrows(AssertionFailedError.class, () -> {
719777
CompilationUnitcu =TestParser.parseCompilationUnit(s);

‎javaparser-core-testing/src/test/java/com/github/javaparser/ast/body/TypeDeclarationTest.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
importstaticcom.github.javaparser.utils.TestParser.parseCompilationUnit;
2626
importstaticjava.util.stream.Collectors.joining;
2727
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
28+
importstaticorg.junit.jupiter.api.Assertions.assertInstanceOf;
29+
importstaticorg.junit.jupiter.api.Assertions.assertTrue;
2830

31+
importcom.github.javaparser.ast.CompilationUnit;
2932
importcom.github.javaparser.ast.Node;
33+
importcom.github.javaparser.ast.stmt.ReturnStmt;
34+
importcom.github.javaparser.ast.type.ClassOrInterfaceType;
3035
importorg.junit.jupiter.api.Test;
3136

3237
classTypeDeclarationTest {
@@ -77,6 +82,58 @@ void qualifiedNameOfDetachedClassIsEmpty() {
7782
assertFQN("?",parseBodyDeclaration("class X{}"));
7883
}
7984

85+
/**
86+
* "module" became a keyword in Java 9, but can still be used as an identifier
87+
* in certain contexts. This test verifies the AST for an enum named "module"
88+
* that also uses "module" as a return type and in a field access expression.
89+
*/
90+
@Test
91+
voidenumWithModuleAsName() {
92+
Strings ="enum module {\n"
93+
+" FOO;\n"
94+
+"\n"
95+
+" module foo() {\n"
96+
+" return module.FOO;\n"
97+
+" }\n"
98+
+"}\n";
99+
100+
CompilationUnitcu =parseCompilationUnit(s);
101+
102+
// Verify there is exactly one enum declaration
103+
assertEquals(1,cu.findAll(EnumDeclaration.class).size());
104+
105+
EnumDeclarationenumDecl =cu.findFirst(EnumDeclaration.class).get();
106+
assertEquals("module",enumDecl.getNameAsString());
107+
108+
// Verify the enum has one constant "FOO"
109+
assertEquals(1,enumDecl.getEntries().size());
110+
EnumConstantDeclarationconstant =enumDecl.getEntries().get(0);
111+
assertEquals("FOO",constant.getNameAsString());
112+
113+
// Verify the enum has one method "foo"
114+
assertEquals(1,enumDecl.getMembers().size());
115+
assertTrue(enumDecl.getMembers().get(0).isMethodDeclaration());
116+
117+
MethodDeclarationmethod =enumDecl.getMembers().get(0).asMethodDeclaration();
118+
assertEquals("foo",method.getNameAsString());
119+
120+
// Verify the return type is "module"
121+
assertInstanceOf(ClassOrInterfaceType.class,method.getType());
122+
assertEquals("module",method.getType().asClassOrInterfaceType().getNameAsString());
123+
124+
// Verify the method body contains a return statement
125+
assertTrue(method.getBody().isPresent());
126+
assertEquals(1,method.getBody().get().getStatements().size());
127+
assertTrue(method.getBody().get().getStatements().get(0).isReturnStmt());
128+
129+
ReturnStmtreturnStmt =method.getBody().get().getStatements().get(0).asReturnStmt();
130+
assertTrue(returnStmt.getExpression().isPresent());
131+
132+
// Verify the return expression is a field access "module.FOO"
133+
assertTrue(returnStmt.getExpression().get().isFieldAccessExpr());
134+
assertEquals("module.FOO",returnStmt.getExpression().get().toString());
135+
}
136+
80137
voidassertFQN(Stringfqn,Nodenode) {
81138
assertEquals(
82139
fqn,

‎javaparser-core-testing/src/test/java/com/github/javaparser/ast/expr/BinaryExprTest.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@
2121

2222
packagecom.github.javaparser.ast.expr;
2323

24+
importstaticcom.github.javaparser.StaticJavaParser.parseExpression;
2425
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
26+
importstaticorg.junit.jupiter.api.Assertions.assertInstanceOf;
2527

28+
importcom.github.javaparser.ParserConfiguration;
2629
importcom.github.javaparser.StaticJavaParser;
30+
importorg.junit.jupiter.api.BeforeEach;
2731
importorg.junit.jupiter.api.Nested;
2832
importorg.junit.jupiter.api.Test;
2933

3034
classBinaryExprTest {
3135

36+
@BeforeEach
37+
voidinitParser() {
38+
StaticJavaParser.setConfiguration(newParserConfiguration());
39+
}
40+
3241
@Test
3342
voidconvertOperator() {
3443
assertEquals(
@@ -135,4 +144,44 @@ private Expression applyBrackets(Expression expression) {
135144

136145
returnexpression;
137146
}
147+
148+
@Test
149+
voidbinaryExprWithAssertAsLeftOperandTest() {
150+
// Note: "assert" as an identifier is only valid in Java < 1.4
151+
// In modern Java, "assert" is a keyword. However, "assert" as an identifier is still supported
152+
// for backward compatibility.
153+
StaticJavaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_1_0);
154+
155+
Expressione =parseExpression("assert + 42");
156+
assertInstanceOf(BinaryExpr.class,e);
157+
BinaryExprbinary =e.asBinaryExpr();
158+
assertEquals(BinaryExpr.Operator.PLUS,binary.getOperator());
159+
160+
// Check left operand is "assert" (as identifier)
161+
assertInstanceOf(NameExpr.class,binary.getLeft());
162+
assertEquals("assert",binary.getLeft().asNameExpr().getNameAsString());
163+
164+
assertInstanceOf(IntegerLiteralExpr.class,binary.getRight());
165+
assertEquals("42",binary.getRight().asIntegerLiteralExpr().getValue());
166+
}
167+
168+
@Test
169+
voidbinaryExprWithAssertAsRightOperandTest() {
170+
// Note: "assert" as an identifier is only valid in Java < 1.4
171+
// In modern Java, "assert" is a keyword. However, "assert" as an identifier is still supported
172+
// for backward compatibility.
173+
StaticJavaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_1_0);
174+
175+
Expressione =parseExpression("x + assert");
176+
assertInstanceOf(BinaryExpr.class,e);
177+
BinaryExprbinary =e.asBinaryExpr();
178+
assertEquals(BinaryExpr.Operator.PLUS,binary.getOperator());
179+
180+
assertInstanceOf(NameExpr.class,binary.getLeft());
181+
assertEquals("x",binary.getLeft().asNameExpr().getNameAsString());
182+
183+
// Check right operand is "assert" (as identifier)
184+
assertInstanceOf(NameExpr.class,binary.getRight());
185+
assertEquals("assert",binary.getRight().asNameExpr().getNameAsString());
186+
}
138187
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp