refactor creating test suites

This commit is contained in:
Pavel Talanov
2012-02-06 16:08:17 +04:00
parent ad32f6ec49
commit e1513ae579
6 changed files with 88 additions and 77 deletions
@@ -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");
}
});
}
@@ -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);
}
}
@@ -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);
}
});
}
@@ -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;
}
}
@@ -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");
}
@@ -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));
}
}
}