From 7513ba314bdb172b90b5a8cf6d9d99a5fe1c50b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lars=C3=A9n?= Date: Wed, 12 May 2021 13:48:43 +0200 Subject: [PATCH 1/2] Migrate TestSnipePriter to JUnit5 --- .../test/prettyprinter/TestSniperPrinter.java | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java b/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java index 31a3e002f44..f1ea3c34812 100644 --- a/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java +++ b/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java @@ -7,14 +7,12 @@ */ package spoon.test.prettyprinter; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import spoon.Launcher; import spoon.SpoonException; import spoon.refactoring.Refactoring; import spoon.reflect.CtModel; -import spoon.reflect.code.CtBlock; import spoon.reflect.code.CtConstructorCall; import spoon.reflect.code.CtCodeSnippetExpression; import spoon.reflect.code.CtExpression; @@ -60,43 +58,39 @@ import java.util.List; import java.util.function.BiConsumer; import java.util.function.Consumer; -import java.util.function.Function; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class TestSniperPrinter { - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - @Test - public void testClassRename1() throws Exception { + public void testClassRename1(@TempDir File tempDir) throws Exception { // contract: one can sniper out of the box after Refactoring.changeTypeName - testClassRename(type -> { + testClassRename(tempDir, type -> { Refactoring.changeTypeName(type, "Bar"); }); } @Test - public void testClassRename2() throws Exception { + public void testClassRename2(@TempDir File tempDir) throws Exception { // contract: one can sniper after setSimpleName // with the necessary tweaks - testClassRename(type -> { + testClassRename(tempDir, type -> { type.setSimpleName("Bar"); type.getFactory().CompilationUnit().addType(type); }); } - public void testClassRename(Consumer> renameTransfo) throws Exception { + public void testClassRename(File tempdir, Consumer> renameTransfo) throws Exception { // contract: sniper supports class rename String testClass = ToBeChanged.class.getName(); Launcher launcher = new Launcher(); @@ -104,7 +98,7 @@ public void testClassRename(Consumer> renameTransfo) throws Exception launcher.getEnvironment().setPrettyPrinterCreator(() -> { return new SniperJavaPrettyPrinter(launcher.getEnvironment()); }); - launcher.setBinaryOutputDirectory(folder.newFolder()); + launcher.setBinaryOutputDirectory(tempdir); launcher.buildModel(); Factory f = launcher.getFactory(); @@ -117,7 +111,7 @@ public void testClassRename(Consumer> renameTransfo) throws Exception String contentOfPrettyPrintedClassFromDisk = getContentOfPrettyPrintedClassFromDisk(type); - assertTrue(contentOfPrettyPrintedClassFromDisk, contentOfPrettyPrintedClassFromDisk.contains("EOLs*/ Bar")); + assertTrue(contentOfPrettyPrintedClassFromDisk.contains("EOLs*/ Bar"), contentOfPrettyPrintedClassFromDisk); } @@ -559,7 +553,7 @@ public void testAddedImportStatementPlacedOnSeparateLineInFileWithoutPackageStat Consumer> addArrayListImport = type -> { Factory factory = type.getFactory(); - assertTrue("there should be no package statement in this test file", type.getPackage().isUnnamedPackage()); + assertTrue(type.getPackage().isUnnamedPackage(), "there should be no package statement in this test file"); CtCompilationUnit cu = factory.CompilationUnit().getOrCreate(type); CtTypeReference arrayListRef = factory.Type().get(java.util.ArrayList.class).getReference(); cu.getImports().add(factory.createImport(arrayListRef)); @@ -580,7 +574,7 @@ public void testAddedImportStatementPlacedOnSeparateLineInFileWithPackageStateme Consumer> addArrayListImport = type -> { Factory factory = type.getFactory(); - assertFalse("there should be a package statement in this test file", type.getPackage().isUnnamedPackage()); + assertFalse(type.getPackage().isUnnamedPackage(), "there should be a package statement in this test file"); CtCompilationUnit cu = factory.CompilationUnit().getOrCreate(type); CtTypeReference arrayListRef = factory.Type().get(java.util.ArrayList.class).getReference(); cu.getImports().add(factory.createImport(arrayListRef)); @@ -870,8 +864,8 @@ public void testToStringWithSniperPrinter(String inputSourcePath) throws Excepti assertTrue(result.length() > 0); } - assertTrue("ToString() on element (" + el.getClass().getName() + ") = \"" + el + "\" is not in original content", - originalContent.contains(result.replace("\t", ""))); + assertTrue(originalContent.contains(result.replace("\t", "")), + "ToString() on element (" + el.getClass().getName() + ") = \"" + el + "\" is not in original content"); } catch (UnsupportedOperationException | SpoonException e) { //Printer should not throw exception on printable element. (Unless there is a bug in the printer...) fail("ToString() on Element (" + el.getClass().getName() + "): at " + el.getPath() + " lead to an exception: " + e); @@ -884,4 +878,9 @@ public void testToStringWithSniperOnElementScan() throws Exception { testToStringWithSniperPrinter("src/test/java/spoon/test/prettyprinter/testclasses/ElementScan.java"); } + @org.junit.Test + public void test() { + + } + } From c4da2e6fcfcd19fc910db7aae5cef0cdf4a62e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lars=C3=A9n?= Date: Wed, 12 May 2021 13:50:10 +0200 Subject: [PATCH 2/2] Remove accidentally added test case --- .../java/spoon/test/prettyprinter/TestSniperPrinter.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java b/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java index f1ea3c34812..27c7061f624 100644 --- a/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java +++ b/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java @@ -877,10 +877,4 @@ public void testToStringWithSniperPrinter(String inputSourcePath) throws Excepti public void testToStringWithSniperOnElementScan() throws Exception { testToStringWithSniperPrinter("src/test/java/spoon/test/prettyprinter/testclasses/ElementScan.java"); } - - @org.junit.Test - public void test() { - - } - }