Search in sources :

Example 6 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CommentTest method testRemoveComment.

@Test
public void testRemoveComment() {
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(InlineComment.class);
    List<CtComment> comments = type.getComments();
    assertEquals(3, comments.size());
    type.removeComment(comments.get(0));
    assertEquals(2, type.getComments().size());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) InlineComment(spoon.test.comment.testclasses.InlineComment) Test(org.junit.Test)

Example 7 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CommentTest method testWildComments.

@Test
public void testWildComments() {
    // contract: tests that value of comment is correct even for wild combinations of characters. See WildComments class for details
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(WildComments.class);
    List<CtLiteral<String>> literals = (List) ((CtNewArray<?>) type.getField("comments").getDefaultExpression()).getElements();
    assertTrue(literals.size() > 10);
    /*
		 * each string literal has a comment and string value, which defines expected value of it's comment
		 */
    for (CtLiteral<String> literal : literals) {
        assertEquals(1, literal.getComments().size());
        CtComment comment = literal.getComments().get(0);
        String expected = literal.getValue();
        assertEquals(literal.getPosition().toString(), expected, comment.getContent());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) CtLiteral(spoon.reflect.code.CtLiteral) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) List(java.util.List) ArrayList(java.util.ArrayList) WildComments(spoon.test.comment.testclasses.WildComments) Test(org.junit.Test)

Example 8 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CommentTest method testJavaDocEmptyCommentAndTag.

@Test
public void testJavaDocEmptyCommentAndTag() {
    // the sources are checked out with \n even on Windows.
    String EOL = "\n";
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(JavaDocEmptyCommentAndTags.class);
    CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
    // contract: content is never null
    assertNotNull(classJavaDoc.getContent());
    // contract: empty content is ""
    assertEquals("", classJavaDoc.getContent());
    CtJavaDoc methodJavaDoc = (CtJavaDoc) type.getMethodsByName("m").get(0).getComments().get(1);
    // contract: content is never null
    assertNotNull(methodJavaDoc.getContent());
    // contract: empty content is ""
    assertEquals("", methodJavaDoc.getContent());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtJavaDoc(spoon.reflect.code.CtJavaDoc) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) JavaDocEmptyCommentAndTags(spoon.test.comment.testclasses.JavaDocEmptyCommentAndTags) Test(org.junit.Test)

Example 9 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CommentTest method testCommentsInResourcesWithWindowsEOL.

@Test
public void testCommentsInResourcesWithWindowsEOL() throws IOException {
    // contract: the WindowsEOL.java contains MS Windows \r\n as EOL
    try (InputStream is = new FileInputStream(new File("./src/test/java/spoon/test/comment/testclasses/WindowsEOL.java"))) {
        int b;
        boolean lastWasCR = false;
        while ((b = is.read()) != -1) {
            if (lastWasCR) {
                // next must be LF
                assertTrue(b == '\n');
                lastWasCR = false;
            }
            if (b == '\r') {
                lastWasCR = true;
            }
        }
    }
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/comment/testclasses/WindowsEOL.java", "-o", "./target/spooned/", "-c" });
    Factory f = launcher.getFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(WindowsEOL.class);
    CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
    // contract: test that java doc is printed correctly
    String str = classJavaDoc.toString();
    StringTokenizer st = new StringTokenizer(str, System.getProperty("line.separator"));
    boolean first = true;
    while (st.hasMoreTokens()) {
        String line = st.nextToken();
        if (first) {
            // first
            first = false;
            assertTrue(line.length() == 3);
            assertEquals("/**", line);
        } else {
            if (st.hasMoreTokens()) {
                // in the middle
                assertTrue(line.length() >= 2);
                assertEquals(" *", line.substring(0, 2));
            } else {
                // last
                assertTrue(line.length() == 3);
                assertEquals(" */", line.substring(0, 3));
            }
        }
    }
    // This test passes on MS Windows too - why spoon uses `\n` on MS Windows too?
    assertEquals("This file contains MS Windows EOL.\n" + "It is here to test whether comments are printed well\n" + "in this case", classJavaDoc.getContent());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FileInputStream(java.io.FileInputStream) CtClass(spoon.reflect.declaration.CtClass) StringTokenizer(java.util.StringTokenizer) CtJavaDoc(spoon.reflect.code.CtJavaDoc) Launcher(spoon.Launcher) WindowsEOL(spoon.test.comment.testclasses.WindowsEOL) File(java.io.File) Test(org.junit.Test)

Example 10 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CommentTest method testJavadocShortAndLongComment.

@Test
public void testJavadocShortAndLongComment() {
    // contract: in case we cannot determine if it is a short comment, we take the whole content
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(OtherJavaDoc.class);
    CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
    assertEquals("A short description without a proper end", classJavaDoc.getShortDescription());
    assertEquals("A short description without a proper end", classJavaDoc.getLongDescription());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtJavaDoc(spoon.reflect.code.CtJavaDoc) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) OtherJavaDoc(spoon.test.comment.testclasses.OtherJavaDoc) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14