KJS: introduce new base class WebDemoExamples* tests
This commit is contained in:
@@ -60,7 +60,7 @@ import java.nio.charset.Charset
|
||||
import java.util.regex.Pattern
|
||||
|
||||
abstract class BasicBoxTest(
|
||||
private val pathToTestDir: String,
|
||||
protected val pathToTestDir: String,
|
||||
private val pathToOutputDir: String,
|
||||
private val typedArraysEnabled: Boolean = false,
|
||||
private val generateSourceMap: Boolean = false,
|
||||
@@ -72,6 +72,10 @@ abstract class BasicBoxTest(
|
||||
protected open fun getOutputPostfixFile(testFilePath: String): File? = null
|
||||
|
||||
fun doTest(filePath: String) {
|
||||
doTest(filePath, "OK", MainCallParameters.noCall())
|
||||
}
|
||||
|
||||
fun doTest(filePath: String, expectedResult: String, mainCallParameters: MainCallParameters) {
|
||||
val file = File(filePath)
|
||||
val outputDir = getOutputDir(file)
|
||||
val fileContent = KotlinTestUtils.doLoadFile(file)
|
||||
@@ -93,7 +97,8 @@ abstract class BasicBoxTest(
|
||||
val dependencies = module.dependencies.mapNotNull { modules[it]?.outputFileName(outputDir) + ".meta.js" }
|
||||
|
||||
val outputFileName = module.outputFileName(outputDir) + ".js"
|
||||
generateJavaScriptFile(file.parent, module, outputFileName, dependencies, modules.size > 1, outputPrefixFile, outputPostfixFile)
|
||||
generateJavaScriptFile(file.parent, module, outputFileName, dependencies, modules.size > 1,
|
||||
outputPrefixFile, outputPostfixFile, mainCallParameters)
|
||||
|
||||
if (!module.name.endsWith(OLD_MODULE_SUFFIX)) outputFileName else null
|
||||
}
|
||||
@@ -143,13 +148,24 @@ abstract class BasicBoxTest(
|
||||
FileUtil.writeToFile(File(nodeRunnerName), nodeRunnerText)
|
||||
}
|
||||
|
||||
val checker = RhinoFunctionResultChecker(mainModuleName, testFactory.testPackage, TEST_FUNCTION, "OK", withModuleSystem)
|
||||
RhinoUtils.runRhinoTest(allJsFiles, checker)
|
||||
runGeneratedCode(allJsFiles, mainModuleName, testFactory.testPackage, TEST_FUNCTION, expectedResult, withModuleSystem)
|
||||
|
||||
performAdditionalChecks(generatedJsFiles, outputPrefixFile, outputPostfixFile)
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun runGeneratedCode(
|
||||
jsFiles: List<String>,
|
||||
testModuleName: String,
|
||||
testPackage: String?,
|
||||
testFunction: String,
|
||||
expectedResult: String,
|
||||
withModuleSystem: Boolean
|
||||
) {
|
||||
val checker = RhinoFunctionResultChecker(testModuleName, testPackage, testFunction, expectedResult, withModuleSystem)
|
||||
RhinoUtils.runRhinoTest(jsFiles, checker)
|
||||
}
|
||||
|
||||
protected open fun performAdditionalChecks(generatedJsFiles: List<String>, outputPrefixFile: File?, outputPostfixFile: File?) {}
|
||||
|
||||
private fun generateNodeRunner(
|
||||
@@ -208,7 +224,8 @@ abstract class BasicBoxTest(
|
||||
dependencies: List<String>,
|
||||
multiModule: Boolean,
|
||||
outputPrefixFile: File?,
|
||||
outputPostfixFile: File?
|
||||
outputPostfixFile: File?,
|
||||
mainCallParameters: MainCallParameters
|
||||
) {
|
||||
val testFiles = module.files.map { it.fileName }.filter { it.endsWith(".kt") }
|
||||
val globalCommonFiles = JsTestUtils.getFilesInDirectoryByExtension(
|
||||
@@ -223,7 +240,7 @@ abstract class BasicBoxTest(
|
||||
val config = createConfig(module, dependencies, multiModule)
|
||||
val outputFile = File(outputFileName)
|
||||
|
||||
translateFiles(psiFiles, outputFile, config, outputPrefixFile, outputPostfixFile)
|
||||
translateFiles(psiFiles, outputFile, config, outputPrefixFile, outputPostfixFile, mainCallParameters)
|
||||
}
|
||||
|
||||
protected fun translateFiles(
|
||||
@@ -231,10 +248,11 @@ abstract class BasicBoxTest(
|
||||
outputFile: File,
|
||||
config: JsConfig,
|
||||
outputPrefixFile: File?,
|
||||
outputPostfixFile: File?
|
||||
outputPostfixFile: File?,
|
||||
mainCallParameters: MainCallParameters
|
||||
) {
|
||||
val translator = K2JSTranslator(config)
|
||||
val translationResult = translator.translate(psiFiles, MainCallParameters.noCall())
|
||||
val translationResult = translator.translate(psiFiles, mainCallParameters)
|
||||
|
||||
if (translationResult !is TranslationResult.Success) {
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
@@ -375,7 +393,8 @@ abstract class BasicBoxTest(
|
||||
}
|
||||
|
||||
companion object {
|
||||
val TEST_DATA_DIR_PATH = "js/js.translator/testData/"
|
||||
const val TEST_DATA_DIR_PATH = "js/js.translator/testData/"
|
||||
const val DIST_DIR_JS_PATH = "dist/js/"
|
||||
|
||||
private val COMMON_FILES_NAME = "_common"
|
||||
private val COMMON_FILES_DIR = "_commonFiles/"
|
||||
|
||||
@@ -1,402 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.js.test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.StandardFileSystems;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.openapi.vfs.VirtualFileSystem;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection;
|
||||
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport;
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer;
|
||||
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector;
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys;
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration;
|
||||
import org.jetbrains.kotlin.idea.KotlinFileType;
|
||||
import org.jetbrains.kotlin.js.JavaScript;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsProgram;
|
||||
import org.jetbrains.kotlin.js.config.EcmaVersion;
|
||||
import org.jetbrains.kotlin.js.config.JSConfigurationKeys;
|
||||
import org.jetbrains.kotlin.js.config.JsConfig;
|
||||
import org.jetbrains.kotlin.js.facade.K2JSTranslator;
|
||||
import org.jetbrains.kotlin.js.facade.MainCallParameters;
|
||||
import org.jetbrains.kotlin.js.facade.TranslationResult;
|
||||
import org.jetbrains.kotlin.js.test.rhino.RhinoResultChecker;
|
||||
import org.jetbrains.kotlin.js.test.utils.DirectiveTestUtils;
|
||||
import org.jetbrains.kotlin.js.test.utils.JsTestUtils;
|
||||
import org.jetbrains.kotlin.js.test.utils.JsVerificationKt;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.KotlinTestWithEnvironment;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.kotlin.js.test.rhino.RhinoUtils.runRhinoTest;
|
||||
import static org.jetbrains.kotlin.js.test.utils.JsTestUtils.convertFileNameToDotJsFile;
|
||||
import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isDirectiveDefined;
|
||||
import static org.jetbrains.kotlin.utils.PathUtil.getKotlinPathsForDistDirectory;
|
||||
|
||||
public abstract class BasicTest extends KotlinTestWithEnvironment {
|
||||
// predictable order of ecma version in tests
|
||||
protected static final Iterable<EcmaVersion> DEFAULT_ECMA_VERSIONS = Lists.newArrayList(EcmaVersion.v5);
|
||||
|
||||
private static final boolean DELETE_OUT = false;
|
||||
|
||||
public static final String TEST_DATA_DIR_PATH = "js/js.translator/testData/";
|
||||
public static final String DIST_DIR_JS_PATH = "dist/js/";
|
||||
|
||||
private static final String CASES = "cases/";
|
||||
private static final String OUT = "out/";
|
||||
private static final String EXPECTED = "expected/";
|
||||
private static final String COMMON_FILES_DIR = "_commonFiles/";
|
||||
|
||||
public static final String TEST_MODULE = "JS_TESTS";
|
||||
public static final String TEST_PACKAGE = "foo";
|
||||
public static final String TEST_FUNCTION = "box";
|
||||
private static final String NO_INLINE_DIRECTIVE = "// NO_INLINE";
|
||||
|
||||
@NotNull
|
||||
private String relativePathToTestDir = "";
|
||||
|
||||
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
|
||||
public BasicTest(@NotNull String relativePathToTestDir) {
|
||||
this.relativePathToTestDir = relativePathToTestDir;
|
||||
}
|
||||
|
||||
protected abstract void checkFooBoxIsOkByPath(String filePath) throws Exception;
|
||||
|
||||
@Override
|
||||
protected KotlinCoreEnvironment createEnvironment() {
|
||||
return KotlinCoreEnvironment.createForTests(getTestRootDisposable(), new CompilerConfiguration(), EnvironmentConfigFiles.JS_CONFIG_FILES);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
File outDir = new File(getOutputPath());
|
||||
|
||||
KotlinTestUtils.mkdirs(outDir);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (!DELETE_OUT) {
|
||||
return;
|
||||
}
|
||||
File outDir = new File(getOutputPath());
|
||||
assertTrue(outDir.exists());
|
||||
assertTrue(FileUtil.delete(outDir));
|
||||
}
|
||||
|
||||
public void doTest(@NotNull String filePath) {
|
||||
try {
|
||||
checkFooBoxIsOkByPath(filePath);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void generateJavaScriptFiles(
|
||||
@NotNull String kotlinFilePath,
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions
|
||||
) throws Exception {
|
||||
generateJavaScriptFiles(Collections.singletonList(kotlinFilePath), getBaseName(kotlinFilePath), mainCallParameters, ecmaVersions);
|
||||
}
|
||||
|
||||
protected void generateJavaScriptFiles(
|
||||
@NotNull List<String> files,
|
||||
@NotNull String testName,
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions
|
||||
) throws Exception {
|
||||
generateJavaScriptFiles(files, testName, mainCallParameters, ecmaVersions, TEST_MODULE, null);
|
||||
}
|
||||
|
||||
private void generateJavaScriptFiles(
|
||||
@NotNull List<String> files,
|
||||
@NotNull String testName,
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions,
|
||||
@NotNull String moduleName,
|
||||
@Nullable List<String> libraries
|
||||
) throws Exception {
|
||||
for (EcmaVersion version : ecmaVersions) {
|
||||
generateJavaScriptFiles(files, testName, mainCallParameters, version, moduleName, libraries);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateJavaScriptFiles(
|
||||
@NotNull List<String> files,
|
||||
@NotNull String testName,
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull EcmaVersion version,
|
||||
@NotNull String moduleName,
|
||||
@Nullable List<String> libraries
|
||||
|
||||
) throws Exception {
|
||||
Project project = getProject();
|
||||
List<String> allFiles = withAdditionalKotlinFiles(files);
|
||||
List<KtFile> jetFiles = createJetFileList(project, allFiles, null);
|
||||
|
||||
JsConfig config = createConfig(getProject(), moduleName, version, libraries, jetFiles);
|
||||
File outputFile = new File(getOutputFilePath(testName, version));
|
||||
|
||||
translateFiles(jetFiles, outputFile, mainCallParameters, config);
|
||||
}
|
||||
|
||||
private void translateFiles(
|
||||
@NotNull List<KtFile> jetFiles,
|
||||
@NotNull File outputFile,
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull JsConfig config
|
||||
) throws Exception {
|
||||
K2JSTranslator translator = new K2JSTranslator(config);
|
||||
TranslationResult translationResult = translator.translate(jetFiles, mainCallParameters);
|
||||
|
||||
if (!(translationResult instanceof TranslationResult.Success)) {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
PrintingMessageCollector collector = new PrintingMessageCollector(
|
||||
new PrintStream(outputStream),
|
||||
MessageRenderer.PLAIN_FULL_PATHS,
|
||||
true
|
||||
);
|
||||
AnalyzerWithCompilerReport.Companion.reportDiagnostics(translationResult.getDiagnostics(), collector);
|
||||
String messages = new String(outputStream.toByteArray(), "UTF-8");
|
||||
throw new AssertionError("The following errors occurred compiling test:\n" + messages);
|
||||
}
|
||||
|
||||
TranslationResult.Success successResult = (TranslationResult.Success) translationResult;
|
||||
|
||||
OutputFileCollection outputFiles = successResult.getOutputFiles(outputFile, getOutputPrefixFile(), getOutputPostfixFile());
|
||||
File outputDir = outputFile.getParentFile();
|
||||
assert outputDir != null : "Parent file for output file should not be null, outputFilePath: " + outputFile.getPath();
|
||||
OutputUtilsKt.writeAllTo(outputFiles, outputDir);
|
||||
|
||||
processJsProgram(successResult.getProgram(), jetFiles);
|
||||
}
|
||||
|
||||
protected File getOutputPostfixFile() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected File getOutputPrefixFile() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected boolean shouldGenerateSourceMap() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void processJsProgram(@NotNull JsProgram program, @NotNull List<KtFile> jetFiles) throws Exception {
|
||||
for (KtFile file : jetFiles) {
|
||||
String text = file.getText();
|
||||
DirectiveTestUtils.processDirectives(program, text);
|
||||
}
|
||||
JsVerificationKt.verifyAst(program);
|
||||
}
|
||||
|
||||
protected void runRhinoTests(
|
||||
@NotNull String testName,
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions,
|
||||
@NotNull RhinoResultChecker checker
|
||||
) throws Exception {
|
||||
for (EcmaVersion ecmaVersion : ecmaVersions) {
|
||||
runRhinoTest(withAdditionalJsFiles(getOutputFilePath(testName, ecmaVersion), ecmaVersion),
|
||||
checker,
|
||||
getRhinoTestVariables(),
|
||||
ecmaVersion);
|
||||
}
|
||||
}
|
||||
|
||||
protected Map<String, Object> getRhinoTestVariables() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<String> additionalKotlinFiles() {
|
||||
List<String> additionalFiles = Lists.newArrayList();
|
||||
|
||||
// add all kotlin files from testData/_commonFiles
|
||||
additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(TEST_DATA_DIR_PATH + COMMON_FILES_DIR, KotlinFileType.EXTENSION));
|
||||
// add all kotlin files from <testDir>/_commonFiles
|
||||
additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(pathToTestDir() + COMMON_FILES_DIR, KotlinFileType.EXTENSION));
|
||||
|
||||
return additionalFiles;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<String> additionalJsFiles(@NotNull EcmaVersion ecmaVersion) {
|
||||
List<String> additionalFiles = Lists.newArrayList();
|
||||
|
||||
// add all js files from testData/_commonFiles
|
||||
additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(TEST_DATA_DIR_PATH + COMMON_FILES_DIR, JavaScript.EXTENSION));
|
||||
// add all js files from <testDir>/_commonFiles
|
||||
additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(pathToTestDir() + COMMON_FILES_DIR, JavaScript.EXTENSION));
|
||||
|
||||
// add <testDir>/cases/<testName>.js if it exists
|
||||
String jsFilePath = getInputFilePath(getTestName(true) + JavaScript.DOT_EXTENSION);
|
||||
File jsFile = new File(jsFilePath);
|
||||
if (jsFile.exists() && jsFile.isFile()) {
|
||||
additionalFiles.add(jsFilePath);
|
||||
}
|
||||
|
||||
return additionalFiles;
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
@NotNull
|
||||
private String pathToTestDir() {
|
||||
return TEST_DATA_DIR_PATH + relativePathToTestDir;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected final String getOutputFilePath(@NotNull String testName, @NotNull EcmaVersion ecmaVersion) {
|
||||
return getOutputPath() + convertFileNameToDotJsFile(testName, ecmaVersion);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected final String getInputFilePath(@NotNull String filename) {
|
||||
return getInputPath() + filename;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected final String expectedFilePath(@NotNull String testName) {
|
||||
return getExpectedPath() + testName + ".out";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsConfig createConfig(
|
||||
@NotNull Project project,
|
||||
@NotNull String moduleName,
|
||||
@NotNull EcmaVersion ecmaVersion,
|
||||
@Nullable List<String> libraries,
|
||||
@NotNull List<KtFile> files
|
||||
) {
|
||||
CompilerConfiguration configuration = getEnvironment().getConfiguration().copy();
|
||||
|
||||
configuration.put(CommonConfigurationKeys.DISABLE_INLINE, hasNoInline(files));
|
||||
|
||||
List<String> librariesWithStdlib = new ArrayList<>(JsConfig.JS_STDLIB);
|
||||
if (libraries != null) {
|
||||
librariesWithStdlib.addAll(libraries);
|
||||
}
|
||||
librariesWithStdlib.add(getKotlinPathsForDistDirectory().getJsKotlinTestJarPath().getAbsolutePath());
|
||||
|
||||
configuration.put(JSConfigurationKeys.LIBRARIES, librariesWithStdlib);
|
||||
|
||||
configuration.put(CommonConfigurationKeys.MODULE_NAME, moduleName);
|
||||
configuration.put(JSConfigurationKeys.TARGET, ecmaVersion);
|
||||
|
||||
configuration.put(JSConfigurationKeys.SOURCE_MAP, shouldGenerateSourceMap());
|
||||
configuration.put(JSConfigurationKeys.META_INFO, false);
|
||||
|
||||
return new JsConfig(project, configuration);
|
||||
}
|
||||
|
||||
private static boolean hasNoInline(@NotNull List<KtFile> files) {
|
||||
for (KtFile file : files) {
|
||||
if (isDirectiveDefined(file.getText(), NO_INLINE_DIRECTIVE)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getOutputPath() {
|
||||
return pathToTestDir() + OUT;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getInputPath() {
|
||||
return pathToTestDir() + CASES;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getExpectedPath() {
|
||||
return pathToTestDir() + EXPECTED;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<String> withAdditionalKotlinFiles(@NotNull List<String> files) {
|
||||
List<String> result = Lists.newArrayList(files);
|
||||
result.addAll(additionalKotlinFiles());
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<String> withAdditionalJsFiles(@NotNull String inputFile, @NotNull EcmaVersion ecmaVersion) {
|
||||
List<String> allFiles = Lists.newArrayList(additionalJsFiles(ecmaVersion));
|
||||
allFiles.add(inputFile);
|
||||
return allFiles;
|
||||
}
|
||||
|
||||
private static List<KtFile> createJetFileList(@NotNull Project project, @NotNull List<String> list, @Nullable String root) {
|
||||
List<KtFile> libFiles = Lists.newArrayList();
|
||||
|
||||
PsiManager psiManager = PsiManager.getInstance(project);
|
||||
VirtualFileSystem fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL);
|
||||
|
||||
VirtualFile rootFile = root == null ? null : fileSystem.findFileByPath(root);
|
||||
|
||||
for (String libFileName : list) {
|
||||
VirtualFile virtualFile = rootFile == null ? fileSystem.findFileByPath(libFileName) : rootFile.findFileByRelativePath(libFileName);
|
||||
//TODO logging?
|
||||
assert virtualFile != null : "virtual file is missing, most likely the file doesn't exist: " + libFileName;
|
||||
PsiFile psiFile = psiManager.findFile(virtualFile);
|
||||
libFiles.add((KtFile) psiFile);
|
||||
}
|
||||
return libFiles;
|
||||
}
|
||||
|
||||
protected static String getBaseName(String path) {
|
||||
String systemIndependentPath = FileUtil.toSystemIndependentName(path);
|
||||
|
||||
int start = systemIndependentPath.lastIndexOf("/");
|
||||
if (start == -1) {
|
||||
start = 0;
|
||||
}
|
||||
|
||||
int end = systemIndependentPath.lastIndexOf(".");
|
||||
if (end == -1) {
|
||||
end = path.length();
|
||||
}
|
||||
|
||||
return path.substring(start, end);
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.js.test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.js.config.EcmaVersion;
|
||||
import org.jetbrains.kotlin.js.facade.MainCallParameters;
|
||||
import org.jetbrains.kotlin.js.test.rhino.RhinoFunctionResultChecker;
|
||||
import org.jetbrains.kotlin.js.test.rhino.RhinoSystemOutputChecker;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.js.test.utils.JsTestUtils.readFile;
|
||||
|
||||
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
|
||||
public abstract class SingleFileTranslationTest extends BasicTest {
|
||||
public SingleFileTranslationTest(@NotNull String main) {
|
||||
super(main);
|
||||
}
|
||||
|
||||
protected void runFunctionOutputTest(
|
||||
@NotNull String kotlinFilename,
|
||||
@NotNull String packageName,
|
||||
@NotNull String functionName,
|
||||
@NotNull Object expectedResult
|
||||
) throws Exception {
|
||||
runFunctionOutputTest(DEFAULT_ECMA_VERSIONS, kotlinFilename, packageName, functionName, expectedResult);
|
||||
}
|
||||
|
||||
protected void runFunctionOutputTest(
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions,
|
||||
@NotNull String kotlinFilename,
|
||||
@NotNull String packageName,
|
||||
@NotNull String functionName,
|
||||
@NotNull Object expectedResult
|
||||
) throws Exception {
|
||||
runFunctionOutputTestByPath(ecmaVersions, getInputFilePath(kotlinFilename), packageName, functionName, expectedResult);
|
||||
}
|
||||
|
||||
private void runFunctionOutputTestByPath(
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions,
|
||||
@NotNull String kotlinFilePath,
|
||||
@NotNull String packageName,
|
||||
@NotNull String functionName,
|
||||
@NotNull Object expectedResult
|
||||
) throws Exception {
|
||||
runFunctionOutputTestByPaths(ecmaVersions, Collections.singletonList(kotlinFilePath), packageName, functionName, expectedResult);
|
||||
}
|
||||
|
||||
private void runFunctionOutputTestByPaths(
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions,
|
||||
@NotNull List<String> kotlinFilePaths,
|
||||
@NotNull String packageName,
|
||||
@NotNull String functionName,
|
||||
@NotNull Object expectedResult
|
||||
) throws Exception {
|
||||
String testName = getTestName(true);
|
||||
generateJavaScriptFiles(kotlinFilePaths, testName, MainCallParameters.noCall(), ecmaVersions);
|
||||
RhinoFunctionResultChecker checker = new RhinoFunctionResultChecker(TEST_MODULE, packageName, functionName, expectedResult);
|
||||
runRhinoTests(testName, ecmaVersions, checker);
|
||||
}
|
||||
|
||||
protected void checkFooBoxIsOk(@NotNull String filename) throws Exception {
|
||||
checkFooBoxIsOkByPath(getInputFilePath(filename));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkFooBoxIsOkByPath(@NotNull String filePath) throws Exception {
|
||||
runFunctionOutputTestByPath(DEFAULT_ECMA_VERSIONS, filePath, TEST_PACKAGE, TEST_FUNCTION, "OK");
|
||||
}
|
||||
|
||||
protected void checkOutput(@NotNull String kotlinFilename,
|
||||
@NotNull String expectedResult,
|
||||
@NotNull String... args) throws Exception {
|
||||
checkOutput(kotlinFilename, expectedResult, DEFAULT_ECMA_VERSIONS, args);
|
||||
}
|
||||
|
||||
private void checkOutput(
|
||||
@NotNull String kotlinFilename,
|
||||
@NotNull String expectedResult,
|
||||
@NotNull Iterable<EcmaVersion> ecmaVersions,
|
||||
String... args
|
||||
) throws Exception {
|
||||
generateJavaScriptFiles(getInputFilePath(kotlinFilename), MainCallParameters.mainWithArguments(Lists.newArrayList(args)), ecmaVersions);
|
||||
runRhinoTests(getBaseName(kotlinFilename), ecmaVersions, new RhinoSystemOutputChecker(expectedResult));
|
||||
}
|
||||
|
||||
protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception {
|
||||
checkOutput(testName + ".kt", readFile(expectedFilePath(testName + testId)), DEFAULT_ECMA_VERSIONS, args);
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.js.backend.ast.*
|
||||
import org.jetbrains.kotlin.js.inline.clean.FunctionPostProcessor
|
||||
import org.jetbrains.kotlin.js.parser.parse
|
||||
import org.jetbrains.kotlin.js.sourceMap.JsSourceGenerationVisitor
|
||||
import org.jetbrains.kotlin.js.test.BasicTest
|
||||
import org.jetbrains.kotlin.js.test.BasicBoxTest
|
||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
|
||||
import org.junit.Assert
|
||||
import org.junit.Rule
|
||||
@@ -41,7 +41,7 @@ abstract class BasicOptimizerTest(private var basePath: String) {
|
||||
|
||||
protected fun box() {
|
||||
val methodName = testName.methodName
|
||||
val baseName = "${BasicTest.TEST_DATA_DIR_PATH}/js-optimizer/$basePath"
|
||||
val baseName = "${BasicBoxTest.TEST_DATA_DIR_PATH}/js-optimizer/$basePath"
|
||||
val unoptimizedName = "$baseName/$methodName.original.js"
|
||||
val optimizedName = "$baseName/$methodName.optimized.js"
|
||||
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.kotlin.js.test.semantics
|
||||
|
||||
import com.google.common.collect.Lists
|
||||
import org.jetbrains.kotlin.js.facade.MainCallParameters
|
||||
import org.jetbrains.kotlin.js.test.BasicBoxTest
|
||||
import org.jetbrains.kotlin.js.test.rhino.RhinoSystemOutputChecker
|
||||
import org.jetbrains.kotlin.js.test.rhino.RhinoUtils
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractWebDemoExamplesTest(relativePath: String) : BasicBoxTest(
|
||||
BasicBoxTest.TEST_DATA_DIR_PATH + "/$relativePath/",
|
||||
BasicBoxTest.TEST_DATA_DIR_PATH + "out/$relativePath/",
|
||||
generateNodeJsRunner = false
|
||||
) {
|
||||
override fun runGeneratedCode(
|
||||
jsFiles: List<String>,
|
||||
testModuleName: String,
|
||||
testPackage: String?,
|
||||
testFunction: String,
|
||||
expectedResult: String,
|
||||
withModuleSystem: Boolean
|
||||
) {
|
||||
RhinoUtils.runRhinoTest(jsFiles, RhinoSystemOutputChecker(expectedResult))
|
||||
}
|
||||
|
||||
protected fun runMainAndCheckOutput(fileName: String, expectedResult: String, vararg args: String) {
|
||||
doTest(pathToTestDir + fileName, expectedResult, MainCallParameters.mainWithArguments(Lists.newArrayList(*args)))
|
||||
}
|
||||
|
||||
protected fun runMainAndCheckOutputWithExpectedFile(testName: String, testId: String, vararg args: String) {
|
||||
val expectedResult = File(pathToTestDir + testName + testId + ".out").readText()
|
||||
doTest(pathToTestDir + testName + ".kt", expectedResult, MainCallParameters.mainWithArguments(Lists.newArrayList(*args)))
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -17,14 +17,13 @@
|
||||
package org.jetbrains.kotlin.js.test.semantics;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.js.test.SingleFileTranslationTest;
|
||||
import org.mozilla.javascript.EcmaError;
|
||||
|
||||
/*
|
||||
* We can't really check that this examples work in non-browser environment, so we just check that examples compile and
|
||||
* running them produce expected error.
|
||||
* */
|
||||
public final class WebDemoCanvasExamplesTest extends SingleFileTranslationTest {
|
||||
public final class WebDemoCanvasExamplesTest extends AbstractWebDemoExamplesTest {
|
||||
|
||||
public WebDemoCanvasExamplesTest() {
|
||||
super("webDemoCanvasExamples/");
|
||||
@@ -48,7 +47,7 @@ public final class WebDemoCanvasExamplesTest extends SingleFileTranslationTest {
|
||||
|
||||
private void doTest(@NotNull String filename, @NotNull String firstUnknownSymbolEncountered) throws Exception {
|
||||
try {
|
||||
checkOutput(filename, "");
|
||||
runMainAndCheckOutput(filename, "");
|
||||
fail();
|
||||
}
|
||||
catch (EcmaError e) {
|
||||
|
||||
@@ -16,80 +16,78 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.test.semantics;
|
||||
|
||||
import org.jetbrains.kotlin.js.test.SingleFileTranslationTest;
|
||||
|
||||
public final class WebDemoExamples1Test extends SingleFileTranslationTest {
|
||||
public final class WebDemoExamples1Test extends AbstractWebDemoExamplesTest {
|
||||
|
||||
public WebDemoExamples1Test() {
|
||||
super("webDemoExamples1/");
|
||||
}
|
||||
|
||||
public void testPrintArg() throws Exception {
|
||||
checkOutput("printArg.kt", "Hello, world!", "Hello, world!");
|
||||
runMainAndCheckOutput("printArg.kt", "Hello, world!", "Hello, world!");
|
||||
}
|
||||
|
||||
|
||||
public void testWhileLoop() throws Exception {
|
||||
checkOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4");
|
||||
runMainAndCheckOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4");
|
||||
}
|
||||
|
||||
|
||||
public void testIfAsExpression() throws Exception {
|
||||
checkOutput("ifAsExpression.kt", "20\n", "10", "20");
|
||||
runMainAndCheckOutput("ifAsExpression.kt", "20\n", "10", "20");
|
||||
}
|
||||
|
||||
|
||||
public void testObjectOrientedHello() throws Exception {
|
||||
checkOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel");
|
||||
runMainAndCheckOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel");
|
||||
}
|
||||
|
||||
|
||||
public void testMultiLanguageHello() throws Exception {
|
||||
checkOutput("multiLanguageHello.kt", "Salut!\n", "FR");
|
||||
runMainAndCheckOutput("multiLanguageHello.kt", "Salut!\n", "FR");
|
||||
}
|
||||
|
||||
|
||||
public void testNullChecks() throws Exception {
|
||||
checkOutput("nullChecks.kt", "No number supplied");
|
||||
checkOutput("nullChecks.kt", "6", "2", "3");
|
||||
runMainAndCheckOutput("nullChecks.kt", "No number supplied");
|
||||
runMainAndCheckOutput("nullChecks.kt", "6", "2", "3");
|
||||
}
|
||||
|
||||
|
||||
public void testRanges() throws Exception {
|
||||
checkOutput("ranges.kt", "OK\n" +
|
||||
" 1 2 3 4 5\n" +
|
||||
"Out: array has only 3 elements. x = 4\n" +
|
||||
"Yes: array contains aaa\n" +
|
||||
"No: array doesn't contains ddd\n", "4");
|
||||
runMainAndCheckOutput("ranges.kt", "OK\n" +
|
||||
" 1 2 3 4 5\n" +
|
||||
"Out: array has only 3 elements. x = 4\n" +
|
||||
"Yes: array contains aaa\n" +
|
||||
"No: array doesn't contains ddd\n", "4");
|
||||
|
||||
checkOutput("ranges.kt", " 1 2 3 4 5\n" +
|
||||
"Out: array has only 3 elements. x = 10\n" +
|
||||
"Yes: array contains aaa\n" +
|
||||
"No: array doesn't contains ddd\n", "10");
|
||||
runMainAndCheckOutput("ranges.kt", " 1 2 3 4 5\n" +
|
||||
"Out: array has only 3 elements. x = 10\n" +
|
||||
"Yes: array contains aaa\n" +
|
||||
"No: array doesn't contains ddd\n", "10");
|
||||
}
|
||||
|
||||
|
||||
public void testForLoop() throws Exception {
|
||||
checkOutput("forLoop.kt", "a\n" +
|
||||
"b\n" +
|
||||
"c\n" +
|
||||
"\n" +
|
||||
"a\n" +
|
||||
"b\n" +
|
||||
"c\n", "a", "b", "c");
|
||||
checkOutput("forLoop.kt", "123\n\n123\n", "123");
|
||||
runMainAndCheckOutput("forLoop.kt", "a\n" +
|
||||
"b\n" +
|
||||
"c\n" +
|
||||
"\n" +
|
||||
"a\n" +
|
||||
"b\n" +
|
||||
"c\n", "a", "b", "c");
|
||||
runMainAndCheckOutput("forLoop.kt", "123\n\n123\n", "123");
|
||||
}
|
||||
|
||||
|
||||
public void testIsCheck() throws Exception {
|
||||
checkOutput("isCheck.kt", "3\nnull\n");
|
||||
runMainAndCheckOutput("isCheck.kt", "3\nnull\n");
|
||||
}
|
||||
|
||||
|
||||
public void testPatternMatching() throws Exception {
|
||||
checkOutput("patternMatching.kt", "Greeting\n" +
|
||||
"One\n" +
|
||||
"Not a string\n" +
|
||||
"Unknown\n");
|
||||
runMainAndCheckOutput("patternMatching.kt", "Greeting\n" +
|
||||
"One\n" +
|
||||
"Not a string\n" +
|
||||
"Unknown\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,25 +16,23 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.test.semantics;
|
||||
|
||||
import org.jetbrains.kotlin.js.test.SingleFileTranslationTest;
|
||||
|
||||
public final class WebDemoExamples2Test extends SingleFileTranslationTest {
|
||||
public final class WebDemoExamples2Test extends AbstractWebDemoExamplesTest {
|
||||
|
||||
public WebDemoExamples2Test() {
|
||||
super("webDemoExamples2/");
|
||||
}
|
||||
|
||||
public void testBottles() throws Exception {
|
||||
performTestWithMain("bottles", "2", "2");
|
||||
performTestWithMain("bottles", "");
|
||||
runMainAndCheckOutputWithExpectedFile("bottles", "2", "2");
|
||||
runMainAndCheckOutputWithExpectedFile("bottles", "");
|
||||
}
|
||||
|
||||
public void testLife() throws Exception {
|
||||
performTestWithMain("life", "", "2");
|
||||
runMainAndCheckOutputWithExpectedFile("life", "", "2");
|
||||
}
|
||||
|
||||
public void testBuilder() throws Exception {
|
||||
performTestWithMain("builder", "");
|
||||
performTestWithMain("builder", "1", "over9000");
|
||||
runMainAndCheckOutputWithExpectedFile("builder", "");
|
||||
runMainAndCheckOutputWithExpectedFile("builder", "1", "over9000");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user