From 57c9de776e8d3ff04b150aa5ca64f192bdbb3ad7 Mon Sep 17 00:00:00 2001 From: pTalanov Date: Tue, 28 Feb 2012 15:18:16 +0400 Subject: [PATCH] Move TestConfig to js.tests module. Remove K2JSTranslationUtils. Remove core.Dummy class. TestConfig references library files using project file structure instead of getResourceAsStream. --- js/js.libraries/src/core/Dummy.java | 23 ------ .../org/jetbrains/k2js/test}/TestConfig.java | 45 +++++------ .../jetbrains/k2js/test/TranslationTest.java | 36 ++++----- .../jetbrains/k2js/facade/K2JSTranslator.java | 7 +- .../k2js/facade/K2JSTranslatorUtils.java | 74 ------------------- 5 files changed, 41 insertions(+), 144 deletions(-) delete mode 100644 js/js.libraries/src/core/Dummy.java rename js/{js.translator/src/org/jetbrains/k2js/config => js.tests/test/org/jetbrains/k2js/test}/TestConfig.java (70%) delete mode 100644 js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslatorUtils.java diff --git a/js/js.libraries/src/core/Dummy.java b/js/js.libraries/src/core/Dummy.java deleted file mode 100644 index 3a72c52114b..00000000000 --- a/js/js.libraries/src/core/Dummy.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 core; - -/** - * @author Pavel Talanov - */ -public final class Dummy { -} diff --git a/js/js.translator/src/org/jetbrains/k2js/config/TestConfig.java b/js/js.tests/test/org/jetbrains/k2js/test/TestConfig.java similarity index 70% rename from js/js.translator/src/org/jetbrains/k2js/config/TestConfig.java rename to js/js.tests/test/org/jetbrains/k2js/test/TestConfig.java index 0740353e7c3..189e4b8b049 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/TestConfig.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TestConfig.java @@ -14,21 +14,20 @@ * limitations under the License. */ -package org.jetbrains.k2js.config; +package org.jetbrains.k2js.test; -import com.intellij.openapi.Disposable; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.io.FileUtil; -import core.Dummy; 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.io.InputStreamReader; -import java.io.Reader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -39,7 +38,6 @@ import java.util.List; //TODO: review/refactor public final class TestConfig extends Config { - //TODO: provide some generic way to get the files of the project @NotNull private static final List LIB_FILE_NAMES = Arrays.asList( "/core/annotations.kt", @@ -56,6 +54,8 @@ public final class TestConfig extends Config { "/html5/image.kt" ); + private static final String LIBRARIES_LOCATION = "js.libraries/src"; + @Nullable private /*var*/ List jsLibFiles = null; @@ -78,14 +78,21 @@ public final class TestConfig extends Config { for (String libFileName : LIB_FILE_NAMES) { JetFile file = null; //TODO: close stream? - InputStream stream = Dummy.class.getResourceAsStream(libFileName); try { - String text = FileUtil.loadTextAndClose(stream); - file = JetFileUtils.createPsiFile(libFileName, text, project); - } catch (IOException e) { - e.printStackTrace(); + @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."); } - libFiles.add(file); + } return libFiles; } @@ -97,18 +104,4 @@ public final class TestConfig extends Config { } return jsLibFiles; } - -// @NotNull -// private static String readString(@NotNull InputStream is) throws IOException { -// char[] buf = new char[2048]; -// Reader r = new InputStreamReader(is, "UTF-8"); -// StringBuilder s = new StringBuilder(); -// while (true) { -// int n = r.read(buf); -// if (n < 0) -// break; -// s.append(buf, 0, n); -// } -// return s.toString(); -// } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java index 5e4e3029b05..bbb77c11318 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java @@ -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 inputFiles, - @NotNull String outputFile) throws Exception { + public void traslateFiles(@NotNull List inputFiles, + @NotNull String outputFile) throws Exception { K2JSTranslator translator = new K2JSTranslator(new TestConfig(getProject())); List 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 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 { diff --git a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java b/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java index b5983e53caa..fe744752b94 100644 --- a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java +++ b/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java @@ -26,7 +26,6 @@ import org.jetbrains.jet.plugin.JetMainDetector; import org.jetbrains.k2js.analyze.Analyzer; import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.IDEAConfig; -import org.jetbrains.k2js.config.TestConfig; import org.jetbrains.k2js.generate.CodeGenerator; import org.jetbrains.k2js.translate.general.Translation; import org.jetbrains.k2js.utils.GenerationUtils; @@ -35,10 +34,12 @@ import org.jetbrains.k2js.utils.JetFileUtils; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.StringTokenizer; import static org.jetbrains.k2js.translate.utils.PsiUtils.getNamespaceName; -import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList; //TODO: clean up the code diff --git a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslatorUtils.java b/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslatorUtils.java deleted file mode 100644 index df69c35ca1b..00000000000 --- a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslatorUtils.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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.facade; - -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.k2js.config.TestConfig; - -/** - * Created by IntelliJ IDEA. - * User: Natalia.Ukhorskaya - * Date: 2/9/12 - * Time: 7:49 PM - */ - -public class K2JSTranslatorUtils { - @SuppressWarnings("FieldCanBeLocal") - private static String EXCEPTION = "exception="; - - @Nullable - public String translateToJS(@NotNull Project project, @NotNull String code, @NotNull String arguments) { - try { - return generateJSCode(project, code, arguments); - } catch (AssertionError e) { - reportException(e); - return EXCEPTION + "Translation error."; - } catch (UnsupportedOperationException e) { - reportException(e); - return EXCEPTION + "Unsupported feature."; - } catch (Throwable e) { - reportException(e); - return EXCEPTION + "Unexpected exception."; - } - } - - @Nullable - public BindingContext getBindingContext(@NotNull Project project, @NotNull String programText) { - try { - K2JSTranslator k2JSTranslator = new K2JSTranslator(new TestConfig(project)); - return k2JSTranslator.analyzeProgramCode(programText); - } catch (Throwable e) { - e.printStackTrace(); - reportException(e); - return null; - } - } - - @NotNull - private String generateJSCode(@NotNull Project project, @NotNull String code, @NotNull String arguments) { - String generatedCode = (new K2JSTranslator(new TestConfig(project))).translateStringWithCallToMain(code, arguments); - return generatedCode; - } - - private void reportException(@NotNull Throwable e) { - System.out.println("Exception in translateToJS!!!"); - e.printStackTrace(); - } -}