diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java index 4786976f9cd..0473b167572 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java @@ -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); diff --git a/js/js.translator/testData/test.js b/js/js.translator/testData/test.js index db6e1c03be0..2d28abc7e40 100644 --- a/js/js.translator/testData/test.js +++ b/js/js.translator/testData/test.js @@ -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()); } \ No newline at end of file diff --git a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt index de99f29e364..76e2d1ab331 100644 --- a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt +++ b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt @@ -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.") diff --git a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/DefaultAsserter.kt b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/DefaultAsserter.kt index b5cedf80a61..38b6c53267d 100644 --- a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/DefaultAsserter.kt +++ b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/DefaultAsserter.kt @@ -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() diff --git a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Utils.kt b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Utils.kt index d64a45c3fe8..c808b3d5d66 100644 --- a/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Utils.kt +++ b/libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Utils.kt @@ -1,4 +1,12 @@ package kotlin.test internal fun messagePrefix(message: String?) = if (message == null) "" else "$message. " -internal header fun lookupAsserter(): Asserter \ No newline at end of file +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 +} \ No newline at end of file diff --git a/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt b/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt index 735bbc0ee17..c177b72cee1 100644 --- a/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt +++ b/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt @@ -18,8 +18,12 @@ class BasicAssertionsTest { fun testAssertFailsWith() { assertFailsWith { throw IllegalStateException() } assertFailsWith { throw AssertionError() } + } - run { + @Test fun testAssertFailsWithFails() { + assertTrue(true) // at least one assertion required for qunit + + withDefaultAsserter run@ { try { assertFailsWith { throw IllegalArgumentException() } } @@ -29,7 +33,7 @@ class BasicAssertionsTest { throw AssertionError("Expected to fail") } - run { + withDefaultAsserter run@ { try { assertFailsWith { } } @@ -42,7 +46,7 @@ class BasicAssertionsTest { @Test fun testAssertEqualsFails() { - assertFailsWith { assertEquals(1, 2) } + checkFailedAssertion { assertEquals(1, 2) } } @Test @@ -53,16 +57,20 @@ class BasicAssertionsTest { @Test() fun testAssertTrueFails() { - assertFailsWith { assertTrue(false) } - assertFailsWith { assertTrue { false } } + checkFailedAssertion { assertTrue(false) } + checkFailedAssertion { assertTrue { false } } } @Test fun testAssertFalse() { assertFalse(false) assertFalse { false } - assertFailsWith { assertFalse(true) } - assertFailsWith { assertFalse { true } } + } + + @Test + fun testAssertFalseFails() { + checkFailedAssertion { assertFalse(true) } + checkFailedAssertion{ assertFalse { true } } } @Test @@ -72,7 +80,7 @@ class BasicAssertionsTest { @Test() fun testAssertFailsFails() { - assertFailsWith { assertFails { } } + checkFailedAssertion { assertFails { } } } @@ -83,7 +91,7 @@ class BasicAssertionsTest { @Test() fun testAssertNotEqualsFails() { - assertFailsWith { assertNotEquals(1, 1) } + checkFailedAssertion { assertNotEquals(1, 1) } } @Test @@ -93,7 +101,7 @@ class BasicAssertionsTest { @Test() fun testAssertNotNullFails() { - assertFailsWith { assertNotNull(null) } + checkFailedAssertion { assertNotNull(null) } } @Test @@ -103,7 +111,7 @@ class BasicAssertionsTest { @Test fun testAssertNotNullLambdaFails() { - assertFailsWith { + checkFailedAssertion { val value: String? = null assertNotNull(value) { it.substring(0, 0) @@ -118,18 +126,37 @@ class BasicAssertionsTest { @Test fun testAssertNullFails() { - assertFailsWith { assertNull("") } + checkFailedAssertion { assertNull("") } } @Test() fun testFail() { - assertFailsWith { fail("should fail") } + checkFailedAssertion { fail("should fail") } } @Test fun testExpect() { expect(1) { 1 } - assertFailsWith { expect(1) { 2 } } } -} \ No newline at end of file + @Test + fun testExpectFails() { + checkFailedAssertion { expect(1) { 2 } } + } +} + + +private fun checkFailedAssertion(assertion: () -> Unit) { + assertFailsWith { withDefaultAsserter(assertion) } +} + +@Suppress("INVISIBLE_MEMBER") +private fun withDefaultAsserter(block: () -> Unit) { + val current = overrideAsserter(DefaultAsserter()) + try { + block() + } + finally { + overrideAsserter(current) + } +} diff --git a/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt b/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt index f0258e508be..b8f2732fa6b 100644 --- a/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt +++ b/libraries/kotlin.test/js/src/main/kotlin/JsImpl.kt @@ -38,13 +38,14 @@ inline fun 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) { diff --git a/libraries/kotlin.test/js/src/test/karma/karma.conf.js b/libraries/kotlin.test/js/src/test/karma/karma.conf.js deleted file mode 100644 index 8c41577ab37..00000000000 --- a/libraries/kotlin.test/js/src/test/karma/karma.conf.js +++ /dev/null @@ -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'] - } - } - ) -}; \ No newline at end of file diff --git a/libraries/kotlin.test/js/src/test/karma/package.json b/libraries/kotlin.test/js/src/test/karma/package.json deleted file mode 100644 index 0e19fa9b39a..00000000000 --- a/libraries/kotlin.test/js/src/test/karma/package.json +++ /dev/null @@ -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" - } -} \ No newline at end of file diff --git a/libraries/kotlin.test/jvm/src/main/kotlin/AsserterLookup.kt b/libraries/kotlin.test/jvm/src/main/kotlin/AsserterLookup.kt index cd6af8a599b..3a7573e6871 100644 --- a/libraries/kotlin.test/jvm/src/main/kotlin/AsserterLookup.kt +++ b/libraries/kotlin.test/jvm/src/main/kotlin/AsserterLookup.kt @@ -11,6 +11,8 @@ private val contributors = ArrayList() 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() { diff --git a/libraries/stdlib/js/build.gradle b/libraries/stdlib/js/build.gradle index bb81969ad53..1372f56bcb4 100644 --- a/libraries/stdlib/js/build.gradle +++ b/libraries/stdlib/js/build.gradle @@ -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 diff --git a/libraries/stdlib/js/web/index.html b/libraries/stdlib/js/web/index.html index e96a0a2705f..b701fcf925c 100644 --- a/libraries/stdlib/js/web/index.html +++ b/libraries/stdlib/js/web/index.html @@ -12,6 +12,7 @@ +