JS: make most of box tests running in mocha.js

This commit is contained in:
Alexey Andreev
2016-08-31 18:02:32 +03:00
parent 9da1a50cae
commit aa1a0307a6
4 changed files with 114 additions and 37 deletions
@@ -92,9 +92,10 @@ abstract class BasicBoxTest(
generateJavaScriptFile(file.parent, module, outputFileName, dependencies, modules.size > 1)
outputFileName
}
val mainModule = if (TEST_MODULE in modules) TEST_MODULE else DEFAULT_MODULE
val mainModuleName = if (TEST_MODULE in modules) TEST_MODULE else DEFAULT_MODULE
val mainModule = modules[mainModuleName]!!
val checker = RhinoFunctionResultChecker(mainModule, testFactory.testPackage, TEST_FUNCTION, "OK")
val checker = RhinoFunctionResultChecker(mainModuleName, testFactory.testPackage, TEST_FUNCTION, "OK")
val globalCommonFiles = JsTestUtils.getFilesInDirectoryByExtension(
TEST_DATA_DIR_PATH + COMMON_FILES_DIR, JavaScript.EXTENSION)
val localCommonFile = file.parent + "/" + COMMON_FILES_NAME + JavaScript.DOT_EXTENSION
@@ -117,10 +118,37 @@ abstract class BasicBoxTest(
val allJsFiles = additionalFiles + inputJsFiles + generatedJsFiles + globalCommonFiles + localCommonFiles +
additionalCommonFiles
val nodeRunnerName = mainModule.outputFileName(outputDir) + ".node.js"
val nodeRunnerText = generateNodeRunner(allJsFiles, outputDir, mainModuleName, testFactory.testPackage)
FileUtil.writeToFile(File(nodeRunnerName), nodeRunnerText)
RhinoUtils.runRhinoTest(allJsFiles, checker)
}
}
private fun generateNodeRunner(files: Collection<String>, dir: File, moduleName: String, testPackage: String?): String {
val sb = StringBuilder("var text = \"\";\n")
sb.append("var fs = require('fs');\n")
sb.append("module.exports = function(kotlin, requireFromString) {\n")
sb.append("text += 'module.exports = function(kotlin) {\\n';\n")
for (file in files) {
val fileName = FileUtil.getRelativePath(dir, File(file))!!
sb.append("text += fs.readFileSync(__dirname + \"/$fileName\") + \"\\n\";\n")
}
sb.append("text += 'return $moduleName;';\n")
sb.append("text += \"};\";\n")
val fqn = testPackage?.let { ".$it" } ?: ""
sb.append("var testModule = requireFromString(text)(kotlin);\n")
sb.append("return testModule$fqn.box();\n")
sb.append("};")
return sb.toString()
}
private fun getOutputDir(file: File): File {
val stopFile = File(pathToTestDir)
return generateSequence(file.parentFile) { it.parentFile }
@@ -130,9 +158,13 @@ abstract class BasicBoxTest(
.fold(File(pathToOutputDir), ::File)
}
private fun TestModule.outputFileName(directory: File): String {
private fun TestModule.outputFileSimpleName(): String {
val outputFileSuffix = if (this.name == TEST_MODULE) "" else "-$name"
return directory.absolutePath + "/" + getTestName(true) + "${outputFileSuffix}_v5"
return getTestName(true) + "${outputFileSuffix}_v5"
}
private fun TestModule.outputFileName(directory: File): String {
return directory.absolutePath + "/" + outputFileSimpleName()
}
private fun generateJavaScriptFile(
@@ -225,7 +257,10 @@ abstract class BasicBoxTest(
val ktFile = KtPsiFactory(project).createFile(text)
val boxFunction = ktFile.declarations.find { it is KtNamedFunction && it.name == TEST_FUNCTION }
if (boxFunction != null) {
testPackage = SingleFileTranslationTest.getPackageName(ktFile)
testPackage = ktFile.packageFqName.asString()
if (testPackage?.isEmpty() ?: false) {
testPackage = null
}
}
if (module != null) {
@@ -243,7 +278,7 @@ abstract class BasicBoxTest(
KotlinTestUtils.mkdirs(temporaryFile.parentFile)
temporaryFile.writeText(text, Charsets.UTF_8)
return TestFile(fileName, temporaryFile.absolutePath, module ?: defaultModule)
return TestFile(temporaryFile.absolutePath, module ?: defaultModule)
}
override fun createModule(name: String, dependencies: List<String>): TestModule? {
@@ -255,7 +290,7 @@ abstract class BasicBoxTest(
}
}
private class TestFile(val name: String, val fileName: String, val module: TestModule) {
private class TestFile(val fileName: String, val module: TestModule) {
init {
module.files += this
}
@@ -50,15 +50,12 @@ 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.js.translate.context.Namer;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtPsiFactory;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.KotlinTestWithEnvironment;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
@@ -392,20 +389,6 @@ public abstract class BasicTest extends KotlinTestWithEnvironment {
return libFiles;
}
@NotNull
protected String getPackageName(@NotNull String filename) throws IOException {
String content = FileUtil.loadFile(new File(filename), true);
KtPsiFactory psiFactory = new KtPsiFactory(getProject());
KtFile ktFile = psiFactory.createFile(content);
return getPackageName(ktFile);
}
@NotNull
protected static String getPackageName(KtFile ktFile) {
String packageName = ktFile.getPackageFqName().asString();
return packageName.isEmpty() ? Namer.getRootPackageName() : packageName;
}
protected static String getBaseName(String path) {
String systemIndependentPath = FileUtil.toSystemIndependentName(path);
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.js.test.rhino;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.js.translate.context.Namer;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
@@ -57,18 +56,15 @@ public class RhinoFunctionResultChecker implements RhinoResultChecker {
private String functionCallString() {
StringBuilder sb = new StringBuilder();
if (packageName != null) {
sb.append("kotlin.modules");
if (moduleId.contains(".")) {
sb.append("['").append(moduleId).append("']");
} else {
sb.append(".").append(moduleId);
}
if (!Namer.getRootPackageName().equals(packageName) ) {
sb.append('.').append(packageName);
}
sb.append('.');
sb.append("kotlin.modules");
if (moduleId.contains(".")) {
sb.append("['").append(moduleId).append("']");
} else {
sb.append(".").append(moduleId);
}
return sb.append(functionName).append("()").toString();
if (packageName != null) {
sb.append('.').append(packageName);
}
return sb.append(".").append(functionName).append("()").toString();
}
}
+63
View File
@@ -0,0 +1,63 @@
"use strict";
var assert = require('assert');
var fs = require('fs');
var kotlin = require(process.env.KOTLIN_JS_LOCATION);
var requireFromString = require('require-from-string');
var model = generateModel("out");
exposeModel(model, "./out");
function exposeModel(model, path) {
for (var property in model) {
if (!model.hasOwnProperty(property)) {
continue;
}
var childPath = path + "/" + property;
var item = model[property];
describe(property, function(childPath, item) {
return function() {
if (typeof item === "string") {
it("", function () {
var result = require(childPath);
assert.equal("OK", result(kotlin, requireFromString));
});
}
else if (typeof item === "object") {
exposeModel(item, childPath);
}
}
}(childPath, item));
}
}
/**
* @param path String
* @returns String|{*}
*/
function generateModel(path) {
var stats = fs.statSync(path);
if (stats.isDirectory()) {
var result = {};
var files = fs.readdirSync(path);
var empty = true;
for (var i = 0; i < files.length; ++i) {
var child = files[i];
var childModel = generateModel(path + "/" + child);
if (childModel !== void 0) {
result[child] = childModel;
empty = false;
}
}
return !empty ? result : void 0;
} else if (stats.isFile()) {
if (path.endsWith(".node.js")) {
return path;
} else {
return void 0;
}
} else {
return void 0;
}
}