-
-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Avoid import duplication on unresolved imports (#3730)
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/spoon/reflect/visitor/ImportCleanerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package spoon.reflect.visitor; | ||
|
||
import org.junit.Test; | ||
import spoon.Launcher; | ||
import spoon.reflect.CtModel; | ||
import spoon.reflect.declaration.CtCompilationUnit; | ||
import spoon.reflect.declaration.CtImport; | ||
import spoon.reflect.declaration.CtType; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class ImportCleanerTest { | ||
|
||
@Test | ||
public void testDoesNotDuplicateUnresolvedImports() { | ||
// contract: The import cleaner should not duplicate unresolved imports | ||
|
||
// arrange | ||
Launcher launcher = new Launcher(); | ||
launcher.addInputResource("./src/test/resources/unresolved/UnresolvedImport.java"); | ||
CtModel model = launcher.buildModel(); | ||
CtType<?> type = model.getAllTypes().stream().findFirst().get(); | ||
CtCompilationUnit cu = type.getFactory().CompilationUnit().getOrCreate(type); | ||
List<String> importsBefore = getTextualImports(cu); | ||
|
||
// act | ||
new ImportCleaner().process(cu); | ||
|
||
// assert | ||
List<String> importsAfter = getTextualImports(cu); | ||
assertThat(importsAfter, equalTo(importsBefore)); | ||
} | ||
|
||
private static List<String> getTextualImports(CtCompilationUnit cu) { | ||
return cu.getImports().stream() | ||
.map(CtImport::toString) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// this import will be unresolved as the package does not exist | ||
import non.existing.pkg.SomeClass; | ||
|
||
public class UnresolvedImport { | ||
public static void main(String[] args) { | ||
SomeClass instance = new SomeClass(); | ||
} | ||
} |