Compare commits

...

2 Commits

Author SHA1 Message Date
Anton Bannykh
558fba32a1 wip 2017-06-26 19:43:29 +03:00
Anton Bannykh
cd39f6865f JS tests: changes to kotlin.test + the way compiler tests are generated. 2017-06-26 16:43:30 +03:00
32 changed files with 939 additions and 319 deletions

View File

@@ -575,6 +575,17 @@ public class DescriptorUtils {
@NotNull
public static FunctionDescriptor getFunctionByName(@NotNull MemberScope scope, @NotNull Name name) {
FunctionDescriptor result = getFunctionByNameOrNull(scope, name);
if (result == null) {
throw new IllegalStateException("Function not found");
}
return result;
}
@Nullable
public static FunctionDescriptor getFunctionByNameOrNull(@NotNull MemberScope scope, @NotNull Name name) {
Collection<DeclarationDescriptor> functions = scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS,
MemberScope.Companion.getALL_NAME_FILTER());
for (DeclarationDescriptor d : functions) {
@@ -583,7 +594,7 @@ public class DescriptorUtils {
}
}
throw new IllegalStateException("Function not found");
return null;
}
@NotNull

View File

@@ -1,115 +0,0 @@
/*
* Copyright 2010-2015 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.translate.general;
import com.google.common.collect.Lists;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter;
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
import org.jetbrains.kotlin.types.KotlinType;
import java.util.Collection;
import java.util.List;
/**
* Helps find functions which are annotated with a @Test annotation from junit
*/
public class JetTestFunctionDetector {
private JetTestFunctionDetector() {
}
private static boolean isTest(@NotNull FunctionDescriptor functionDescriptor) {
Annotations annotations = functionDescriptor.getAnnotations();
for (AnnotationDescriptor annotation : annotations) {
// TODO ideally we should find the fully qualified name here...
KotlinType type = annotation.getType();
String name = type.toString();
if (name.equals("Test")) {
return true;
}
}
/*
if (function.getName().startsWith("test")) {
List<JetParameter> parameters = function.getValueParameters();
return parameters.size() == 0;
}
*/
return false;
}
@NotNull
public static List<FunctionDescriptor> getTestFunctionDescriptors(
@NotNull ModuleDescriptor moduleDescriptor
) {
List<FunctionDescriptor> answer = Lists.newArrayList();
getTestFunctions(FqName.ROOT, moduleDescriptor, answer);
return answer;
}
private static void getTestFunctions(
@NotNull FqName packageName,
@NotNull ModuleDescriptor moduleDescriptor,
@NotNull List<FunctionDescriptor> foundFunctions
) {
for (PackageFragmentDescriptor packageDescriptor : moduleDescriptor.getPackage(packageName).getFragments()) {
if (DescriptorUtils.getContainingModule(packageDescriptor) != moduleDescriptor) continue;
Collection<DeclarationDescriptor> descriptors = packageDescriptor.getMemberScope().getContributedDescriptors(
DescriptorKindFilter.CLASSIFIERS, MemberScope.Companion.getALL_NAME_FILTER());
for (DeclarationDescriptor descriptor : descriptors) {
if (descriptor instanceof ClassDescriptor) {
getTestFunctions((ClassDescriptor) descriptor, foundFunctions);
}
}
}
for (FqName subpackageName : moduleDescriptor.getSubPackagesOf(packageName, MemberScope.Companion.getALL_NAME_FILTER())) {
getTestFunctions(subpackageName, moduleDescriptor, foundFunctions);
}
}
private static void getTestFunctions(
@NotNull ClassDescriptor classDescriptor,
@NotNull List<FunctionDescriptor> foundFunctions
) {
if (classDescriptor.getModality() == Modality.ABSTRACT) return;
Collection<DeclarationDescriptor> allDescriptors = classDescriptor.getUnsubstitutedMemberScope().getContributedDescriptors(
DescriptorKindFilter.FUNCTIONS, MemberScope.Companion.getALL_NAME_FILTER());
List<FunctionDescriptor> testFunctions = ContainerUtil.mapNotNull(
allDescriptors,
descriptor-> {
if (descriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
if (isTest(functionDescriptor)) return functionDescriptor;
}
return null;
});
foundFunctions.addAll(testFunctions);
}
}

View File

