Search in sources :

Example 11 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class CommentTest method testCombinedPackageInfoComment.

@Test
public void testCombinedPackageInfoComment() {
    Factory f = getSpoonFactory();
    CtPackage p = f.Package().get("spoon.test.comment.testclasses");
    String l_content = ((JavaOutputProcessor) f.getEnvironment().getDefaultFileGenerator()).getPrinter().printPackageInfo(p);
    String EOL = System.getProperty("line.separator");
    assertEquals("/* comment1 */" + EOL + "// comment2" + EOL + "/**" + EOL + " * Comment3" + EOL + " */" + EOL + "@java.lang.Deprecated" + EOL + "package spoon.test.comment.testclasses;" + EOL, l_content);
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 12 with Factory

use of spoon.reflect.factory.Factory 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 13 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class CommentTest method testAddCommentsToSnippet.

@Test
public void testAddCommentsToSnippet() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setCommentEnabled(true);
    CtStatement statement = factory.Code().createCodeSnippetStatement("System.out.println(\"Caenorhabditis\")");
    CtComment comment = factory.createComment("My comment on my statement", CtComment.CommentType.INLINE);
    statement.addComment(comment);
    CtExpression expression = factory.Code().createCodeSnippetExpression("\"Caenorhabditis\" + \"Caenorhabditis\"");
    CtComment commentExpression = factory.createComment("My comment on my expression", CtComment.CommentType.INLINE);
    expression.addComment(commentExpression);
    assertEquals("// My comment on my statement" + newLine + "System.out.println(\"Caenorhabditis\")", statement.toString());
    assertEquals("// My comment on my expression" + newLine + "\"Caenorhabditis\" + \"Caenorhabditis\"", expression.toString());
}
Also used : CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 14 with Factory

use of spoon.reflect.factory.Factory 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)

Example 15 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class CtTypeTest method testIsSubTypeOfonTypeReferences.

@Test
public void testIsSubTypeOfonTypeReferences() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-c" });
    launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
    CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
    O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
        nameToTypeRef.put(var.getSimpleName(), var.getType());
    });
    int[] count = new int[1];
    O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
        for (CtComment comment : ass.getComments()) {
            checkIsNotSubtype(comment, nameToTypeRef);
            count[0]++;
        }
        ;
        count[0]++;
        checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
    });
    assertTrue(count[0] > (9 * 8));
}
Also used : CtComment(spoon.reflect.code.CtComment) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtAssignment(spoon.reflect.code.CtAssignment) HashMap(java.util.HashMap) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19