diff --git a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java index 400c52c18f0..9bc28c3c075 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java @@ -22,6 +22,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetTestUtils; import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.facade.MainCallParameters; +import org.jetbrains.k2js.test.config.TestConfig; +import org.jetbrains.k2js.test.config.TestConfigFactory; import org.jetbrains.k2js.test.rhino.RhinoResultChecker; import org.jetbrains.k2js.test.utils.TranslationUtils; @@ -99,12 +101,25 @@ public abstract class BasicTest extends TestWithEnvironment { ecmaVersions); } + protected void generateJavaScriptFiles(@NotNull List files, @NotNull String testName, + @NotNull MainCallParameters mainCallParameters, + @NotNull Iterable ecmaVersions, + @NotNull TestConfigFactory configFactory) + throws Exception { + for (EcmaVersion version : ecmaVersions) { + TranslationUtils.translateFiles(getProject(), withAdditionalFiles(files), + getOutputFilePath(testName, version), mainCallParameters, + version, configFactory); + } + } + protected void generateJavaScriptFiles(@NotNull List files, @NotNull String testName, @NotNull MainCallParameters mainCallParameters, @NotNull Iterable ecmaVersions) throws Exception { for (EcmaVersion version : ecmaVersions) { TranslationUtils.translateFiles(getProject(), withAdditionalFiles(files), - getOutputFilePath(testName, version), mainCallParameters, version); + getOutputFilePath(testName, version), mainCallParameters, + version, TestConfig.FACTORY); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfig.java b/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfig.java index bd2db405a78..188400bb9db 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfig.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfig.java @@ -28,7 +28,18 @@ import java.util.List; /** * @author Pavel Talanov */ -public final class TestConfig extends Config { +public class TestConfig extends Config { + + @NotNull + public static TestConfigFactory FACTORY = new TestConfigFactory() { + @Override + public TestConfig create(@NotNull Project project, + @NotNull EcmaVersion version, + @NotNull List files, + @NotNull BindingContext context) { + return new TestConfig(project, version, files, context); + } + }; //NOTE: hard-coded in kotlin-lib files @NotNull diff --git a/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfigFactory.java b/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfigFactory.java new file mode 100644 index 00000000000..eb7383bbbcb --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfigFactory.java @@ -0,0 +1,33 @@ +/* + * Copyright 2010-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.config; + +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.k2js.config.EcmaVersion; + +import java.util.List; + +/** + * @author Pavel Talanov + */ +public interface TestConfigFactory { + TestConfig create(@NotNull Project project, @NotNull EcmaVersion version, + @NotNull List files, @NotNull BindingContext context); +} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfigWithUnitTests.java b/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfigWithUnitTests.java new file mode 100644 index 00000000000..684c0c3b105 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/config/TestConfigWithUnitTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2010-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.config; + +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.k2js.config.EcmaVersion; +import org.jetbrains.k2js.translate.test.JSRhinoUnitTester; +import org.jetbrains.k2js.translate.test.JSTester; + +import java.util.List; + +/** + * @author Pavel Talanov + */ +public class TestConfigWithUnitTests extends TestConfig { + + @NotNull + public static TestConfigFactory FACTORY = new TestConfigFactory() { + @Override + public TestConfig create(@NotNull Project project, + @NotNull EcmaVersion version, + @NotNull List files, + @NotNull BindingContext context) { + return new TestConfigWithUnitTests(project, version, files, context); + } + }; + + @Override + public JSTester getTester() { + return new JSRhinoUnitTester(); + } + + public TestConfigWithUnitTests(@NotNull Project project, + @NotNull EcmaVersion version, + @NotNull List files, + @NotNull BindingContext context) { + super(project, version, files, context); + } +} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java index 5de1231b42c..e711f4b522f 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java @@ -22,6 +22,7 @@ import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.test.MultipleFilesTranslationTest; +import org.jetbrains.k2js.test.config.TestConfigWithUnitTests; import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker; import java.util.ArrayList; @@ -72,7 +73,8 @@ public class JsUnitTestBase extends MultipleFilesTranslationTest { private void performUnitTest(String... testFiles) throws Exception { Iterable versions = Collections.singletonList(EcmaVersion.v3); - generateJavaScriptFiles(Lists.newArrayList(testFiles), "myTest", MainCallParameters.noCall(), versions); + generateJavaScriptFiles(Lists.newArrayList(testFiles), "myTest", MainCallParameters.noCall(), versions, + TestConfigWithUnitTests.FACTORY); runRhinoTests("myTest", versions, new RhinoSystemOutputChecker("")); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java index cf9d3e3375f..c8ee1b1f7e8 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java @@ -16,7 +16,6 @@ package org.jetbrains.k2js.test.utils; -import closurecompiler.internal.com.google.common.collect.Maps; import com.google.common.base.Predicate; import com.google.common.collect.Lists; import com.google.dart.compiler.backend.js.ast.JsProgram; @@ -34,26 +33,23 @@ import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.facade.K2JSTranslator; import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.generate.CodeGenerator; -import org.jetbrains.k2js.test.config.TestConfig; +import org.jetbrains.k2js.test.config.TestConfigFactory; import org.jetbrains.k2js.utils.JetFileUtils; import java.io.*; import java.util.List; -import java.util.Map; import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList; /** * @author Pavel Talanov */ +//TODO: use method object public final class TranslationUtils { private TranslationUtils() { } - @NotNull - private static final Map testConfigs = Maps.newHashMap(); - @Nullable private static BindingContext libraryContext = null; @@ -96,22 +92,17 @@ public final class TranslationUtils { } @NotNull - public static Config getConfig(@NotNull Project project, @NotNull EcmaVersion version) { - Config config = testConfigs.get(version); - if (config == null) { - BindingContext preanalyzedContext = getLibraryContext(project); - config = new TestConfig(project, version, getLibFilesWithCode(getAllLibFiles(project)), preanalyzedContext); - testConfigs.put(version, config); - } - return config; + public static Config getConfig(@NotNull Project project, @NotNull EcmaVersion version, @NotNull TestConfigFactory configFactory) { + BindingContext preanalyzedContext = getLibraryContext(project); + return configFactory.create(project, version, getLibFilesWithCode(getAllLibFiles(project)), preanalyzedContext); } public static void translateFiles(@NotNull Project project, @NotNull List inputFiles, @NotNull String outputFile, @NotNull MainCallParameters mainCallParameters, - @NotNull EcmaVersion version) throws Exception { + @NotNull EcmaVersion version, TestConfigFactory configFactory) throws Exception { List psiFiles = createPsiFileList(inputFiles, project); - JsProgram program = getTranslator(project, version).generateProgram(psiFiles, mainCallParameters); + JsProgram program = new K2JSTranslator(getConfig(project, version, configFactory)).generateProgram(psiFiles, mainCallParameters); FileWriter writer = new FileWriter(new File(outputFile)); try { writer.write(CodeGenerator.generateProgramToString(program)); @@ -121,11 +112,6 @@ public final class TranslationUtils { } } - @NotNull - private static K2JSTranslator getTranslator(@NotNull Project project, @NotNull EcmaVersion version) { - return new K2JSTranslator(getConfig(project, version)); - } - @NotNull private static List initLibFiles(@NotNull Project project) { return getLibFiles(project, Config.LIB_FILE_NAMES);