@@ -39,8 +39,6 @@ import org.jetbrains.kotlin.js.translate.declaration.FileDeclarationVisitor;
import org.jetbrains.kotlin.js.translate.expression.ExpressionVisitor;
import org.jetbrains.kotlin.js.translate.expression.PatternTranslator;
import org.jetbrains.kotlin.js.translate.test.JSTestGenerator;
import org.jetbrains.kotlin.js.translate.test.JSTester;
import org.jetbrains.kotlin.js.translate.test.QUnitTester;
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils;
import org.jetbrains.kotlin.js.translate.utils.BindingUtils;
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
@@ -392,8 +390,8 @@ public final class Translation {
) {
StaticContext staticContext = new StaticContext(trace, config, moduleDescriptor);
TranslationContext context = TranslationContext.rootContext(staticContext);
JSTester tester = new QUnitTester(context);
JSTestGenerator.generateTestCalls(context, moduleDescriptor, tester);
new JSTestGenerator(context).generateTestCalls(moduleDescriptor);
return staticContext.getFragment();
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2010-2015 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.translate.test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.js.backend.ast.*;
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
public abstract class CommonUnitTester extends JSTester {
public CommonUnitTester(@NotNull TranslationContext context) {
super(context);
}
@Override
public void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall,
@NotNull JsStringLiteral testName) {
JsFunction functionToTest = new JsFunction(getContext().scope(), "test function");
functionToTest.setBody(new JsBlock(functionToTestCall.makeStmt()));
getContext().addTopLevelStatement(new JsInvocation(getTestMethodRef(), testName, functionToTest).makeStmt());
}
@NotNull
protected abstract JsExpression getTestMethodRef();
}

View File

