Skip to content

Commit

Permalink
feature:Print Comment in CtSnippet (#1101)
Browse files Browse the repository at this point in the history
* fix default printer

* add test cases
  • Loading branch information
danglotb authored and surli committed Jan 11, 2017
1 parent c17598e commit 41b2895
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1675,11 +1675,13 @@ public void visitCtWhile(CtWhile whileLoop) {

@Override
public <T> void visitCtCodeSnippetExpression(CtCodeSnippetExpression<T> expression) {
elementPrinterHelper.writeComment(expression);
printer.write(expression.getValue());
}

@Override
public void visitCtCodeSnippetStatement(CtCodeSnippetStatement statement) {
elementPrinterHelper.writeComment(statement);
printer.write(statement.getValue());
}

Expand Down
24 changes: 22 additions & 2 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.declaration.ParentNotInitializedException;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.FactoryImpl;
import spoon.reflect.visitor.AstParentConsistencyChecker;
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.DefaultCoreFactory;
Expand Down Expand Up @@ -559,6 +557,28 @@ public void testSnippedWithComments(){
assertEquals("inline comment", returnSt.getComments().get(1).getContent());
}

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

@Test
public void testDocumentationContract() throws Exception {
// contract: all metamodel classes must be commented with an example.
Expand Down

0 comments on commit 41b2895

Please sign in to comment.