Skip to content

Commit

Permalink
test: Add tests for comment association (#5169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuplyx authored Apr 21, 2023
1 parent cd90d7a commit 2f6ff0b
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
78 changes: 77 additions & 1 deletion src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2006-2018 INRIA and contributors
* Copyright (C) 2006-2023 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
Expand All @@ -24,6 +24,7 @@
import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCatch;
Expand All @@ -46,6 +47,7 @@
import spoon.reflect.code.CtSwitch;
import spoon.reflect.code.CtSynchronized;
import spoon.reflect.code.CtTry;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnnotationType;
import spoon.reflect.declaration.CtAnonymousExecutable;
Expand All @@ -61,6 +63,7 @@
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.FactoryImpl;
Expand All @@ -71,6 +74,7 @@
import spoon.support.StandardEnvironment;
import spoon.support.compiler.jdt.JDTSnippetCompiler;
import spoon.support.reflect.code.CtCommentImpl;
import spoon.test.GitHubIssue;
import spoon.test.comment.testclasses.BlockComment;
import spoon.test.comment.testclasses.Comment1;
import spoon.test.comment.testclasses.Comment2;
Expand Down Expand Up @@ -1249,4 +1253,76 @@ public void testCatchComments(CtModel model) {
assertEquals(1, catches.get(0).getBody().getComments().size());
assertEquals("second comment", catches.get(0).getBody().getComments().get(0).getContent());
}

@ModelTest("./src/test/java/spoon/test/comment/testclasses/AnnotationComments.java")
@GitHubIssue(issueNumber = 2482, fixed = false)
public void testAnnotationComments(CtModel model) {
//contract: comments at annotations should be properly added to the AST
List<CtComment> comments = model.getElements(new TypeFilter<>(CtComment.class));
List<CtAnnotation<?>> annotations = model.getElements(new TypeFilter<>(CtAnnotation.class));

assertEquals(3, comments.size());
assertEquals("comment 1", comments.get(0).getContent());
assertEquals("comment 2", comments.get(1).getContent());
assertEquals("comment 3", comments.get(2).getContent());

assertEquals(1, annotations.get(0).getComments().size());
assertEquals("comment 1", annotations.get(0).getComments().get(0).getContent());
assertEquals(1, annotations.get(1).getComments().size());
assertEquals("comment 2", annotations.get(1).getComments().get(0).getContent());
assertEquals(1, annotations.get(2).getComments().size());
assertEquals("comment 3", annotations.get(2).getComments().get(0).getContent());
}

@ModelTest("./src/test/java/spoon/test/comment/testclasses/ArrayAccessComments.java")
@GitHubIssue(issueNumber = 2482, fixed = false)
public void testArrayAccessComments(CtModel model) {
//contract: comments at array accesses should be properly added to the AST
List<CtComment> comments = model.getElements(new TypeFilter<>(CtComment.class));
List<CtArrayAccess<?, ?>> arrayAccesses = model.getElements(new TypeFilter<>(CtArrayAccess.class));

assertEquals(2,comments.size());
assertEquals("comment 1", comments.get(0).getContent());
assertEquals("comment 2", comments.get(1).getContent());

assertEquals(1, arrayAccesses.get(0).getComments().size());
assertEquals("comment 1", arrayAccesses.get(0).getComments().get(0).getContent());
assertEquals(1, arrayAccesses.get(1).getComments().size());
assertEquals("comment 2", arrayAccesses.get(1).getComments().get(0).getContent());
}

@ModelTest("./src/test/java/spoon/test/comment/testclasses/BinaryOperatorComments.java")
@GitHubIssue(issueNumber = 2482, fixed = false)
public void testBinaryOperatorComments(CtModel model) {
//contract: comments at binary operators should be properly added to the AST
List<CtComment> comments = model.getElements(new TypeFilter<>(CtComment.class));
List<CtBinaryOperator<?>> binaryOperators = model.getElements(new TypeFilter<>(CtBinaryOperator.class));

assertEquals(1, comments.size());
assertEquals("comment 1", comments.get(0).getContent());

assertEquals(1, binaryOperators.get(0).getComments().size());
assertEquals("comment 1", binaryOperators.get(0).getComments().get(0).getContent());
}

@ModelTest("./src/test/java/spoon/test/comment/testclasses/TypeParameterComments.java")
@GitHubIssue(issueNumber = 2482, fixed = false)
public void testTypeParameterComments(CtModel model) {
//contract: comments at type parameters should be properly added to the AST
List<CtComment> comments = model.getElements(new TypeFilter<>(CtComment.class));
List<CtTypeParameter> typeParameters = model.getElements(new TypeFilter<>(CtTypeParameter.class));

assertEquals(4, comments.size());
assertEquals("comment 1", comments.get(0).getContent());
assertEquals("comment 2", comments.get(1).getContent());
assertEquals("comment 3", comments.get(2).getContent());
assertEquals("comment 4", comments.get(3).getContent());

assertEquals(1, typeParameters.get(0).getComments().size());
assertEquals("comment 1", typeParameters.get(0).getComments().get(0).getContent());
assertEquals(3, typeParameters.get(1).getComments().size());
assertEquals("comment 2", typeParameters.get(1).getComments().get(0).getContent());
assertEquals("comment 3", typeParameters.get(1).getComments().get(1).getContent());
assertEquals("comment 4", typeParameters.get(1).getComments().get(2).getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package spoon.test.comment.testclasses;

@SuppressWarnings/*comment 1*/("unused")
public class AnnotationComments {
@SuppressWarnings/*comment 2*/("unused")
public String foo = "bar";

@SuppressWarnings/*comment 3*/("unused")
public void bar(){

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package spoon.test.comment.testclasses;

public class ArrayAccessComments {

public void bar(int[] foo)
{
foo// comment 1
[1] = 0;
int bar = foo // comment 2
[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package spoon.test.comment.testclasses;

public class BinaryOperatorComments {

public void foo(int bar)
{
if (!(bar < 0
// comment 1
))
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package spoon.test.comment.testclasses;

public class TypeParameterComments <T /*comment 1*/, S /*comment 2*/ extends /*comment 3*/String/*comment 4*/> {

}

0 comments on commit 2f6ff0b

Please sign in to comment.