@@ -1,68 +0,0 @@
/*
* 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.translate.test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
import org.jetbrains.kotlin.js.backend.ast.JsNew;
import org.jetbrains.kotlin.js.backend.ast.JsStringLiteral;
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
import org.jetbrains.kotlin.js.translate.general.JetTestFunctionDetector;
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import java.util.Collections;
import java.util.List;
//TODO: use method object instead of static functions
public final class JSTestGenerator {
private JSTestGenerator() {
}
public static void generateTestCalls(@NotNull TranslationContext context,
@NotNull ModuleDescriptor moduleDescriptor, @NotNull JSTester tester) {
List<FunctionDescriptor> functionDescriptors =
JetTestFunctionDetector.getTestFunctionDescriptors(moduleDescriptor);
doGenerateTestCalls(functionDescriptors, context, tester);
}
private static void doGenerateTestCalls(@NotNull List<FunctionDescriptor> functionDescriptors,
@NotNull TranslationContext context, @NotNull JSTester jsTester) {
for (FunctionDescriptor functionDescriptor : functionDescriptors) {
ClassDescriptor classDescriptor = DescriptorUtils.getContainingClass(functionDescriptor);
if (classDescriptor == null) {
return;
}
generateCodeForTestMethod(context, functionDescriptor, classDescriptor, jsTester);
}
}
private static void generateCodeForTestMethod(@NotNull TranslationContext context,
@NotNull FunctionDescriptor functionDescriptor,
@NotNull ClassDescriptor classDescriptor, @NotNull JSTester tester) {
JsExpression expression = ReferenceTranslator.translateAsValueReference(classDescriptor, context);
JsNew testClass = new JsNew(expression);
JsExpression functionToTestCall =
CallTranslator.INSTANCE.buildCall(context, functionDescriptor, Collections.emptyList(), testClass);
JsStringLiteral testName = new JsStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName());
tester.constructTestMethodInvocation(functionToTestCall, testName);
}
}

View File

@@ -0,0 +1,152 @@
/*
* 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.translate.test
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.js.backend.ast.*
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator
import org.jetbrains.kotlin.js.translate.context.TranslationContext
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScope
//TODO: use method object instead of static functions
class JSTestGenerator(val context: TranslationContext) {
private fun findFunction(name: String): JsExpression {
for (d in context.config.moduleDescriptors) {
if ("<kotlin-test>" == d.name) {
val descriptor = DescriptorUtils.getFunctionByNameOrNull(d.data.getPackage(FqName.ROOT).memberScope,
Name.identifier(name)) ?: continue
return context.getQualifiedReference(descriptor)
}
}
return JsNameRef(name, JsNameRef("Kotlin"))
}
fun generateTestCalls(moduleDescriptor: ModuleDescriptor) {
val rootFunction = JsFunction(context.scope(), JsBlock(), "root suite function")
generateTestCalls(moduleDescriptor, FqName.ROOT, rootFunction)
if (!rootFunction.body.isEmpty) {
context.addTopLevelStatement(JsInvocation(suiteRef, JsStringLiteral(""), rootFunction).makeStmt())
}
}
private fun generateTestCalls(moduleDescriptor: ModuleDescriptor, packageName: FqName, parentFun: JsFunction) {
for (packageDescriptor in moduleDescriptor.getPackage(packageName).fragments) {
if (DescriptorUtils.getContainingModule(packageDescriptor) !== moduleDescriptor) continue
packageDescriptor.getMemberScope().getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS, MemberScope.ALL_NAME_FILTER).forEach {
if (it is ClassDescriptor) {
generateTestFunctions(it, parentFun)
}
}
}
for (subpackageName in moduleDescriptor.getSubPackagesOf(packageName, MemberScope.ALL_NAME_FILTER)) {
val subPackageFunction = JsFunction(context.scope(), JsBlock(), "${subpackageName.asString()} package suite function")
generateTestCalls(moduleDescriptor, subpackageName, subPackageFunction)
if (!subPackageFunction.body.isEmpty) {
parentFun.body.statements += JsInvocation(suiteRef, JsStringLiteral(subpackageName.shortName().asString()), subPackageFunction).makeStmt()
}
}
}
private fun generateTestFunctions(classDescriptor: ClassDescriptor, parentFun: JsFunction) {
if (classDescriptor.modality === Modality.ABSTRACT) return
val suiteFunction = JsFunction(context.scope(), JsBlock(), "suite function")
classDescriptor.unsubstitutedMemberScope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS, MemberScope.ALL_NAME_FILTER).forEach {
if (it is FunctionDescriptor && it.isTest) {
generateCodeForTestMethod(it, classDescriptor, suiteFunction)
}
}
if (!suiteFunction.body.isEmpty) {
val suiteName = JsStringLiteral(classDescriptor.name.toString())
parentFun.body.statements += JsInvocation(classDescriptor.ref, suiteName, suiteFunction).makeStmt()
}
}
private fun generateCodeForTestMethod(functionDescriptor: FunctionDescriptor, classDescriptor: ClassDescriptor, parentFun: JsFunction) {
val functionToTest = generateTestFunction(functionDescriptor, classDescriptor, parentFun.scope)
val testName = JsStringLiteral(functionDescriptor.name.toString())
parentFun.body.statements += JsInvocation(functionDescriptor.ref, testName, functionToTest).makeStmt()
}
private fun generateTestFunction(functionDescriptor: FunctionDescriptor, classDescriptor: ClassDescriptor, scope: JsScope): JsFunction {
val expression = ReferenceTranslator.translateAsValueReference(classDescriptor, context)
val testClass = JsNew(expression)
val functionToTestCall = CallTranslator.buildCall(context, functionDescriptor, emptyList<JsExpression>(), testClass)
val functionToTest = JsFunction(scope, "test function")
functionToTest.body = JsBlock(functionToTestCall.makeStmt())
return functionToTest
}
private val suiteRef: JsExpression = findFunction("suite")
private val fsuiteRef: JsExpression = findFunction("fsuite")
private val xsuiteRef: JsExpression = findFunction("xsuite")
private val testRef: JsExpression = findFunction("test")
private val ignoreRef: JsExpression = findFunction("xtest")
private val onlyRef: JsExpression = findFunction("ftest")
private val ClassDescriptor.ref: JsExpression
get() = when {
isIgnored -> xsuiteRef
isFocused -> fsuiteRef
else -> suiteRef
}
private val FunctionDescriptor.ref: JsExpression
get() = when {
isIgnored -> ignoreRef
isFocused -> onlyRef
else -> testRef
}
/**
* JUnit3 style:
* if (function.getName().startsWith("test")) {
* List<JetParameter> parameters = function.getValueParameters();
* return parameters.size() == 0;
* }
*/
private val FunctionDescriptor.isTest
get() = annotationFinder("Test", "kotlin.test", "org.junit") // Support both ways for now.
private val DeclarationDescriptor.isIgnored
get() = annotationFinder("Ignore", "kotlin.test")
private val DeclarationDescriptor.isFocused
get() = annotationFinder("Only", "kotlin.test")
private fun DeclarationDescriptor.annotationFinder(shortName: String, vararg packages: String) = packages.any { packageName ->
annotations.hasAnnotation(FqName("$packageName.$shortName"))
}
}

View File

