Trying to run stdlib unit tests as part or js.tests: first step

This commit is contained in:
Pavel V. Talanov
2012-07-05 20:15:25 +04:00
parent 5683ee456f
commit ec7b0a1d3a
13 changed files with 391 additions and 57 deletions
@@ -43,8 +43,8 @@ public abstract class BasicTest extends TestWithEnvironment {
private static final String CASES = "cases/";
private static final String OUT = "out/";
private static final String EXPECTED = "expected/";
public static final String JSLINT_LIB = pathToTestFilesRoot() + "jslint.js";
public static final String JSLINT_LIB = pathToTestFilesRoot() + "jslint.js";
@NotNull
private String mainDirectory = "";
@@ -103,21 +103,35 @@ public abstract class BasicTest extends TestWithEnvironment {
@NotNull MainCallParameters mainCallParameters, @NotNull Iterable<EcmaVersion> ecmaVersions)
throws Exception {
for (EcmaVersion version : ecmaVersions) {
TranslationUtils.translateFiles(getProject(), files, getOutputFilePath(testName, version), mainCallParameters, version);
TranslationUtils.translateFiles(getProject(), withAdditionalFiles(files),
getOutputFilePath(testName, version), mainCallParameters, version);
}
}
protected void runRhinoTests(@NotNull String filename, @NotNull Iterable<EcmaVersion> ecmaVersions, @NotNull RhinoResultChecker checker) throws Exception {
@NotNull
private List<String> withAdditionalFiles(@NotNull List<String> files) {
List<String> result = Lists.newArrayList(files);
result.addAll(additionalKotlinFiles());
return result;
}
protected void runRhinoTests(@NotNull String filename, @NotNull Iterable<EcmaVersion> ecmaVersions,
@NotNull RhinoResultChecker checker) throws Exception {
for (EcmaVersion ecmaVersion : ecmaVersions) {
runRhinoTest(withAdditionalFiles(getOutputFilePath(filename, ecmaVersion), ecmaVersion), checker, getRhinoTestVariables(),
ecmaVersion);
}
}
protected Map<String,Object> getRhinoTestVariables() throws Exception {
protected Map<String, Object> getRhinoTestVariables() throws Exception {
return null;
}
@NotNull
protected List<String> additionalKotlinFiles() {
return Lists.newArrayList();
}
protected static String casesDirectoryName() {
return CASES;
@@ -0,0 +1,70 @@
/*
* 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.semantics;
import closurecompiler.internal.com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.test.MultipleFilesTranslationTest;
import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.k2js.test.utils.LibraryFilePathsUtil.getAdditionalLibraryFiles;
/**
* @author Pavel Talanov
*/
public class JsUnitTestBase extends MultipleFilesTranslationTest {
@NotNull
private static final String JS_TESTS = pathToTestFilesRoot() + "jsTester/";
@NotNull
protected static final String JS_TESTS_KT = JS_TESTS + "jsTester.kt";
@NotNull
protected static final String JS_TESTS_JS = JS_TESTS + "jsTester.js";
public JsUnitTestBase() {
super("jsUnitTests/");
}
@NotNull
@Override
protected List<String> additionalJSFiles(@NotNull EcmaVersion ecmaVersion) {
ArrayList<String> result = Lists.newArrayList(super.additionalJSFiles(ecmaVersion));
result.add(JS_TESTS_JS);
return result;
}
@NotNull
@Override
protected List<String> additionalKotlinFiles() {
ArrayList<String> result = Lists.newArrayList();
List<String> additionalLibraryFiles = getAdditionalLibraryFiles();
additionalLibraryFiles.add(JS_TESTS_KT);
boolean removed = additionalLibraryFiles.remove(Config.LIBRARIES_LOCATION + "/stdlib/testCode.kt");
assert removed;
result.addAll(additionalLibraryFiles);
return result;
}
public void testDummy() throws Exception {
checkFooBoxIsTrue("test");
}
}
@@ -27,6 +27,7 @@ import org.jetbrains.jet.cli.js.K2JSCompilerArguments;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.test.SingleFileTranslationTest;
import org.jetbrains.k2js.test.utils.LibraryFilePathsUtil;
import javax.annotation.Nullable;
import java.io.File;
@@ -47,7 +48,7 @@ abstract class StdLibTestBase extends SingleFileTranslationTest {
}
private void compileFiles(@NotNull EnumSet<EcmaVersion> ecmaVersions, @NotNull List<String> files) throws Exception {
List<String> libFiles = createLibFilesList();
List<String> libFiles = LibraryFilePathsUtil.getBasicLibraryFiles();
for (EcmaVersion version : ecmaVersions) {
String outputFilePath = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
invokeCompiler(files, libFiles, version, outputFilePath);
@@ -84,37 +85,20 @@ abstract class StdLibTestBase extends SingleFileTranslationTest {
}
@NotNull
private static List<String> createLibFilesList() {
return Lists.transform(Config.LIB_FILE_NAMES, new Function<String, String>() {
@Override
public String apply(@Nullable String s) {
return Config.LIBRARIES_LOCATION + s;
}
});
private static List<String> constructFilesToCompileList(@NotNull String sourceDir, @NotNull String[] stdLibFiles) {
List<String> files = filesFromDir(sourceDir, stdLibFiles);
files.addAll(LibraryFilePathsUtil.getAdditionalLibraryFiles());
return files;
}
@NotNull
private static List<String> constructFilesToCompileList(@NotNull String sourceDir, @NotNull String[] stdLibFiles) {
private static List<String> filesFromDir(@NotNull String sourceDir, @NotNull String[] stdLibFiles) {
List<String> files = Lists.newArrayList();
File stdlibDir = new File(sourceDir);
assertTrue("Cannot find stdlib source: " + stdlibDir, stdlibDir.exists());
for (String file : stdLibFiles) {
files.add(new File(stdlibDir, file).getPath());
}
// lets add the standard JS library files
Iterable<String> names = Config.LIB_FILE_NAMES_DEPENDENT_ON_STDLIB;
for (String libFileName : names) {
System.out.println("Compiling " + libFileName);
files.add(Config.LIBRARIES_LOCATION + libFileName);
}
// lets add the standard Kotlin library files
for (String libFileName : Config.STDLIB_FILE_NAMES) {
System.out.println("Compiling " + libFileName);
files.add(Config.STDLIB_LOCATION + libFileName);
}
return files;
}
}
@@ -60,7 +60,7 @@ public final class JsTestUtils {
@NotNull
public static List<String> getAllFilesInDir(@NotNull String dirName) {
File dir = new File(dirName);
assert dir.isDirectory();
assert dir.isDirectory() : dir + " is not a directory.";
List<String> fullFilePaths = new ArrayList<String>();
for (String fileName : dir.list()) {
fullFilePaths.add(dirName + "/" + fileName);
@@ -0,0 +1,61 @@
/*
* 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.utils;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.config.Config;
import javax.annotation.Nullable;
import java.util.List;
/**
* @author Pavel Talanov
*/
public final class LibraryFilePathsUtil {
private LibraryFilePathsUtil() {
}
@NotNull
public static List<String> getBasicLibraryFiles() {
return Lists.transform(Config.LIB_FILE_NAMES, new Function<String, String>() {
@Override
public String apply(@Nullable String s) {
return Config.LIBRARIES_LOCATION + s;
}
});
}
@NotNull
public static List<String> getAdditionalLibraryFiles() {
List<String> additionalKotlinFiles = Lists.newArrayList();
// lets add the standard JS library files
Iterable<String> names = Config.LIB_FILE_NAMES_DEPENDENT_ON_STDLIB;
for (String libFileName : names) {
System.out.println("Compiling " + libFileName);
additionalKotlinFiles.add(Config.LIBRARIES_LOCATION + libFileName);
}
// lets add the standard Kotlin library files
for (String libFileName : Config.STDLIB_FILE_NAMES) {
System.out.println("Compiling " + libFileName);
additionalKotlinFiles.add(Config.STDLIB_LOCATION + libFileName);
}
return additionalKotlinFiles;
}
}
@@ -19,7 +19,6 @@ package org.jetbrains.k2js.translate.context;
import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.name.NameUtils;
import org.jetbrains.k2js.translate.utils.JsAstUtils;
/**
@@ -54,8 +53,7 @@ public final class NamingScope {
}
@NotNull
/*package*/ JsName declareUnobfuscatableName(@NotNull String name) {
NameUtils.requireIdentifier(name);
/*package*/ JsName declareUnobfuscatableName(@NotNull String name) {
JsName declaredName = scope.declareName(name);
declaredName.setObfuscatable(false);
return declaredName;
@@ -36,6 +36,7 @@ import java.util.List;
/**
* @author Pavel Talanov
*/
//TODO: use method object instead of static functions
public final class JSTestGenerator {
private JSTestGenerator() {
}
@@ -44,23 +45,27 @@ public final class JSTestGenerator {
@NotNull Collection<JetFile> files,
@NotNull JsBlock block) {
List<FunctionDescriptor> functionDescriptors = JetTestFunctionDetector.getTestFunctionDescriptors(context.bindingContext(), files);
doGenerateTestCalls(functionDescriptors, block, context);
doGenerateTestCalls(functionDescriptors, context, new PlainAssertionTester(block, context));
}
private static void doGenerateTestCalls(@NotNull List<FunctionDescriptor> functionDescriptors,
@NotNull JsBlock block,
@NotNull TranslationContext context) {
@NotNull TranslationContext context, @NotNull JSTester jsTester) {
for (FunctionDescriptor functionDescriptor : functionDescriptors) {
ClassDescriptor classDescriptor = JsDescriptorUtils.getContainingClass(functionDescriptor);
if (classDescriptor == null) {
return;
}
JsExpression expression = ReferenceTranslator.translateAsFQReference(classDescriptor, context);
JsNew testClass = new JsNew(expression);
JsExpression functionToTestCall = CallBuilder.build(context).descriptor(functionDescriptor).receiver(testClass).translate();
JsStringLiteral testName = context.program().getStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName());
(new JSTester(block, context)).constructTestMethodInvocation(functionToTestCall, testName);
generateCodeForTestMethod(context, functionDescriptor, classDescriptor, jsTester);
}
}
private static void generateCodeForTestMethod(@NotNull TranslationContext context,
@NotNull FunctionDescriptor functionDescriptor,
@NotNull ClassDescriptor classDescriptor, @NotNull JSTester tester) {
JsExpression expression = ReferenceTranslator.translateAsFQReference(classDescriptor, context);
JsNew testClass = new JsNew(expression);
JsExpression functionToTestCall = CallBuilder.build(context).descriptor(functionDescriptor).receiver(testClass).translate();
JsStringLiteral testName = context.program().getStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName());
tester.constructTestMethodInvocation(functionToTestCall, testName);
}
}
@@ -16,35 +16,26 @@
package org.jetbrains.k2js.translate.test;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import com.google.dart.compiler.backend.js.ast.JsBlock;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsStringLiteral;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.context.TranslationContext;
import static com.google.dart.compiler.util.AstUtil.newBlock;
import static com.google.dart.compiler.util.AstUtil.newInvocation;
/**
* @author Pavel Talanov
*/
public class JSTester {
public abstract class JSTester {
@NotNull
private final JsBlock block;
protected final JsBlock block;
@NotNull
private final TranslationContext context;
@NotNull
private static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("QUnit.test");
protected final TranslationContext context;
public JSTester(@NotNull JsBlock block, @NotNull TranslationContext context) {
this.block = block;
this.context = context;
}
void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall,
@NotNull JsStringLiteral testName) {
JsFunction functionToTest = new JsFunction(this.context.jsScope());
functionToTest.setBody(newBlock(functionToTestCall.makeStmt()));
block.getStatements().add(newInvocation(TEST_FUN_REF, testName, functionToTest).makeStmt());
}
public abstract void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name);
}
@@ -0,0 +1,37 @@
/*
* 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.translate.test;
import com.google.dart.compiler.backend.js.ast.JsBlock;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsStringLiteral;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.context.TranslationContext;
/**
* @author Pavel Talanov
*/
public final class PlainAssertionTester extends JSTester {
public PlainAssertionTester(@NotNull JsBlock block, @NotNull TranslationContext context) {
super(block, context);
}
@Override
public void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name) {
block.getStatements().add(call.makeStmt());
}
}
@@ -0,0 +1,45 @@
/*
* 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.translate.test;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.context.TranslationContext;
import static com.google.dart.compiler.util.AstUtil.newBlock;
import static com.google.dart.compiler.util.AstUtil.newInvocation;
/**
* @author Pavel Talanov
*/
public final class QUnitTester extends JSTester {
@NotNull
private static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("QUnit.test");
public QUnitTester(@NotNull JsBlock block, @NotNull TranslationContext context) {
super(block, context);
}
@Override
public void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall,
@NotNull JsStringLiteral testName) {
JsFunction functionToTest = new JsFunction(this.context.jsScope());
functionToTest.setBody(newBlock(functionToTestCall.makeStmt()));
block.getStatements().add(newInvocation(TEST_FUN_REF, testName, functionToTest).makeStmt());
}
}
@@ -0,0 +1,49 @@
/*
* 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.
*/
/*Asserter*/
var JsTests = (function() {
var out = null;
function setOut(newOut) {
out = newOut;
}
var Asserter = function(out) {
this.out = out;
this.assertTrue = function (message, actual) {
if (!actual) {
this.out.println(message);
}
};
this.assertEquals = function (message, actual) {
};
};
var test = function(testName, testFun) {
try {
testFun();
}
catch (fail) {
out.println("Test " + testName + " failed");
}
};
return {
test : test,
Asserter : Asserter,
setOut : setOut
}
})();
@@ -0,0 +1,25 @@
package kotlin.test
import kotlin.test.*
import js.*
public var asserter : Asserter = JsTestsAsserter()
native("JsTests.Asserter")
public class JsTestsAsserter(): Asserter {
native
public override fun assertTrue(message: String, actual: Boolean) = noImpl
native
public override fun assertEquals(message: String, expected: Any?, actual: Any?) = noImpl
native
public override fun assertNotNull(message: String, actual: Any?) = noImpl
native
public override fun assertNull(message: String, actual: Any?) = noImpl
native
public override fun fail(message: String) = noImpl
}
@@ -0,0 +1,55 @@
package test.collections
import java.util.ArrayList
import kotlin.test.*
import org.junit.Test as test
class ListTest {
test fun toString() {
val data = arrayList("foo", "bar")
assertEquals("[foo, bar]", data.toString())
}
test fun head() {
val data = arrayList("foo", "bar")
assertEquals("foo", data.head)
}
test fun tail() {
val data = arrayList("foo", "bar", "whatnot")
val actual = data.tail
val expected = arrayList("bar", "whatnot")
assertEquals(expected, actual)
}
test fun first() {
val data = arrayList("foo", "bar")
assertEquals("foo", data.first)
}
test fun last() {
val data = arrayList("foo", "bar")
assertEquals("bar", data.last)
}
/* test fun withIndices() {
val data = arrayList("foo", "bar")
val wis = data.withIndices()
var index = 0
for (withIndex in wis) {
assertEquals(withIndex._1, index)
assertEquals(withIndex._2, data[index])
index++
}
assertEquals(data.size(), index)
}*/
test fun lastIndex() {
val emptyData = ArrayList<String>()
val data = arrayList("foo", "bar")
assertEquals(-1, emptyData.lastIndex)
assertEquals(1, data.lastIndex)
}
}