Move TestConfig to js.tests module. Remove K2JSTranslationUtils. Remove core.Dummy class. TestConfig references library files using project file structure instead of getResourceAsStream.

This commit is contained in:
pTalanov
2012-02-28 15:18:16 +04:00
parent 8c2d20f303
commit 57c9de776e
5 changed files with 41 additions and 144 deletions
@@ -0,0 +1,107 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.test;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Pavel Talanov
*/
//TODO: review/refactor
public final class TestConfig extends Config {
@NotNull
private static final List<String> LIB_FILE_NAMES = Arrays.asList(
"/core/annotations.kt",
"/jquery/common.kt",
"/jquery/ui.kt",
"/core/javautil.kt",
"/core/javalang.kt",
"/core/core.kt",
"/core/math.kt",
"/core/json.kt",
"/raphael/raphael.kt",
"/html5/canvas.kt",
"/html5/files.kt",
"/html5/image.kt"
);
private static final String LIBRARIES_LOCATION = "js.libraries/src";
@Nullable
private /*var*/ List<JetFile> jsLibFiles = null;
@NotNull
private final Project project;
public TestConfig(@NotNull Project project) {
this.project = project;
}
@NotNull
@Override
public Project getProject() {
return project;
}
@NotNull
private static List<JetFile> initLibFiles(@NotNull Project project) {
List<JetFile> libFiles = new ArrayList<JetFile>();
for (String libFileName : LIB_FILE_NAMES) {
JetFile file = null;
//TODO: close stream?
try {
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
InputStream stream = new FileInputStream(LIBRARIES_LOCATION + libFileName);
try {
String text = FileUtil.loadTextAndClose(stream);
file = JetFileUtils.createPsiFile(libFileName, text, project);
} catch (IOException e) {
e.printStackTrace();
}
libFiles.add(file);
} catch (FileNotFoundException e) {
//TODO: throw generic expception
throw new AssertionError("Lib files not found.");
}
}
return libFiles;
}
@NotNull
public List<JetFile> getLibFiles() {
if (jsLibFiles == null) {
jsLibFiles = initLibFiles(getProject());
}
return jsLibFiles;
}
}
@@ -19,7 +19,6 @@ package org.jetbrains.k2js.test;
import com.google.dart.compiler.backend.js.ast.JsProgram;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.TestConfig;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
@@ -51,20 +50,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/";
public void testTranslateFile(@NotNull String inputFile,
@NotNull String outputFile) throws Exception {
testTranslateFiles(Collections.singletonList(inputFile), outputFile);
public void translateFile(@NotNull String inputFile,
@NotNull String outputFile) throws Exception {
traslateFiles(Collections.singletonList(inputFile), outputFile);
}
public void testTranslateFiles(@NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception {
public void traslateFiles(@NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception {
K2JSTranslator translator = new K2JSTranslator(new TestConfig(getProject()));
List<JetFile> psiFiles = createPsiFileList(inputFiles, getProject());
JsProgram program = translator.generateProgram(psiFiles);
K2JSTranslator.saveProgramToFile(outputFile, program);
}
protected String kotlinLibraryPath() {
protected static String kotlinLibraryPath() {
return KOTLIN_JS_LIB;
}
@@ -78,7 +77,7 @@ public abstract class TranslationTest extends BaseTest {
protected abstract String mainDirectory();
private String testFilesPath() {
private String pathToTestFiles() {
return TEST_FILES + suiteDirectoryName() + mainDirectory();
}
@@ -87,17 +86,18 @@ public abstract class TranslationTest extends BaseTest {
}
private String getOutputPath() {
return testFilesPath() + outDirectoryName();
return pathToTestFiles() + outDirectoryName();
}
protected String getInputPath() {
return testFilesPath() + casesDirectoryName();
return pathToTestFiles() + casesDirectoryName();
}
private String getExpectedPath() {
return testFilesPath() + expectedDirectoryName();
return pathToTestFiles() + expectedDirectoryName();
}
@SuppressWarnings("MethodMayBeStatic")
private String expectedDirectoryName() {
return EXPECTED;
}
@@ -106,19 +106,19 @@ public abstract class TranslationTest extends BaseTest {
String functionName, Object expectedResult) throws Exception {
translateFile(filename);
runRhinoTest(generateFilenameList(getOutputFilePath(filename)),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
}
protected void testMultiFile(String dirName, String namespaceName,
String functionName, Object expectedResult) throws Exception {
translateFilesInDir(dirName);
runRhinoTest(generateFilenameList(getOutputFilePath(dirName + ".kt")),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
}
protected void translateFile(String filename) throws Exception {
testTranslateFile(getInputFilePath(filename),
getOutputFilePath(filename));
translateFile(getInputFilePath(filename),
getOutputFilePath(filename));
}
protected void translateFilesInDir(String dirName) throws Exception {
@@ -129,8 +129,8 @@ public abstract class TranslationTest extends BaseTest {
fullFilePaths.add(getInputFilePath(dirName) + "\\" + fileName);
}
assert dir.isDirectory();
testTranslateFiles(fullFilePaths,
getOutputFilePath(dirName + ".kt"));
traslateFiles(fullFilePaths,
getOutputFilePath(dirName + ".kt"));
}
protected List<String> generateFilenameList(String inputFile) {
@@ -185,7 +185,7 @@ public abstract class TranslationTest extends BaseTest {
protected void checkOutput(String filename, String expectedResult, String... args) throws Exception {
translateFile(filename);
runRhinoTest(generateFilenameList(getOutputFilePath(filename)),
new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args)));
new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args)));
}
protected void testWithMain(String testName, String testId, String... args) throws Exception {