@@ -1,39 +0,0 @@
/*
* Copyright 2010-2015 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.translate.test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
import org.jetbrains.kotlin.js.backend.ast.JsStringLiteral;
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
public abstract class JSTester {
@NotNull
private final TranslationContext context;
public JSTester(@NotNull TranslationContext context) {
this.context = context;
}
public abstract void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name);
@NotNull
protected TranslationContext getContext() {
return context;
}
}

View File

@@ -1,37 +0,0 @@
/*
* Copyright 2010-2015 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.translate.test;
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
import org.jetbrains.kotlin.js.backend.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
public final class QUnitTester extends CommonUnitTester {
public QUnitTester(@NotNull TranslationContext context) {
super(context);
}
@NotNull
private static final JsNameRef TEST_FUN_REF = new JsNameRef("test", "QUnit");
@Override
@NotNull
protected JsExpression getTestMethodRef() {
return TEST_FUN_REF;
}
}

View File

@@ -1,7 +1,6 @@
// EXPECTED_REACHABLE_NODES: 503
package foo
fun box(): String {
val s = StringBuilder()
s.append("a")

View File

@@ -1,7 +1,7 @@
package kotlin.test.tests
import org.junit.*
import kotlin.test.*
import org.junit.Test
class BasicAssertionsTest {
@Test

View File

@@ -0,0 +1,75 @@
plugins {
id "com.moowork.node" version "1.2.0"
}
description = 'Kotlin-test integration tests for JS'
apply plugin: 'kotlin-platform-js'
dependencies {
compile project(':kotlin-test:kotlin-test-js')
}
sourceSets {
main.kotlin.srcDirs += 'src'
test.kotlin.srcDirs += 'test'
}
compileKotlin2Js {
kotlinOptions {
moduleKind = "commonjs"
}
}
compileTestKotlin2Js {
kotlinOptions {
moduleKind = "commonjs"
}
}
compileKotlin2Js.doLast {
configurations.compile.each { File file ->
copy {
includeEmptyDirs = false
from zipTree(file.absolutePath)
into "${buildDir}/classes/"
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
}
}
}
}
node {
version = '6.11.0'
download = true
}
task testJest(type: NpmTask, dependsOn: [compileTestKotlin2Js, npmInstall]) {
args = ['run', 'test-jest']
}
check.dependsOn testJest
task testJasmine(type: NpmTask, dependsOn: [compileTestKotlin2Js, npmInstall]) {
args = ['run', 'test-jasmine']
}
check.dependsOn testJasmine
task testMocha(type: NpmTask, dependsOn: [compileTestKotlin2Js, npmInstall]) {
args = ['run', 'test-mocha']
}
check.dependsOn testMocha
task testQunit(type: NpmTask, dependsOn: [compileTestKotlin2Js, npmInstall]) {
args = ['run', 'test-qunit']
}
check.dependsOn testQunit
task testTape(type: NpmTask, dependsOn: [compileTestKotlin2Js, npmInstall]) {
args = ['run', 'test-tape']
}
check.dependsOn testTape

View File

@@ -0,0 +1,22 @@
var full = {
' SimpleTest testFoo': 'fail',
' SimpleTest testBar': 'pass',
' SimpleTest testFooWrong': 'pending',
' TestTest emptyTest': 'pending',
' org OrgTest test': 'pass',
' org some SomeTest test': 'pass',
' org some name NameTest test': 'pass',
' org other name NameTest test': 'pass'
};
// Filter out pending tests for Tape
var noPending = {};
for (var name in full) {
var result = full[name];
if (result !== 'pending') {
noPending[name] = result;
}
}
module.exports.full = full;
module.exports.noPending = noPending;

View File

@@ -0,0 +1,21 @@
var Tester = require('./test-result-checker');
var tester = new Tester(require('./expected-outcomes').full);
process.on('exit', function() {
tester.end();
});
jasmine.getEnv().addReporter({
specDone: function(result) {
var status = result.status;
if (status === 'passed') {
tester.passed(result.fullName);
}
else if (status === 'failed') {
tester.failed(result.fullName);
}
else {
tester.pending(result.fullName);
}
}
});

View File

@@ -0,0 +1,22 @@
var Tester = require('./test-result-checker');
var expectedOutcomes = require('./expected-outcomes').full;
module.exports = function (results) {
var tester = new Tester(expectedOutcomes);
var testResults = results.testResults[0].testResults;
for (var i = 0; i < testResults.length; i++) {
var tr = testResults[i];
if (tr.status === 'passed') {
tester.passed(tr.fullName);
}
else if (tr.status === 'failed') {
tester.failed(tr.fullName);
}
else {
tester.pending(tr.fullName);
}
}
tester.end();
};

View File

@@ -0,0 +1,25 @@
var mocha = require('mocha');
var Tester = require('./test-result-checker');
var expectedOutcomes = require('./expected-outcomes').full;
module.exports = function (runner) {
mocha.reporters.Base.call(this, runner);
var tester = new Tester(expectedOutcomes);
runner.on('pass', function (test) {
tester.passed(test.fullTitle());
});
runner.on('fail', function (test, err) {
tester.failed(test.fullTitle());
});
runner.on('pending', function (test) {
tester.pending(test.fullTitle());
});
runner.on('end', function () {
tester.end();
});
};

View File

@@ -0,0 +1,5 @@
var paths = require('app-module-path');
paths.addPath('build/classes');
paths.addPath('build/classes/main');
paths.addPath('build/classes/test');

View File

@@ -0,0 +1,20 @@
var Tester = require('./test-result-checker');
var tester = new Tester(require('./expected-outcomes').full);
QUnit.testDone(function (details) {
var testName = details.module + ' ' + details.name;
if (details.skipped) {
tester.pending(testName);
}
else if (!details.failed) {
tester.passed(testName);
}
else {
tester.failed(testName);
}
});
QUnit.done(function (details) {
details.failed = tester._total - tester._passed;
});

View File

@@ -0,0 +1,67 @@
var tape = require('tape');
var kotlin_test = require('kotlin-test');
var setAssertHook = function(t) {
kotlin_test.setAssertHook(function (result, expected, actual, lazyMessage) {
t.ok(result, lazyMessage());
if (!result) {
t.end();
}
});
};
var suiteContext;
kotlin_test.setAdapter({
suite: function (name, fn) {
tape(name, function(t) {
var prevContext = suiteContext;
suiteContext = t;
fn();
suiteContext = prevContext;
});
},
xsuite: function (name, fn) {
tape.skip(name, function(t) {
var prevContext = suiteContext;
suiteContext = t;
fn();
suiteContext = prevContext;
});
},
fsuite: function (name, fn) {
tape(name, function(t) {
var prevContext = suiteContext;
suiteContext = t;
fn();
suiteContext = prevContext;
});
},
test: function (name, fn) {
suiteContext.test(name, function (t) {
setAssertHook(t);
fn();
t.end();
});
},
xtest: function (name, fn) {
suiteContext.test(name, { skip: true}, function (t) {
setAssertHook(t);
fn();
t.end();
});
},
ftest: function (name, fn) {
suiteContext.test(name, function (t) {
setAssertHook(t);
fn();
t.end();
});
}
});

View File

@@ -0,0 +1,38 @@
var test = require('tape');
var path = require('path');
var Tester = require('./test-result-checker');
// Tape doesn't report pending tests.
// See https://github.com/substack/tape/pull/197 and https://github.com/substack/tape/issues/90
var tester = new Tester(require('./expected-outcomes').noPending);
process.on('exit', function () {
tester.end();
});
var stream = test.createStream({objectMode: true});
var nameStack = [];
stream.on('data', function (row) {
console.log(JSON.stringify(row));
if (row.type === 'test') {
nameStack.push(row.name);
}
else if (row.type === 'end') {
nameStack.pop();
}
else if (row.type === 'assert') {
var name = nameStack.join(' ');
if (row.ok) {
tester.passed(name);
}
else {
tester.failed(name);
}
}
});
process.argv.slice(2).forEach(function (file) {
require(path.resolve(file));
});

View File

@@ -0,0 +1,49 @@
var Tester = function(testMap) {
this._testMap = testMap;
this._testCount = {};
this._passed = 0;
this._total = 0;
};
Tester.prototype._check = function(name, result) {
this._total++;
var count = this._testCount[name] | 0;
this._testCount = count + 1;
if (count === 1) {
throw new Error('Duplicate test: "' + name + '"');
}
var expected = this._testMap[name];
if (!expected) {
throw new Error('Unexpected test: "' + name + '"');
}
if (result !== expected) {
throw new Error('For test "' + name + '": expected ' + expected + ' actual ' + result);
}
this._passed++;
};
Tester.prototype.passed = function(name) {
this._check(name, 'pass');
};
Tester.prototype.failed = function(name) {
this._check(name, 'fail');
};
Tester.prototype.pending = function(name) {
this._check(name, 'pending');
};
Tester.prototype.end = function() {
console.log('Passage rate ' + this._passed + ' / ' + this._total);
process.exitCode = this._total - this._passed;
process.exit();
};
module.exports = Tester;

View File

@@ -0,0 +1,40 @@
{
"name": "it",
"version": "1.0.0",
"description": "",
"main": "js/index.js",
"scripts": {
"test-jasmine": "jasmine js/paths.js js/jasmine-reporter.js build/classes/test/it_test.js",
"test-jest": "jest",
"test-mocha": "mocha -r js/paths.js --reporter js/mocha-reporter.js build/classes/test/it_test.js",
"test-qunit": "qunit -d js/paths.js -c js/qunit-reporter.js -t build/classes/test/it_test.js",
"test-tape": "tape js/paths.js js/tape-reporter.js js/tape-plugin.js build/classes/test/it_test.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"app-module-path": "^2.2.0",
"jasmine": "^2.6.0",
"jest": "^20.0.4",
"mocha": "^3.4.2",
"qunit": "^1.0.0",
"tape": "^4.6.3",
"watchify": "^3.9.0"
},
"jest": {
"verbose": true,
"roots": [
"<rootDir>/build/classes/kotlin.js",
"<rootDir>/build/classes/kotlin-test.js",
"<rootDir>/build/classes/main/it_main.js",
"<rootDir>/build/classes/test/it_test.js"
],
"testResultsProcessor": "<rootDir>/js/jest-reporter.js",
"testRegex": "_test\\.js$",
"moduleNameMapper": {
"^kotlin$": "<rootDir>/build/classes/kotlin.js",
"^kotlin-test$": "<rootDir>/build/classes/kotlin-test.js",
"^it_main$": "<rootDir>/build/classes/main/it_main.js"
}
}
}

View File

@@ -0,0 +1 @@
fun foo() = 10

View File

@@ -0,0 +1,23 @@
import kotlin.test.*
class SimpleTest {
@Test fun testFoo() {
assertEquals(20, foo())
}
@Test fun testBar() {
assertEquals(10, foo())
}
@Ignore @Test fun testFooWrong() {
assertEquals(20, foo())
}
}
@Ignore
class TestTest {
@Test fun emptyTest() {
}
}

View File

@@ -0,0 +1,8 @@
package org
import kotlin.test.*
class OrgTest {
@Test fun test() {
}
}

View File

@@ -0,0 +1,8 @@
package org.other.name
import kotlin.test.*
class NameTest {
@Test fun test() {
}
}

View File

@@ -0,0 +1,8 @@
package org.some
import kotlin.test.*
class SomeTest {
@Test fun test() {
}
}

View File

@@ -0,0 +1,8 @@
package org.some.name
import kotlin.test.*
class NameTest {
@Test fun test() {
}
}

View File

@@ -0,0 +1,275 @@
/*
* 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.
*/
@JsName("suite")
fun suite(name: String, suiteFn: () -> Unit) {
currentAdapter.suite(name, suiteFn)
}
@JsName("xsuite")
fun xsuite(name: String, suiteFn: () -> Unit) {
currentAdapter.xsuite(name, suiteFn)
}
@JsName("fsuite")
fun fsuite(name: String, suiteFn: () -> Unit) {
currentAdapter.fsuite(name, suiteFn)
}
@JsName("test")
fun test(name: String, testFn: () -> Unit) {
currentAdapter.test(name, testFn)
}
@JsName("xtest")
fun xtest(name: String, testFn: () -> Unit) {
currentAdapter.xtest(name, testFn)
}
@JsName("only")
fun ftest(name: String, testFn: () -> Unit) {
currentAdapter.ftest(name, testFn)
}
internal var currentAdapter: FrameworkAdapter = DefaultAdapters.AUTO_DETECTED
@JsName("setAdapter")
fun setAdapter(adapter: dynamic) {
if (js("typeof adapter === 'string'")) {
if (adapter in DefaultAdapters.NAME_TO_ADAPTER) {
setAdapter(DefaultAdapters.NAME_TO_ADAPTER[adapter])
} else {
throw IllegalArgumentException("Unsupported test framework adapter: '$adapter'")
}
} else {
currentAdapter = adapter
}
}
@JsName("setAssertHook")
fun setAssertHook(hook: (result: Boolean, expected: Any?, actual: Any?, lazyMessage: () -> String?) -> Unit) {
assertHook = hook
}
external interface FrameworkAdapter {
fun suite(name: String, suiteFn: () -> Unit)
fun xsuite(name: String, suiteFn: () -> Unit)
fun fsuite(name: String, suiteFn: () -> Unit)
fun test(name: String, testFn: () -> Unit)
fun xtest(name: String, testFn: () -> Unit)
fun ftest(name: String, testFn: () -> Unit)
}
enum class DefaultAdapters : FrameworkAdapter {
QUNIT {
var scopeStack = mutableListOf<String>()
var shouldSkip = false
val testName: String
get() = scopeStack.joinToString(separator = " ")
private fun withName(name: String, block: () -> Unit) {
scopeStack.add(name)
block()
scopeStack.removeAt(scopeStack.size - 1)
}
override fun suite(name: String, suiteFn: () -> Unit) = withName(name, suiteFn)
override fun xsuite(name: String, suiteFn: () -> Unit) {
if (shouldSkip) {
suiteFn()
}
else {
shouldSkip = true
suiteFn()
shouldSkip = false
}
}
// QUnit doesn't support focusing on a single module
override fun fsuite(name: String, suiteFn: () -> Unit) = withName(name, suiteFn)
override fun test(name: String, testFn: () -> Unit) {
if (shouldSkip) {
xtest(name, testFn)
}
else {
withName(name) {
QUnit.test(testName, wrapTest(testFn))
}
}
}
override fun xtest(name: String, testFn: () -> Unit) = withName(name) {
QUnit.skip(testName, wrapTest(testFn))
}
override fun ftest(name: String, testFn: () -> Unit) = withName(name) {
QUnit.only(testName, wrapTest(testFn))
}
private fun wrapTest(testFn: () -> Unit): (dynamic) -> Unit = { assert ->
if (js("typeof assert !== 'function'")) {
assertHook = { result, _, _, msgFn -> assert.ok(result, msgFn()) }
} else {
assertHook = { result, expected, actual, msgFn ->
val data = js("{}")
data.result = result
data.actual = actual
data.expected = expected
data.message = msgFn()
assert.pushResult(data)
}
}
testFn()
}
},
JASMINE {
override fun suite(name: String, suiteFn: () -> Unit) {
describe(name, suiteFn)
}
override fun xsuite(name: String, suiteFn: () -> Unit) {
xdescribe(name, suiteFn)
}
override fun fsuite(name: String, suiteFn: () -> Unit) {
fdescribe(name, suiteFn)
}
override fun test(name: String, testFn: () -> Unit) {
it(name, testFn)
}
override fun xtest(name: String, testFn: () -> Unit) {
xit(name, testFn)
}
override fun ftest(name: String, testFn: () -> Unit) {
fit(name, testFn)
}
},
MOCHA {
override fun suite(name: String, suiteFn: () -> Unit) {
describe(name, suiteFn)
}
override fun xsuite(name: String, suiteFn: () -> Unit) {
xdescribe(name, suiteFn)
}
override fun fsuite(name: String, suiteFn: () -> Unit) {
js("describe.only")(name, suiteFn)
}
override fun test(name: String, testFn: () -> Unit) {
it(name, testFn)
}
override fun xtest(name: String, testFn: () -> Unit) {
xit(name, testFn)
}
override fun ftest(name: String, testFn: () -> Unit) {
js("it.only")(name, testFn)
}
},
BARE {
override fun suite(name: String, suiteFn: () -> Unit) {
suiteFn()
}
override fun xsuite(name: String, suiteFn: () -> Unit) {
// Do nothing
}
override fun fsuite(name: String, suiteFn: () -> Unit) {
suiteFn()
}
override fun test(name: String, testFn: () -> Unit) {
testFn()
}
override fun xtest(name: String, testFn: () -> Unit) {
// Do nothing
}
override fun ftest(name: String, testFn: () -> Unit) {
testFn()
}
};
companion object {
val AUTO_DETECTED: FrameworkAdapter
get() = when {
js("typeof QUnit !== 'undefined'") -> QUNIT
js("typeof describe === 'function' && typeof it === 'function'") -> {
if (js("typeof xit === 'function'")) JASMINE else MOCHA
}
else -> BARE
}
val NAME_TO_ADAPTER = mapOf(
"qunit" to QUNIT,
"jasmine" to JASMINE,
"mocha" to MOCHA,
"auto" to AUTO_DETECTED)
}
}
internal var assertHook: (result: Boolean, expected: Any?, actual: Any?, () -> String?) -> Unit = { result, _, _, msgFun ->
if (js("typeof QUnit !== 'undefined'")) {
ok(result, msgFun())
}
}
/**
* The [QUnit](http://qunitjs.com/) API
*/
external object QUnit {
fun module(name: String, testFn: () -> Unit): Unit
fun test(name: String, testFn: (dynamic) -> Unit): Unit
fun skip(name: String, testFn: (dynamic) -> Unit): Unit
fun only(name: String, testFn: (dynamic) -> Unit): Unit
}
// Old Qunit API
external fun ok(result: Boolean, message: String?)
/**
* Jasmine/Mocha API
*/
external fun describe(name: String, fn: () -> Unit)
external fun xdescribe(name: String, fn: () -> Unit)
external fun fdescribe(name: String, fn: () -> Unit)
external fun it(name: String, fn: () -> Unit)
external fun xit(name: String, fn: () -> Unit)
external fun fit(name: String, fn: () -> Unit)

