Run kotlin-test-js tests with kotlin-stdlib-js

Rework kotlin-test common tests to make them runnable with qunit.
Change the way how asserter is overridden in js box tests.
Minor: remove unneeded test configs
This commit is contained in:
Ilya Gorbunov
2017-04-19 07:40:22 +03:00
parent f009e0c665
commit ec8ead754f
12 changed files with 80 additions and 118 deletions
@@ -189,9 +189,8 @@ public final class RhinoUtils {
runFileWithRhino(DIST_DIR_JS_PATH + "../../js/js.translator/testData/rhino-polyfills.js", context, scope);
runFileWithRhino(DIST_DIR_JS_PATH + "kotlin.js", context, scope);
runFileWithRhino(DIST_DIR_JS_PATH + "kotlin-test.js", context, scope);
context.evaluateString(scope,
"this['kotlin-test'].kotlin.test._asserter = new this['kotlin-test'].kotlin.test.DefaultAsserter();",
"this['kotlin-test'].kotlin.test.overrideAsserter_wbnzx$(new this['kotlin-test'].kotlin.test.DefaultAsserter());",
"change asserter to DefaultAsserter", 1, null);
//runFileWithRhino(pathToTestFilesRoot() + "jshint.js", context, scope);
+3 -30
View File
@@ -20,7 +20,7 @@ var kotlinJsTestLocation = process.env.KOTLIN_JS_TEST_LOCATION || __dirname + "/
var kotlin = require("kotlin");
var kotlinTest = require(kotlinJsTestLocation);
supplyAsserter(kotlin, kotlinTest);
supplyAsserter(kotlinTest);
var requireFromString = require('require-from-string');
var baseDir = "out";
@@ -102,33 +102,6 @@ function runTest(testRunner, location) {
});
}
function supplyAsserter(kotlin, kotlinTest) {
function AsserterClass() {
}
AsserterClass.prototype.assertTrue_o10pc4$ = function(lazyMessage, actual) {
kotlinTest.kotlin.test.assertTrue_ifx8ge$(actual, lazyMessage());
};
AsserterClass.prototype.assertTrue_4mavae$ = function(message, actual) {
if (!actual) {
this.failWithMessage(message);
}
};
AsserterClass.prototype.assertNotNull_67rc9h$ = kotlinTest.kotlin.test.Asserter.prototype.assertNotNull_67rc9h$;
AsserterClass.prototype.assertEquals_lzc6tz$ = kotlinTest.kotlin.test.Asserter.prototype.assertEquals_lzc6tz$;
AsserterClass.prototype.fail_pdl1vj$ = function(message) {
this.failWithMessage(message);
};
AsserterClass.prototype.failWithMessage = function(message) {
if (message == null) {
throw new Kotlin.AssertionError();
}
else {
throw new Kotlin.AssertionError(message);
}
};
AsserterClass.$metadata$ = {
type: kotlin.Kind.CLASS,
baseClasses: [kotlinTest.kotlin.test.Asserter]
};
kotlinTest.kotlin.test._asserter = new AsserterClass();
function supplyAsserter(kotlinTest) {
kotlinTest.kotlin.test.overrideAsserter_wbnzx$(new kotlinTest.kotlin.test.DefaultAsserter());
}
@@ -25,8 +25,14 @@ package kotlin.test
import kotlin.internal.*
/**
* Current adapter providing assertion implementations
*/
val asserter: Asserter
get() = lookupAsserter()
get() = _asserter ?: lookupAsserter()
/** Used to override current asserter internally */
internal var _asserter: Asserter? = null
/** Asserts that the given [block] returns `true`. */
fun assertTrue(message: String? = null, block: () -> Boolean): Unit = assertTrue(block(), message)
@@ -97,6 +103,7 @@ fun assertFails(message: String?, block: () -> Unit): Throwable {
try {
block()
} catch (e: Throwable) {
assertEquals(e.message, e.message) // success path assertion for qunit
return e
}
asserter.fail(messagePrefix(message) + "Expected an exception to be thrown, but was completed successfully.")
@@ -3,8 +3,8 @@ package kotlin.test
/**
* Default [Asserter] implementation to avoid dependency on JUnit or TestNG.
*/
class DefaultAsserter() : Asserter {
// TODO: make object in 1.2
class DefaultAsserter : Asserter {
override fun fail(message: String?): Nothing {
if (message == null)
throw AssertionError()
@@ -1,4 +1,12 @@
package kotlin.test
internal fun messagePrefix(message: String?) = if (message == null) "" else "$message. "
internal header fun lookupAsserter(): Asserter
internal header fun lookupAsserter(): Asserter
@PublishedApi // required to get stable name as it's called from box tests
internal fun overrideAsserter(value: Asserter?): Asserter? {
// TODO: Replace with return _asserter.also { _asserter = value } after KT-17540 is fixed
val previous = _asserter
_asserter = value
return previous
}
@@ -18,8 +18,12 @@ class BasicAssertionsTest {
fun testAssertFailsWith() {
assertFailsWith<IllegalStateException> { throw IllegalStateException() }
assertFailsWith<AssertionError> { throw AssertionError() }
}
run {
@Test fun testAssertFailsWithFails() {
assertTrue(true) // at least one assertion required for qunit
withDefaultAsserter run@ {
try {
assertFailsWith<IllegalStateException> { throw IllegalArgumentException() }
}
@@ -29,7 +33,7 @@ class BasicAssertionsTest {
throw AssertionError("Expected to fail")
}
run {
withDefaultAsserter run@ {
try {
assertFailsWith<IllegalStateException> { }
}
@@ -42,7 +46,7 @@ class BasicAssertionsTest {
@Test
fun testAssertEqualsFails() {
assertFailsWith<AssertionError> { assertEquals(1, 2) }
checkFailedAssertion { assertEquals(1, 2) }
}
@Test
@@ -53,16 +57,20 @@ class BasicAssertionsTest {
@Test()
fun testAssertTrueFails() {
assertFailsWith<AssertionError> { assertTrue(false) }
assertFailsWith<AssertionError> { assertTrue { false } }
checkFailedAssertion { assertTrue(false) }
checkFailedAssertion { assertTrue { false } }
}
@Test
fun testAssertFalse() {
assertFalse(false)
assertFalse { false }
assertFailsWith<AssertionError> { assertFalse(true) }
assertFailsWith<AssertionError> { assertFalse { true } }
}
@Test
fun testAssertFalseFails() {
checkFailedAssertion { assertFalse(true) }
checkFailedAssertion{ assertFalse { true } }
}
@Test
@@ -72,7 +80,7 @@ class BasicAssertionsTest {
@Test()
fun testAssertFailsFails() {
assertFailsWith<AssertionError> { assertFails { } }
checkFailedAssertion { assertFails { } }
}
@@ -83,7 +91,7 @@ class BasicAssertionsTest {
@Test()
fun testAssertNotEqualsFails() {
assertFailsWith<AssertionError> { assertNotEquals(1, 1) }
checkFailedAssertion { assertNotEquals(1, 1) }
}
@Test
@@ -93,7 +101,7 @@ class BasicAssertionsTest {
@Test()
fun testAssertNotNullFails() {
assertFailsWith<AssertionError> { assertNotNull(null) }
checkFailedAssertion { assertNotNull(null) }
}
@Test
@@ -103,7 +111,7 @@ class BasicAssertionsTest {
@Test
fun testAssertNotNullLambdaFails() {
assertFailsWith<AssertionError> {
checkFailedAssertion {
val value: String? = null
assertNotNull(value) {
it.substring(0, 0)
@@ -118,18 +126,37 @@ class BasicAssertionsTest {
@Test
fun testAssertNullFails() {
assertFailsWith<AssertionError> { assertNull("") }
checkFailedAssertion { assertNull("") }
}
@Test()
fun testFail() {
assertFailsWith<AssertionError> { fail("should fail") }
checkFailedAssertion { fail("should fail") }
}
@Test
fun testExpect() {
expect(1) { 1 }
assertFailsWith<AssertionError> { expect(1) { 2 } }
}
}
@Test
fun testExpectFails() {
checkFailedAssertion { expect(1) { 2 } }
}
}
private fun checkFailedAssertion(assertion: () -> Unit) {
assertFailsWith<AssertionError> { withDefaultAsserter(assertion) }
}
@Suppress("INVISIBLE_MEMBER")
private fun withDefaultAsserter(block: () -> Unit) {
val current = overrideAsserter(DefaultAsserter())
try {
block()
}
finally {
overrideAsserter(current)
}
}
@@ -38,13 +38,14 @@ inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noin
}
*/
var _asserter: Asserter = QUnitAsserter()
/**
* Provides the JS implementation of asserter using [QUnit](http://QUnitjs.com/)
*/
internal impl fun lookupAsserter(): Asserter = _asserter
internal impl fun lookupAsserter(): Asserter = qunitAsserter
private val qunitAsserter = QUnitAsserter()
// TODO: make object in 1.2
class QUnitAsserter : Asserter {
override fun assertTrue(lazyMessage: () -> String?, actual: Boolean) {
@@ -1,47 +0,0 @@
/*
* Copyright 2000-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.
*/
module.exports = function (config) {
config.set({
frameworks: ['qunit'],
reporters: ['progress', 'junit'],
files: [
'../../../target/test-js/kotlin.js',
'../../../target/test-js/*.js',
'../../../target/classes/*.js'
],
exclude: [],
port: 9876,
runnerPort: 9100,
colors: true,
autoWatch: false,
browsers: [
'PhantomJS'
],
captureTimeout: 5000,
//singleRun: false,
singleRun: true,
reportSlowerThan: 500,
junitReporter: {
outputFile: '../../../target/reports/test-results.xml',
suite: ''
},
preprocessors: {
'**/*.js': ['sourcemap']
}
}
)
};
@@ -1,14 +0,0 @@
{
"name": "karma-tests",
"version": "0.0.0",
"devDependencies": {
"karma": "*",
"qunitjs": "*",
"karma-qunit": "*",
"karma-junit-reporter": "~0.2",
"karma-sourcemap-loader": "~0.3",
"karma-teamcity-reporter": "*",
"karma-phantomjs-launcher": "*",
"phantomjs": "1.9.13"
}
}
@@ -11,6 +11,8 @@ private val contributors = ArrayList<AsserterContributor>()
internal impl fun lookupAsserter(): Asserter = lookup()
private val defaultAsserter = DefaultAsserter()
internal fun lookup(): Asserter {
initContributorsIfNeeded()
@@ -21,7 +23,7 @@ internal fun lookup(): Asserter {
}
}
return DefaultAsserter()
return defaultAsserter
}
private fun initContributors() {
+7 -2
View File
@@ -21,6 +21,8 @@ def jsSrcJsDir = "${jsSrcDir}/js"
def jsOutputFile = "${buildDir}/classes/kotlin.js"
def jsTestOutputFile = "${buildDir}/classes/test/kotlin-stdlib-js_test.js"
def kotlinTestJsOutputFile = "${project(':kotlin-test:kotlin-test-js').buildDir}/classes/main/kotlin-test.js"
def kotlinTestJsTestOutputFile = "${project(':kotlin-test:kotlin-test-js').buildDir}/classes/test/kotlin-test-js_test.js"
sourceSets {
builtins {
@@ -268,10 +270,13 @@ karma {
sourceFiles = []
testBases = ['']
testFiles = [jsTestOutputFile]
testFiles = [jsTestOutputFile, kotlinTestJsTestOutputFile]
}
}
karmaGenerateConfig.outputs.upToDateWhen { false }
karmaRun.dependsOn testClasses
karmaRun {
dependsOn testClasses
dependsOn tasks.getByPath(':kotlin-test:kotlin-test-js:testClasses')
}
clean.dependsOn karmaClean
+1
View File
@@ -12,6 +12,7 @@
<script src="../build/classes/kotlin.js"></script>
<script src="../../../kotlin.test/js/build/classes/main/kotlin-test.js"></script>
<script src="../build/classes/test/kotlin-stdlib-js_test.js"></script>
<script src="../../../kotlin.test/js/build/classes/test/kotlin-test-js_test.js"></script>
</head>
<body>
<div id="qunit"></div>