Skip to content

Commit

Permalink
Make changes for handling #comment on CtComment and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshyAAAgrawal committed Jul 19, 2020
1 parent 1f0ca12 commit 91f7b6c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,9 @@ public ElementSourceFragment getOriginalSourceFragment() {
*/
public void comment() {
if (this instanceof CtStatement && getParent() instanceof CtBlock) {
if (this instanceof CtComment) {
return;
}
final String stmt = toString();
if (stmt.contains(CtComment.LINE_SEPARATOR)) {
this.replace(getFactory().Code().createComment(stmt, CtComment.CommentType.BLOCK)); // Multi line comment
Expand Down
94 changes: 64 additions & 30 deletions src/test/java/spoon/test/statementComment/StatementCommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCase;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCodeSnippetStatement;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtLoop;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.code.CtSynchronized;
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.declaration.CtClass;
Expand Down Expand Up @@ -94,7 +98,7 @@ public void testBlockStatementWithinBody(){
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m1 = allstmt.getMethod("m1");
assertTrue(m1.getBody().getStatement(4) instanceof CtBlock);
CtBlock blockWithinBody = m1.getBody().getStatement(4);
CtBlock<?> blockWithinBody = m1.getBody().getStatement(4);
blockWithinBody.comment();
assertTrue(m1.getBody().getStatement(4) instanceof CtComment);
CtComment blockAsComment = (CtComment) m1.getBody().getStatement(4);
Expand Down Expand Up @@ -144,30 +148,25 @@ public void testCaseStatement(){
CtCase<?> caseStmt = (CtCase<?>) switchStmt.getCases().get(1);
caseStmt.comment();
}

@Test
public void testFlowBreakStatement(){

}

@Test

@Test(expected = UnsupportedOperationException.class)
public void testClassStatement(){

}

@Test
public void testCodeSnippetStatement(){

Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
allstmt.comment();
}

@Test
public void testCommentStatement(){

}

@Test
public void testConstructorCallStatement(){

Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m6 = allstmt.getMethod("m6");
assertTrue(m6.getBody().getStatement(3) instanceof CtComment);
CtComment comment = (CtComment) m6.getBody().getStatement(3);
final String initialContent = comment.getContent();
comment.comment();
assertTrue(m6.getBody().getStatement(3) instanceof CtComment);
assertEquals(((CtComment) m6.getBody().getStatement(3)).getContent(), initialContent);
}

@Test
Expand All @@ -186,20 +185,32 @@ public void testIfStatement(){
"java.lang.System.out.println(\"Seems right...\");" + EOL +
"}", ifAsComment.getContent());
}

@Test
public void testInvocationStatement(){

}


@Test
public void testLocalVariableStatement(){

Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m3 = allstmt.getMethod("m3");
assertTrue(m3.getBody().getStatement(1) instanceof CtLocalVariable);
CtLocalVariable<?> locAsStmt = (CtLocalVariable<?>) m3.getBody().getStatement(1);
locAsStmt.comment();
assertTrue(m3.getBody().getStatement(1) instanceof CtComment);
CtComment comm = (CtComment) m3.getBody().getStatement(1);
assertEquals("int r = 30;", comm.getContent());
}

@Test
public void testLoopStatement(){

Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m6 = allstmt.getMethod("m6");
assertTrue(m6.getBody().getStatement(2) instanceof CtLoop);
CtLoop loopAsStmt = m6.getBody().getStatement(2);
loopAsStmt.comment();
assertTrue(m6.getBody().getStatement(2) instanceof CtComment);
assertEquals("for (int i = 0; i < 10; ++i) {" + EOL +
"java.lang.System.out.println(i);" + EOL +
"}", ((CtComment) m6.getBody().getStatement(2)).getContent());
}

@Test
Expand All @@ -223,7 +234,16 @@ public void testSwitchStatement(){

@Test
public void testSynchronousStatement(){

Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m6 = allstmt.getMethod("m6");
assertTrue(m6.getBody().getStatement(1) instanceof CtSynchronized);
CtSynchronized synchronizedAsStmt = (CtSynchronized) m6.getBody().getStatement(1);
synchronizedAsStmt.comment();
assertTrue(m6.getBody().getStatement(1) instanceof CtComment);
assertEquals("synchronized(obj) {" + EOL +
"java.lang.System.out.println(\"Executing\");" + EOL +
"}", ((CtComment) m6.getBody().getStatement(1)).getContent());
}

@Test
Expand Down Expand Up @@ -267,4 +287,18 @@ public void testUnaryOperatorStatement(){
CtComment incrementComment = m3.getBody().getStatement(2);
assertEquals("r++;", incrementComment.getContent());
}

@Test
public void testCodeSnippetStatement(){
Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m6 = allstmt.getMethod("m6");
CtCodeSnippetStatement codeSnippet = getSpoonFactory().createCodeSnippetStatement("int j = 10");
m6.getBody().insertEnd(codeSnippet);
assertTrue(m6.getBody().getStatement(4) instanceof CtCodeSnippetStatement);
codeSnippet.comment();
assertTrue(m6.getBody().getStatement(4) instanceof CtComment);
CtComment comment = (CtComment) m6.getBody().getStatement(4);
assertEquals("int j = 10;", comment.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public class AllStmtExtensions{
public AllStmtExtensions() {}

void m1() {
assert 1 == 5;
int r = 10;
Expand All @@ -13,9 +14,11 @@ void m1() {
int j = 10;
}
}

void m2() {

}

void m3() {
try {
throw new Exception();
Expand All @@ -25,6 +28,7 @@ void m3() {
int r = 30;
r++;
}

void m4() {
if (5 > 6) {
System.out.println("Impossible!");
Expand All @@ -44,4 +48,15 @@ void m5() {
System.out.println("None");
}
}

void m6() {
Object obj = new Object();
synchronized(obj) {
System.out.println("Executing");
}
for(int i = 0; i < 10; ++i) {
System.out.println(i);
}
// Hi, I am a comment
}
}

0 comments on commit 91f7b6c

Please sign in to comment.