Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: test: check if static imports are placed after type imports #1572

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imoport -> Import

Charset charset = forName("utf-8");
}
}