-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Coverage Enhancement for 'javaparser-core/src/main/java/com/github/javaparser/ast/body' package #4181
Open
YinyinChencr7
wants to merge
2
commits into
javaparser:master
Choose a base branch
from
YinyinChencr7:testCovImprove
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Code Coverage Enhancement for 'javaparser-core/src/main/java/com/github/javaparser/ast/body' package #4181
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
...parser-core-testing/src/test/java/com/github/javaparser/ast/body/BodyDeclarationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2023 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.ast.body; | ||
|
||
import com.github.javaparser.ast.NodeList; | ||
import com.github.javaparser.ast.expr.*; | ||
import com.github.javaparser.ast.visitor.GenericVisitor; | ||
import com.github.javaparser.ast.visitor.VoidVisitor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class BodyDeclarationTest { | ||
|
||
// A concrete subclass of BodyDeclaration for testing | ||
private static class ConcreteBodyDeclaration extends BodyDeclaration<ConcreteBodyDeclaration> { | ||
@Override | ||
public ConcreteBodyDeclaration clone() { | ||
return (ConcreteBodyDeclaration) super.clone(); | ||
} | ||
|
||
@Override | ||
public <R, A> R accept(GenericVisitor<R, A> v, A arg) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public <A> void accept(VoidVisitor<A> v, A arg) { | ||
|
||
} | ||
} | ||
|
||
// Mock AnnotationExpr class for testing purposes | ||
private static class MockAnnotationExpr extends AnnotationExpr { | ||
@Override | ||
public <R, A> R accept(GenericVisitor<R, A> v, A arg) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public <A> void accept(VoidVisitor<A> v, A arg) { | ||
|
||
} | ||
} | ||
|
||
@Test | ||
void testSetAnnotations() { | ||
ConcreteBodyDeclaration decl = new ConcreteBodyDeclaration(); | ||
|
||
assertTrue(decl.getAnnotations().isEmpty()); | ||
|
||
NodeList<AnnotationExpr> annotations = new NodeList<>(new MarkerAnnotationExpr("Override")); | ||
decl.setAnnotations(annotations); | ||
|
||
assertEquals(1, decl.getAnnotations().size()); | ||
assertEquals("Override", decl.getAnnotations().get(0).getNameAsString()); | ||
} | ||
|
||
@Test | ||
void testRemoveAnnotation() { | ||
ConcreteBodyDeclaration decl = new ConcreteBodyDeclaration(); | ||
NodeList<AnnotationExpr> annotations = new NodeList<>(new MarkerAnnotationExpr("Override")); | ||
decl.setAnnotations(annotations); | ||
|
||
assertTrue(decl.remove(decl.getAnnotations().get(0))); | ||
assertTrue(decl.getAnnotations().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testReplace() { | ||
ConcreteBodyDeclaration declaration = new ConcreteBodyDeclaration(); | ||
AnnotationExpr mockAnnotation = new MockAnnotationExpr(); | ||
declaration.getAnnotations().add(mockAnnotation); | ||
AnnotationExpr newAnnotation = new MockAnnotationExpr(); | ||
assertTrue(declaration.replace(mockAnnotation, newAnnotation)); | ||
} | ||
|
||
@Test | ||
public void testIsAnnotationDeclaration() { | ||
ConcreteBodyDeclaration decl = new ConcreteBodyDeclaration(); | ||
assertFalse(decl.isAnnotationDeclaration()); | ||
} | ||
|
||
@Test | ||
public void testIfAnnotationDeclaration() { | ||
ConcreteBodyDeclaration decl = new ConcreteBodyDeclaration(); | ||
decl.ifAnnotationDeclaration(annotationDeclaration -> fail("Action should not be executed")); | ||
} | ||
|
||
@Test | ||
public void testTypeCastingMethods() { | ||
ConcreteBodyDeclaration decl = new ConcreteBodyDeclaration(); | ||
|
||
assertFalse(decl.isAnnotationDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asAnnotationDeclaration); | ||
|
||
assertFalse(decl.isAnnotationMemberDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asAnnotationMemberDeclaration); | ||
|
||
assertFalse(decl.isCallableDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asCallableDeclaration); | ||
|
||
assertFalse(decl.isClassOrInterfaceDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asClassOrInterfaceDeclaration); | ||
|
||
assertFalse(decl.isConstructorDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asConstructorDeclaration); | ||
|
||
assertFalse(decl.isCompactConstructorDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asCompactConstructorDeclaration); | ||
|
||
assertFalse(decl.isEnumConstantDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asEnumConstantDeclaration); | ||
|
||
assertFalse(decl.isEnumDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asEnumDeclaration); | ||
|
||
assertFalse(decl.isFieldDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asFieldDeclaration); | ||
|
||
assertFalse(decl.isInitializerDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asInitializerDeclaration); | ||
|
||
assertFalse(decl.isMethodDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asMethodDeclaration); | ||
|
||
assertFalse(decl.isTypeDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asTypeDeclaration); | ||
|
||
assertFalse(decl.isRecordDeclaration()); | ||
assertThrows(IllegalStateException.class, decl::asRecordDeclaration); | ||
|
||
assertEquals(Optional.empty(), decl.toAnnotationDeclaration()); | ||
assertEquals(Optional.empty(), decl.toAnnotationMemberDeclaration()); | ||
assertEquals(Optional.empty(), decl.toCallableDeclaration()); | ||
assertEquals(Optional.empty(), decl.toClassOrInterfaceDeclaration()); | ||
assertEquals(Optional.empty(), decl.toConstructorDeclaration()); | ||
assertEquals(Optional.empty(), decl.toEnumConstantDeclaration()); | ||
assertEquals(Optional.empty(), decl.toEnumDeclaration()); | ||
assertEquals(Optional.empty(), decl.toFieldDeclaration()); | ||
assertEquals(Optional.empty(), decl.toInitializerDeclaration()); | ||
assertEquals(Optional.empty(), decl.toMethodDeclaration()); | ||
assertEquals(Optional.empty(), decl.toTypeDeclaration()); | ||
assertEquals(Optional.empty(), decl.toRecordDeclaration()); | ||
assertEquals(Optional.empty(), decl.toCompactConstructorDeclaration()); | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
...er-core-testing/src/test/java/com/github/javaparser/ast/body/CallableDeclarationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2023 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.ast.body; | ||
|
||
import com.github.javaparser.ast.Modifier; | ||
import com.github.javaparser.ast.NodeList; | ||
import com.github.javaparser.ast.expr.SimpleName; | ||
import com.github.javaparser.ast.type.ArrayType; | ||
import com.github.javaparser.ast.type.PrimitiveType; | ||
import com.github.javaparser.ast.type.TypeParameter; | ||
import com.github.javaparser.ast.visitor.GenericVisitor; | ||
import com.github.javaparser.ast.visitor.VoidVisitor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CallableDeclarationTest { | ||
|
||
// A concrete subclass of CallableDeclaration for testing | ||
private static class ConcreteCallableDeclaration extends CallableDeclaration<ConcreteCallableDeclaration> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't you use Mockito here? |
||
public ConcreteCallableDeclaration () { | ||
super(new NodeList<>(), new NodeList<>(), new NodeList<>(), new SimpleName("Test"), new NodeList<>(), new NodeList<>(), null); | ||
} | ||
|
||
@Override | ||
public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, boolean includingParameterName) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public <R, A> R accept(GenericVisitor<R, A> v, A arg) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public <A> void accept(VoidVisitor<A> v, A arg) { | ||
|
||
} | ||
} | ||
|
||
@Test | ||
void testConstructor() { | ||
ConcreteCallableDeclaration decl = new ConcreteCallableDeclaration(); | ||
assertNotNull(decl); | ||
assertEquals("Test", decl.getName().getIdentifier()); | ||
} | ||
|
||
@Test | ||
void testRemove() { | ||
ConcreteCallableDeclaration decl = new ConcreteCallableDeclaration(); | ||
|
||
Modifier modToRemove = new Modifier(); | ||
decl.getModifiers().add(modToRemove); | ||
assertTrue(decl.remove(modToRemove)); | ||
assertFalse(decl.getModifiers().contains(modToRemove)); | ||
|
||
Parameter paramToRemove = new Parameter(); | ||
decl.getParameters().add(paramToRemove); | ||
assertTrue(decl.remove(paramToRemove)); | ||
assertFalse(decl.getParameters().contains(paramToRemove)); | ||
|
||
TypeParameter typeParamToRemove = new TypeParameter(); | ||
decl.getTypeParameters().add(typeParamToRemove); | ||
assertTrue(decl.remove(typeParamToRemove)); | ||
assertFalse(decl.getTypeParameters().contains(typeParamToRemove)); | ||
} | ||
|
||
@Test | ||
void testReplace() { | ||
ConcreteCallableDeclaration decl = new ConcreteCallableDeclaration(); | ||
|
||
SimpleName newName = new SimpleName("ReplacedTest"); | ||
decl.replace(decl.getName(), newName); | ||
assertEquals("ReplacedTest", decl.getName().getIdentifier()); | ||
|
||
Modifier oldModifier = new Modifier(); | ||
decl.getModifiers().add(oldModifier); | ||
Modifier newModifier = new Modifier(); | ||
decl.replace(oldModifier, newModifier); | ||
assertTrue(decl.getModifiers().contains(newModifier)); | ||
|
||
Parameter oldParam = new Parameter(); | ||
decl.getParameters().add(oldParam); | ||
Parameter newParam = new Parameter(); | ||
decl.replace(oldParam, newParam); | ||
assertTrue(decl.getParameters().contains(newParam)); | ||
|
||
TypeParameter oldTypeParam = new TypeParameter(); | ||
decl.getTypeParameters().add(oldTypeParam); | ||
TypeParameter newTypeParam = new TypeParameter(); | ||
decl.replace(oldTypeParam, newTypeParam); | ||
assertTrue(decl.getTypeParameters().contains(newTypeParam)); | ||
} | ||
|
||
|
||
@Test | ||
void testVariableArityMethod() { | ||
ConcreteCallableDeclaration decl = new ConcreteCallableDeclaration(); | ||
Parameter varArgParam = new Parameter(new ArrayType(PrimitiveType.intType()), new SimpleName("args")); | ||
varArgParam.setVarArgs(true); | ||
decl.addParameter(varArgParam); | ||
assertTrue(decl.isVariableArityMethod()); | ||
assertFalse(decl.isFixedArityMethod()); | ||
} | ||
|
||
@Test | ||
void testFixedArityMethod() { | ||
ConcreteCallableDeclaration decl = new ConcreteCallableDeclaration(); | ||
decl.addParameter(new Parameter(PrimitiveType.intType(), new SimpleName("arg1"))); | ||
assertFalse(decl.isVariableArityMethod()); | ||
assertTrue(decl.isFixedArityMethod()); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't you use Mockito