View File

@@ -16,6 +16,7 @@
package kotlin.test
import assertHook
import kotlin.reflect.KClass
/**
@@ -48,25 +49,64 @@ private val qunitAsserter = QUnitAsserter()
// TODO: make object in 1.2
class QUnitAsserter : Asserter {
private var e: Any? = undefined
private var a: Any? = undefined
override fun assertEquals(message: String?, expected: Any?, actual: Any?) {
e = expected
a = actual
super.assertEquals(message, expected, actual)
}
override fun assertNotEquals(message: String?, illegal: Any?, actual: Any?) {
e = illegal
a = actual
super.assertNotEquals(message, illegal, actual)
}
override fun assertNull(message: String?, actual: Any?) {
a = actual
super.assertNull(message, actual)
}
override fun assertNotNull(message: String?, actual: Any?) {
a = actual
super.assertNotNull(message, actual)
}
override fun assertTrue(lazyMessage: () -> String?, actual: Boolean) {
assertTrue(actual, lazyMessage())
if (!actual) {
failWithMessage(lazyMessage)
}
else {
invokeHook(true, lazyMessage)
}
}
override fun assertTrue(message: String?, actual: Boolean) {
QUnit.ok(actual, message)
if (!actual) failWithMessage(message)
assertTrue({ message }, actual)
}
override fun fail(message: String?): Nothing {
QUnit.ok(false, message)
failWithMessage(message)
failWithMessage { message }
}
private fun failWithMessage(message: String?): Nothing {
private fun failWithMessage(lazyMessage: () -> String?): Nothing {
val message = lazyMessage()
invokeHook(false) { message }
if (message == null)
throw AssertionError()
else
throw AssertionError(message)
}
}
private fun invokeHook(result: Boolean, lazyMessage: () -> String?) {
try {
assertHook(result, e, a, lazyMessage)
}
finally {
e = undefined
a = undefined
}
}
}

View File

@@ -1,8 +0,0 @@
package QUnit
/**
* The [QUnit](http://qunitjs.com/) API
*/
external fun ok(actual: Boolean, message: String?): Unit

