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);
}
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());
}
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());
}
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());
}
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));
}
Aggregations