From 149ce70ce2f2a3bfca9696b0f5850000a27a24c1 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Mon, 23 Nov 2015 16:00:31 +0300 Subject: [PATCH] Move testing from stdlib to js.stdlib --- jslib_files.xml | 4 + .../kotlin/jvm/annotations.kt | 4 +- .../src/test/kotlin.jvm}/TestJVMTest.kt | 22 ++- .../shared/src/test/kotlin.jvm/UtilsJVM.kt | 7 + .../src/test/kotlin/kotlin/test/tests/util.kt | 2 - libraries/stdlib/src/kotlin/test/Test.kt | 161 ------------------ libraries/stdlib/src/kotlin/test/TestJVM.kt | 81 --------- 7 files changed, 30 insertions(+), 251 deletions(-) rename libraries/kotlin.test/shared/src/main/{kotlin => kotlin.js}/kotlin/jvm/annotations.kt (89%) rename libraries/{stdlib/test/test => kotlin.test/shared/src/test/kotlin.jvm}/TestJVMTest.kt (83%) create mode 100644 libraries/kotlin.test/shared/src/test/kotlin.jvm/UtilsJVM.kt delete mode 100644 libraries/stdlib/src/kotlin/test/Test.kt delete mode 100644 libraries/stdlib/src/kotlin/test/TestJVM.kt diff --git a/jslib_files.xml b/jslib_files.xml index 641749da406..6d4d47e7ec2 100644 --- a/jslib_files.xml +++ b/jslib_files.xml @@ -20,6 +20,10 @@ + + + + diff --git a/libraries/kotlin.test/shared/src/main/kotlin/kotlin/jvm/annotations.kt b/libraries/kotlin.test/shared/src/main/kotlin.js/kotlin/jvm/annotations.kt similarity index 89% rename from libraries/kotlin.test/shared/src/main/kotlin/kotlin/jvm/annotations.kt rename to libraries/kotlin.test/shared/src/main/kotlin.js/kotlin/jvm/annotations.kt index bc99c9c8c36..c9c23e59564 100644 --- a/libraries/kotlin.test/shared/src/main/kotlin/kotlin/jvm/annotations.kt +++ b/libraries/kotlin.test/shared/src/main/kotlin.js/kotlin/jvm/annotations.kt @@ -11,7 +11,7 @@ package kotlin.jvm @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.FILE) @Retention(AnnotationRetention.BINARY) @MustBeDocumented -public annotation class JvmName(public val name: String) +internal annotation class JvmName(public val name: String) /** * Instructs the Kotlin compiler to generate a multifile class with top-level functions and properties declared in this file as one of its parts. @@ -20,6 +20,6 @@ public annotation class JvmName(public val name: String) @Target(AnnotationTarget.FILE) @Retention(AnnotationRetention.SOURCE) @MustBeDocumented -public annotation class JvmMultifileClass +internal annotation class JvmMultifileClass diff --git a/libraries/stdlib/test/test/TestJVMTest.kt b/libraries/kotlin.test/shared/src/test/kotlin.jvm/TestJVMTest.kt similarity index 83% rename from libraries/stdlib/test/test/TestJVMTest.kt rename to libraries/kotlin.test/shared/src/test/kotlin.jvm/TestJVMTest.kt index ce3678e1da7..c94be43873b 100644 --- a/libraries/stdlib/test/test/TestJVMTest.kt +++ b/libraries/kotlin.test/shared/src/test/kotlin.jvm/TestJVMTest.kt @@ -1,9 +1,24 @@ -package test.test +/* + * 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 kotlin.test.tests import kotlin.test.* import org.junit.Test - class TestJVMTest { private fun expectAssertion(checkAssertion: (String?) -> Unit, block: () -> Unit) { @@ -90,7 +105,4 @@ class TestJVMTest { assertTrue(msg.contains(actual.toString())) }, { assertNull(actual, message)}) } - - - } \ No newline at end of file diff --git a/libraries/kotlin.test/shared/src/test/kotlin.jvm/UtilsJVM.kt b/libraries/kotlin.test/shared/src/test/kotlin.jvm/UtilsJVM.kt new file mode 100644 index 00000000000..a8998de9adf --- /dev/null +++ b/libraries/kotlin.test/shared/src/test/kotlin.jvm/UtilsJVM.kt @@ -0,0 +1,7 @@ +package kotlin.test.tests + +@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") +internal operator fun String.contains(sub: String) = (this as java.lang.String).contains(sub) + +@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") +internal fun String.startsWith(other: String) = (this as java.lang.String).startsWith(other) diff --git a/libraries/kotlin.test/shared/src/test/kotlin/kotlin/test/tests/util.kt b/libraries/kotlin.test/shared/src/test/kotlin/kotlin/test/tests/util.kt index 80f09691e6a..0eb8052436d 100644 --- a/libraries/kotlin.test/shared/src/test/kotlin/kotlin/test/tests/util.kt +++ b/libraries/kotlin.test/shared/src/test/kotlin/kotlin/test/tests/util.kt @@ -20,5 +20,3 @@ internal fun setOf(vararg elements: T): Set { return result } -internal operator fun String.contains(sub: String) = (this as java.lang.String).contains(sub) - diff --git a/libraries/stdlib/src/kotlin/test/Test.kt b/libraries/stdlib/src/kotlin/test/Test.kt deleted file mode 100644 index 864f1165632..00000000000 --- a/libraries/stdlib/src/kotlin/test/Test.kt +++ /dev/null @@ -1,161 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("TestAssertionsKt") -/** - * A number of helper methods for writing unit tests. - */ -package kotlin.test - - -/** Asserts that the given [block] returns `false`. */ -@Deprecated("Use assertFalse instead.", ReplaceWith("assertFalse(message, block)")) -public fun assertNot(message: String, block: () -> Boolean): Unit = assertFalse(message, block) - -/** Asserts that the given [block] returns `false`. */ -@Deprecated("Use assertFalse instead.", ReplaceWith("assertFalse(null, block)")) -public fun assertNot(block: () -> Boolean): Unit = assertFalse(block = block) - -/** Asserts that the given [block] returns `true`. */ -public fun assertTrue(message: String? = null, block: () -> Boolean): Unit = assertTrue(block(), message) - -/** Asserts that the expression is `true` with an optional [message]. */ -public fun assertTrue(actual: Boolean, message: String? = null) { - return asserter.assertTrue(message ?: "Expected value to be true.", actual) -} - -/** Asserts that the given [block] returns `false`. */ -public fun assertFalse(message: String? = null, block: () -> Boolean): Unit = assertFalse(block(), message) - -/** Asserts that the expression is `false` with an optional [message]. */ -public fun assertFalse(actual: Boolean, message: String? = null) { - return asserter.assertTrue(message ?: "Expected value to be false.", !actual) -} - -/** Asserts that the [expected] value is equal to the [actual] value, with an optional [message]. */ -public fun <@kotlin.internal.OnlyInputTypes T> assertEquals(expected: T, actual: T, message: String? = null) { - asserter.assertEquals(message, expected, actual) -} - -/** Asserts that the [actual] value is not equal to the illegal value, with an optional [message]. */ -public fun <@kotlin.internal.OnlyInputTypes T> assertNotEquals(illegal: T, actual: T, message: String? = null) { - asserter.assertNotEquals(message, illegal, actual) -} - -/** Asserts that the [actual] value is not `null`, with an optional [message]. */ -public fun assertNotNull(actual: T?, message: String? = null): T { - asserter.assertNotNull(message, actual) - return actual!! -} - -/** Asserts that the [actual] value is not `null`, with an optional [message] and a function [block] to process the not-null value. */ -public fun assertNotNull(actual: T?, message: String? = null, block: (T) -> R) { - asserter.assertNotNull(message, actual) - if (actual != null) { - block(actual) - } -} - -/** Asserts that the [actual] value is `null`, with an optional [message]. */ -public fun assertNull(actual: Any?, message: String? = null) { - asserter.assertNull(message, actual) -} - -/** Marks a test as having failed if this point in the execution path is reached, with an optional [message]. */ -public fun fail(message: String? = null) { - asserter.fail(message) -} - -/** Asserts that given function [block] returns the given [expected] value. */ -public fun <@kotlin.internal.OnlyInputTypes T> expect(expected: T, block: () -> T) { - assertEquals(expected, block()) -} - -/** Asserts that given function [block] returns the given [expected] value and use the given [message] if it fails. */ -public fun <@kotlin.internal.OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) { - assertEquals(expected, block(), message) -} - -@Deprecated("Use assertFails instead.", ReplaceWith("assertFails(block)")) -public fun fails(block: () -> Unit): Throwable? = assertFails(block) - -/** Asserts that given function [block] fails by throwing an exception. */ -public fun assertFails(block: () -> Unit): Throwable? { - var thrown: Throwable? = null - try { - block() - } catch (e: Throwable) { - thrown = e - } - if (thrown == null) - asserter.fail("Expected an exception to be thrown") - return thrown -} - -/** - * Abstracts the logic for performing assertions. Specific implementations of [Asserter] can use JUnit - * or TestNG assertion facilities. - */ -public interface Asserter { - /** - * Fails the current test with the specified message. - * - * @param message the message to report. - */ - public fun fail(message: String?): Unit - - /** - * Asserts that the specified value is `true`. - * - * @param lazyMessage the function to return a message to report if the assertion fails. - */ - public fun assertTrue(lazyMessage: () -> String?, actual: Boolean): Unit { - if (!actual) { - fail(lazyMessage()) - } - } - - /** - * Asserts that the specified value is `true`. - * - * @param message the message to report if the assertion fails. - */ - public fun assertTrue(message: String?, actual: Boolean): Unit { - assertTrue({message}, actual) - } - - /** - * Asserts that the specified values are equal. - * - * @param message the message to report if the assertion fails. - */ - public fun assertEquals(message: String?, expected: Any?, actual: Any?): Unit { - assertTrue({ (message?.let { "$it. " } ?: "") + "Expected <$expected>, actual <$actual>." }, actual == expected) - } - - /** - * Asserts that the specified values are not equal. - * - * @param message the message to report if the assertion fails. - */ - public fun assertNotEquals(message: String?, illegal: Any?, actual: Any?): Unit { - assertTrue({ (message?.let { "$it. " } ?: "") + "Illegal value: <$actual>." }, actual != illegal) - } - - /** - * Asserts that the specified value is `null`. - * - * @param message the message to report if the assertion fails. - */ - public fun assertNull(message: String?, actual: Any?): Unit { - assertTrue({ (message?.let { "$it. " } ?: "") + "Expected value to be null, but was: <$actual>." }, actual == null) - } - - /** - * Asserts that the specified value is not `null`. - * - * @param message the message to report if the assertion fails. - */ - public fun assertNotNull(message: String?, actual: Any?): Unit { - assertTrue({ (message?.let { "$it. " } ?: "") + "Expected value to be not null." }, actual != null) - } - -} diff --git a/libraries/stdlib/src/kotlin/test/TestJVM.kt b/libraries/stdlib/src/kotlin/test/TestJVM.kt deleted file mode 100644 index 6957598bfc2..00000000000 --- a/libraries/stdlib/src/kotlin/test/TestJVM.kt +++ /dev/null @@ -1,81 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("TestAssertionsKt") -package kotlin.test - -import java.util.ServiceLoader -import kotlin.reflect.KClass - -@Deprecated("Use assertFailsWith instead.", ReplaceWith("assertFailsWith(exceptionClass, block)")) -public fun failsWith(exceptionClass: Class, block: ()-> Any): T = assertFailsWith(exceptionClass, { block() }) - -/** Asserts that a [block] fails with a specific exception being thrown. */ -public fun assertFailsWith(exceptionClass: Class, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block) - -/** Asserts that a [block] fails with a specific exception being thrown. */ -public fun assertFailsWith(exceptionClass: Class, message: String?, block: () -> Unit): T { - try { - block() - } - catch (e: Throwable) { - if (exceptionClass.isInstance(e)) { - return e as T - } - asserter.fail((message?.let { "$it. "} ?: "") + "Expected an exception of type $exceptionClass to be thrown, but was $e") - throw e - } - val msg = message?.let { "$it. " } ?: "" - asserter.fail(msg + "Expected an exception of type $exceptionClass to be thrown, but was completed successfully.") - throw IllegalStateException(msg + "Should have failed.") -} - -/** Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown. */ -public fun assertFailsWith(exceptionClass: KClass, message: String? = null, block: () -> Unit): T = assertFailsWith(exceptionClass.java, message, block) - -/** Asserts that a [block] fails with a specific exception of type [T] being thrown. - * Since inline method doesn't allow to trace where it was invoked, it is required to pass a [message] to distinguish this method call from others. - */ -public inline fun assertFailsWith(message: String, noinline block: () -> Unit): T = assertFailsWith(T::class.java, message, block) - - -/** - * Comments out a [block] of test code until it is implemented while keeping a link to the code - * to implement in your unit test output - */ -public inline fun todo(block: ()-> Any) { - println("TODO at " + (Exception() as java.lang.Throwable).getStackTrace()?.get(1)) -} - -private var _asserter: Asserter? = null - -/** - * The active implementation of [Asserter]. An implementation of [Asserter] can be provided - * using the [Java service loader](http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html) mechanism. - */ -public var asserter: Asserter - get() { - if (_asserter == null) { - val klass = Asserter::class.java - val loader = ServiceLoader.load(klass) - _asserter = loader.firstOrNull { it != null } ?: DefaultAsserter() - //debug("using asserter $_asserter") - } - return _asserter!! - } - - set(value) { - _asserter = value - } - - -/** - * Default [Asserter] implementation to avoid dependency on JUnit or TestNG. - */ -private class DefaultAsserter() : Asserter { - - public override fun fail(message : String?) { - if (message == null) - throw AssertionError() - else - throw AssertionError(message) - } -} \ No newline at end of file