View File

@@ -0,0 +1,8 @@
package kotlin.test
annotation class Test
annotation class Ignore
annotation class Only

View File

@@ -6,6 +6,7 @@ include ':kotlin-test:kotlin-test-common'
include ':kotlin-test:kotlin-test-jvm'
include ':kotlin-test:kotlin-test-junit'
include ':kotlin-test:kotlin-test-js'
include ':kotlin-test:kotlin-test-js:it'
include ':kotlin-stdlib-common'
include ':kotlin-stdlib'
include ':kotlin-stdlib-js'
@@ -57,6 +58,7 @@ project(':kotlin-test:kotlin-test-common').projectDir = "$rootDir/kotlin.test/co
project(':kotlin-test:kotlin-test-jvm').projectDir = "$rootDir/kotlin.test/jvm" as File
project(':kotlin-test:kotlin-test-junit').projectDir = "$rootDir/kotlin.test/junit" as File
project(':kotlin-test:kotlin-test-js').projectDir = "$rootDir/kotlin.test/js" as File
project(':kotlin-test:kotlin-test-js:it').projectDir = "$rootDir/kotlin.test/js/it" as File
project(':kotlin-stdlib-common').projectDir = "$rootDir/stdlib/common" as File
project(':kotlin-stdlib').projectDir = "$rootDir/stdlib" as File
project(':kotlin-stdlib-js').projectDir = "$rootDir/stdlib/js" as File