From e1513ae579a208b96bdf99a5f2cab97b84695b09 Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Mon, 6 Feb 2012 16:08:17 +0400 Subject: [PATCH] refactor creating test suites --- .../jetbrains/k2js/test/ExampleTestSuite.java | 38 ++--------- .../jetbrains/k2js/test/MultiFileTest.java | 2 +- .../jetbrains/k2js/test/SimpleTestSuite.java | 36 ++--------- .../test/org/jetbrains/k2js/test/Suite.java | 63 +++++++++++++++++++ .../jetbrains/k2js/test/TranslationTest.java | 14 ++--- .../k2js/test/TranslatorTestCaseBuilder.java | 12 ++-- 6 files changed, 88 insertions(+), 77 deletions(-) create mode 100644 translator/test/org/jetbrains/k2js/test/Suite.java diff --git a/translator/test/org/jetbrains/k2js/test/ExampleTestSuite.java b/translator/test/org/jetbrains/k2js/test/ExampleTestSuite.java index 7a7528448e8..e66aee41e09 100644 --- a/translator/test/org/jetbrains/k2js/test/ExampleTestSuite.java +++ b/translator/test/org/jetbrains/k2js/test/ExampleTestSuite.java @@ -1,44 +1,16 @@ package org.jetbrains.k2js.test; +import com.intellij.testFramework.UsefulTestCase; import junit.framework.Test; import org.jetbrains.annotations.NotNull; -public final class ExampleTestSuite extends TranslationTest { - - - final private static String MAIN = "examples/"; - - @Override - protected String mainDirectory() { - return MAIN; - } - - public void runBoxTest(String filename) throws Exception { - testFunctionOutput(filename, "Anonymous", "box", "OK"); - } - - private String name; - - public ExampleTestSuite(String name) { - this.name = name; - } - - @Override - public String getName() { - return name; - } - - public void runTest() throws Exception { - runBoxTest(getName()); - } +public final class ExampleTestSuite extends UsefulTestCase { public static Test suite() { - return TranslatorTestCaseBuilder.suiteForDirectory("translator", - "/testFiles/examples/cases/", true, new TranslatorTestCaseBuilder.NamedTestFactory() { - @NotNull + return Suite.suiteForDirectory("examples/", new Suite.SingleFileTester() { @Override - public Test createTest(@NotNull String dataPath, @NotNull String name) { - return (new ExampleTestSuite(name)); + public void performTest(@NotNull Suite test, @NotNull String filename) throws Exception { + test.testFunctionOutput(filename, "Anonymous", "box", "OK"); } }); } diff --git a/translator/test/org/jetbrains/k2js/test/MultiFileTest.java b/translator/test/org/jetbrains/k2js/test/MultiFileTest.java index 9d7aaf46394..354bff6824b 100644 --- a/translator/test/org/jetbrains/k2js/test/MultiFileTest.java +++ b/translator/test/org/jetbrains/k2js/test/MultiFileTest.java @@ -21,7 +21,7 @@ public final class MultiFileTest extends TranslationTest { } @Override - protected void testFooBoxIsTrue(String dirName) throws Exception { + public void testFooBoxIsTrue(String dirName) throws Exception { testMultiFile(dirName, "foo", "box", true); } } diff --git a/translator/test/org/jetbrains/k2js/test/SimpleTestSuite.java b/translator/test/org/jetbrains/k2js/test/SimpleTestSuite.java index 9339032d609..924262526a1 100644 --- a/translator/test/org/jetbrains/k2js/test/SimpleTestSuite.java +++ b/translator/test/org/jetbrains/k2js/test/SimpleTestSuite.java @@ -1,45 +1,19 @@ package org.jetbrains.k2js.test; +import com.intellij.testFramework.UsefulTestCase; import junit.framework.Test; import org.jetbrains.annotations.NotNull; /** * @author Pavel Talanov */ -public final class SimpleTestSuite extends TranslationTest { - final private static String MAIN = "simple/"; - - @Override - protected String mainDirectory() { - return MAIN; - } - - public void runBoxTest(String filename) throws Exception { - testFooBoxIsTrue(filename); - } - - private String name; - - public SimpleTestSuite(String name) { - this.name = name; - } - - @Override - public String getName() { - return name; - } - - public void runTest() throws Exception { - runBoxTest(getName()); - } +public final class SimpleTestSuite extends UsefulTestCase { public static Test suite() { - return TranslatorTestCaseBuilder.suiteForDirectory("translator", - "/testFiles/simple/cases/", true, new TranslatorTestCaseBuilder.NamedTestFactory() { - @NotNull + return Suite.suiteForDirectory("simple/", new Suite.SingleFileTester() { @Override - public Test createTest(@NotNull String dataPath, @NotNull String name) { - return (new SimpleTestSuite(name)); + public void performTest(@NotNull Suite test, @NotNull String filename) throws Exception { + test.testFooBoxIsTrue(filename); } }); } diff --git a/translator/test/org/jetbrains/k2js/test/Suite.java b/translator/test/org/jetbrains/k2js/test/Suite.java new file mode 100644 index 00000000000..6ac98095385 --- /dev/null +++ b/translator/test/org/jetbrains/k2js/test/Suite.java @@ -0,0 +1,63 @@ +package org.jetbrains.k2js.test; + +import junit.framework.Test; +import junit.framework.TestResult; +import org.jetbrains.annotations.NotNull; + +/** + * @author Pavel Talanov + */ +public final class Suite extends TranslationTest { + + private String name; + private final SingleFileTester tester; + private final String testMain; + + public Suite(@NotNull String testName, + @NotNull String suiteDirName, + @NotNull final SingleFileTester tester) { + this.name = testName; + this.tester = tester; + this.testMain = suiteDirName; + } + + @Override + protected String mainDirectory() { + return testMain; + } + + public void runTest() throws Exception { + tester.performTest(this, name); + } + + public static Test suite() { + return new Test() { + @Override + public int countTestCases() { + return 0; + } + + @Override + public void run(TestResult testResult) { + //do nothing + } + }; + } + + public static Test suiteForDirectory(@NotNull final String mainName, @NotNull final SingleFileTester testMethod) { + + return TranslatorTestCaseBuilder.suiteForDirectory("translator\\testFiles\\", + mainName + casesDirectoryName(), true, new TranslatorTestCaseBuilder.NamedTestFactory() { + @NotNull + @Override + public Test createTest(@NotNull String name) { + return (new Suite(name, mainName, testMethod)); + } + }); + } + + protected interface SingleFileTester { + void performTest(@NotNull Suite test, @NotNull String filename) throws Exception; + + } +} diff --git a/translator/test/org/jetbrains/k2js/test/TranslationTest.java b/translator/test/org/jetbrains/k2js/test/TranslationTest.java index 20725fc3fa6..3136f468cc3 100644 --- a/translator/test/org/jetbrains/k2js/test/TranslationTest.java +++ b/translator/test/org/jetbrains/k2js/test/TranslationTest.java @@ -28,20 +28,20 @@ public abstract class TranslationTest extends BaseTest { private static final String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js"; private static final String EXPECTED = "expected/"; - protected abstract String mainDirectory(); - protected String kotlinLibraryPath() { return KOTLIN_JS_LIB; } - private String casesDirectoryName() { + protected static String casesDirectoryName() { return CASES; } - private String outDirectoryName() { + private static String outDirectoryName() { return OUT; } + protected abstract String mainDirectory(); + private String testFilesPath() { return TEST_FILES + suiteDirectoryName() + mainDirectory(); } @@ -54,7 +54,7 @@ public abstract class TranslationTest extends BaseTest { return testFilesPath() + outDirectoryName(); } - private String getInputPath() { + protected String getInputPath() { return testFilesPath() + casesDirectoryName(); } @@ -136,11 +136,11 @@ public abstract class TranslationTest extends BaseTest { Context.exit(); } - protected void testFooBoxIsTrue(String filename) throws Exception { + public void testFooBoxIsTrue(String filename) throws Exception { testFunctionOutput(filename, "foo", "box", true); } - protected void testFooBoxIsOk(String filename) throws Exception { + public void testFooBoxIsOk(String filename) throws Exception { testFunctionOutput(filename, "foo", "box", "OK"); } diff --git a/translator/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java b/translator/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java index f10fc869a63..ba84f74a1ca 100644 --- a/translator/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java +++ b/translator/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java @@ -12,7 +12,7 @@ import java.util.Collections; import java.util.List; /** - * @author PavelTalanov + * @author Pavel Talanov */ public abstract class TranslatorTestCaseBuilder { public static FilenameFilter emptyFilter = new FilenameFilter() { @@ -24,16 +24,18 @@ public abstract class TranslatorTestCaseBuilder { public interface NamedTestFactory { @NotNull - Test createTest(@NotNull String dataPath, @NotNull String name); + Test createTest(@NotNull String filename); } @NotNull - public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, @NotNull NamedTestFactory factory) { + public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, + boolean recursive, @NotNull NamedTestFactory factory) { return suiteForDirectory(baseDataDir, dataPath, recursive, emptyFilter, factory); } @NotNull - public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, final FilenameFilter filter, @NotNull NamedTestFactory factory) { + public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, + final FilenameFilter filter, @NotNull NamedTestFactory factory) { TestSuite suite = new TestSuite(dataPath); appendTestsInDirectory(baseDataDir, dataPath, recursive, filter, factory, suite); return suite; @@ -82,7 +84,7 @@ public abstract class TranslatorTestCaseBuilder { String fileName = file.getName(); assert fileName != null; String extension = fileName.endsWith(extensionJet) ? extensionJet : extensionKt; - suite.addTest(factory.createTest(dataPath, fileName)); + suite.addTest(factory.createTest(fileName)); } } }