Skip to content

Commit

Permalink
test: Add a new test to check if static imports are placed after type…
Browse files Browse the repository at this point in the history
… imports (#1572)

fix #1439
  • Loading branch information
surli authored and monperrus committed Oct 5, 2017
1 parent acc853b commit 2374295
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import spoon.test.imports.testclasses.Mole;
import spoon.test.imports.testclasses.NotImportExecutableType;
import spoon.test.imports.testclasses.Pozole;
import spoon.test.imports.testclasses.StaticNoOrdered;
import spoon.test.imports.testclasses.SubClass;
import spoon.test.imports.testclasses.Tacos;
import spoon.test.imports.testclasses.internal.ChildClass;
Expand Down Expand Up @@ -1162,4 +1163,53 @@ public void testSortingOfImports() {
}
assertTrue(countOfImports>10);
}

@Test
public void testSortImportPutStaticImportAfterTypeImport() {
//contract: static import should be after import
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setShouldCompile(true);
String outputDir = "./target/spoon-sort-import";
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/StaticNoOrdered.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.run();

PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get(StaticNoOrdered.class);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);

prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();

StringTokenizer st = new StringTokenizer(output, System.getProperty("line.separator"));

int countImports = 0;

int nbStaticImports = 2;
int nbStandardImports = 4;

boolean startStatic = false;

while (st.hasMoreTokens()) {
String line = st.nextToken();

if (line.startsWith("import static")) {
if (!startStatic) {
assertEquals("Static import should start after exactly "+nbStandardImports+" standard imports", nbStandardImports, countImports);
} else {
assertTrue("It will normally have only "+nbStaticImports+" static imports", countImports <= nbStandardImports+nbStaticImports);
}
startStatic = true;
assertTrue("Static import should be after normal import", countImports >= nbStandardImports);
}

if (line.startsWith("import")) {
countImports++;
}
}

assertEquals("Exactly "+nbStandardImports+nbStaticImports+" should have been counted.", (nbStandardImports+nbStaticImports), countImports);
}
}
40 changes: 40 additions & 0 deletions src/test/java/spoon/test/imports/testclasses/StaticNoOrdered.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package spoon.test.imports.testclasses;


import static org.junit.Assert.assertEquals;
import org.junit.Test;
import static java.nio.charset.Charset.forName;
import java.lang.annotation.Annotation;
import org.junit.Assert;
import java.nio.charset.Charset;


/**
* Created by urli on 04/10/2017.
*/
public class StaticNoOrdered {

public void testMachin() {
assertEquals("bla","truc");
Test test = new Test() {
@Override
public Class<? extends Annotation> annotationType() {
return null;
}

@Override
public Class<? extends Throwable> expected() {
return null;
}

@Override
public long timeout() {
return 0;
}
};
}

public void anotherStaticImoport() {
Charset charset = forName("utf-8");
}
}

0 comments on commit 2374295

Please sign in to comment.