Common kotlin.test.

Remove duplicate code and add actuals.
Unify Before and AfterTest
Add kotlin-test-annotations-common and kotlin-test-common as dependencies.
This commit is contained in:
Pavel Punegov
2018-12-07 16:00:26 +03:00
committed by Pavel Punegov
parent 10b76eacf0
commit f5cc8a4d1e
6 changed files with 27 additions and 272 deletions
+14 -3
View File
@@ -102,6 +102,8 @@ configurations {
kotlin_reflect_jar
kotlin_script_runtime_jar
kotlin_common_stdlib_src
kotlin_test_common_src
kotlin_test_annotations_common_src
trove4j_jar
cli_bcRuntime {
@@ -121,6 +123,8 @@ dependencies {
kotlin_reflect_jar "$kotlinReflectModule@jar"
kotlin_script_runtime_jar "$kotlinScriptRuntimeModule@jar"
kotlin_common_stdlib_src kotlinCommonStdlibModule
kotlin_test_common_src kotlinTestCommonModule
kotlin_test_annotations_common_src kotlinTestAnnotationsCommonModule
compilerCompile "com.google.protobuf:protobuf-java:${protobufVersion}"
@@ -151,9 +155,16 @@ task start(dependsOn: "${hostName}Start")
def commonSrc = file('build/stdlib')
task unzipStdlibSources(type: Copy) {
from (zipTree(configurations.kotlin_common_stdlib_src.singleFile)) {
include 'generated/**/*.kt'
include 'kotlin/**/*.kt'
def jarFiles = configurations.kotlin_common_stdlib_src.files +
configurations.kotlin_test_common_src.files +
configurations.kotlin_test_annotations_common_src.files
jarFiles.forEach { jarFile ->
from(zipTree(jarFile)) {
include 'generated/**/*.kt'
include 'kotlin/**/*.kt'
include 'kotlin.test/*.kt'
}
}
destinationDir commonSrc
}
+2
View File
@@ -46,6 +46,8 @@ ext {
kotlinCommonStdlibModule="org.jetbrains.kotlin:kotlin-stdlib-common:${kotlinVersion}:sources"
kotlinCompilerModule="org.jetbrains.kotlin:kotlin-compiler:${kotlinVersion}"
kotlinStdLibModule="org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"
kotlinTestCommonModule="org.jetbrains.kotlin:kotlin-test-common:${kotlinVersion}:sources"
kotlinTestAnnotationsCommonModule="org.jetbrains.kotlin:kotlin-test-annotations-common:${kotlinVersion}:sources"
kotlinReflectModule="org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
kotlinScriptRuntimeModule="org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}"
@@ -9,7 +9,7 @@ package kotlin.test
* Marks a function as a test.
*/
@Target(AnnotationTarget.FUNCTION)
public annotation class Test
public actual annotation class Test
/**
* Marks a function to be executed before a suite. Not supported in Kotlin/Common.
@@ -27,20 +27,21 @@ public annotation class AfterClass
* Marks a function to be executed before a test.
*/
@Target(AnnotationTarget.FUNCTION)
public annotation class BeforeEach
public actual annotation class BeforeTest
/**
* Marks a function to be executed after a test.
*/
@Target(AnnotationTarget.FUNCTION)
public annotation class AfterEach
public actual annotation class AfterTest
/**
* Marks a test or a suite as ignored.
*/
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
public annotation class Ignore
public actual annotation class Ignore
public typealias AfterTest = AfterEach
public typealias BeforeTest = BeforeEach
@Deprecated("AfterEach should be replaced with AfterTest to unify usage of kotlin.test")
public typealias AfterEach = AfterTest
@Deprecated("BeforeEach should be replaced with BeforeTest to unify usage of kotlin.test")
public typealias BeforeEach = BeforeTest
@@ -10,148 +10,15 @@
package kotlin.test
import kotlin.contracts.*
import kotlin.internal.InlineOnly
import kotlin.internal.OnlyInputTypes
import kotlin.reflect.KClass
/**
* Current adapter providing assertion implementations
*/
private val asserter: Asserter
get() = DefaultAsserter
/** 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) {
contract { returns() implies actual }
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) {
contract { returns() implies (!actual) }
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 <@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 <@OnlyInputTypes T> assertNotEquals(illegal: T, actual: T, message: String? = null) {
asserter.assertNotEquals(message, illegal, actual)
}
/** Asserts that [expected] is the same instance as [actual], with an optional [message]. */
public fun <@OnlyInputTypes T> assertSame(expected: T, actual: T, message: String? = null) {
asserter.assertSame(message, expected, actual)
}
/** Asserts that [actual] is not the same instance as [illegal], with an optional [message]. */
public fun <@OnlyInputTypes T> assertNotSame(illegal: T, actual: T, message: String? = null) {
asserter.assertNotSame(message, illegal, actual)
}
/** Asserts that the [actual] value is not `null`, with an optional [message]. */
public fun <T : Any> assertNotNull(actual: T?, message: String? = null): T {
contract { returns() implies (actual != null) }
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 <T: Any, R> assertNotNull(actual: T?, message: String? = null, block: (T) -> R) {
contract { returns() implies (actual != null) }
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): Nothing {
asserter.fail(message)
}
/** Asserts that given function [block] returns the given [expected] value. */
public fun <@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 <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
assertEquals(expected, block(), message)
}
/**
* Asserts that given function [block] fails by throwing an exception.
*
* @return An exception that was expected to be thrown and was successfully caught.
* The returned exception can be inspected further, for example by asserting its property values.
*/
public fun assertFails(block: () -> Unit): Throwable = assertFails(null, block)
/**
* Asserts that given function [block] fails by throwing an exception.
*
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
*
* @return An exception that was expected to be thrown and was successfully caught.
* The returned exception can be inspected further, for example by asserting its property values.
*/
@SinceKotlin("1.1")
public 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.")
}
/** Asserts that a [block] fails with a specific exception of type [T] being thrown.
*
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
*
* @return An exception of the expected exception type [T] that successfully caught.
* The returned exception can be inspected further, for example by asserting its property values.
*/
@InlineOnly
public inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit) : T =
assertFailsWith(T::class, message, block)
/**
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown.
*
* @return An exception of the expected exception type [T] that successfully caught.
* The returned exception can be inspected further, for example by asserting its property values.
*/
public fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Unit): T =
assertFailsWith(exceptionClass, null, block)
// From AssertionsH.kt
/**
* Takes the given [block] of test code and _doesn't_ execute it.
*
* This keeps the code under test referenced, but doesn't actually test it until it is implemented.
*/
@Suppress("UNUSED_PARAMETER")
public inline fun todo(block: () -> Unit) {
public actual inline fun todo(block: () -> Unit) {
println("TODO")
}
@@ -163,7 +30,7 @@ public inline fun todo(block: () -> Unit) {
* @return An exception of the expected exception type [T] that successfully caught.
* The returned exception can be inspected further, for example by asserting its property values.
*/
public fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T {
public actual fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T {
try {
block()
} catch (e: Throwable) {
@@ -181,103 +48,4 @@ public fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: S
asserter.fail(msg + "Expected an exception of ${exceptionClass.qualifiedName} to be thrown, but was completed successfully.")
}
// From Assertions.kt
/**
* Abstracts the logic for performing assertions.
*/
public interface Asserter {
/**
* Fails the current test with the specified message.
*
* @param message the message to report.
*/
fun fail(message: String?): Nothing
/**
* Asserts that the specified value is `true`.
*
* @param lazyMessage the function to return a message to report if the assertion fails.
*/
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.
*/
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.
*/
fun assertEquals(message: String?, expected: Any?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "Expected <$expected>, actual <$actual>." }, actual == expected)
}
/**
* Asserts that the specified values are not equal.
*
* @param message the message to report if the assertion fails.
*/
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "Illegal value: <$actual>." }, actual != illegal)
}
/**
* Asserts that the specified values are the same instance.
*
* @param message the message to report if the assertion fails.
*/
fun assertSame(message: String?, expected: Any?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "Expected <$expected>, actual <$actual> is not same." }, actual === expected)
}
/**
* Asserts that the specified values are not the same instance.
*
* @param message the message to report if the assertion fails.
*/
fun assertNotSame(message: String?, illegal: Any?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "Expected not same as <$actual>." }, actual !== illegal)
}
/**
* Asserts that the specified value is `null`.
*
* @param message the message to report if the assertion fails.
*/
fun assertNull(message: String?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "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.
*/
fun assertNotNull(message: String?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "Expected value to be not null." }, actual != null)
}
}
/**
* Checks applicability and provides Asserter instance
*/
public interface AsserterContributor {
/**
* Provides [Asserter] instance or `null` depends on the current context.
*
* @return asserter instance or null if it is not applicable now
*/
fun contribute(): Asserter?
}
internal actual fun lookupAsserter(): Asserter = DefaultAsserter
@@ -1,18 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package kotlin.test
/**
* Default [Asserter] implementation for Kotlin/Native.
*/
public object DefaultAsserter : Asserter {
override fun fail(message: String?): Nothing {
if (message == null)
throw AssertionError()
else
throw AssertionError(message)
}
}
@@ -1,9 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package kotlin.test
@PublishedApi
internal fun messagePrefix(message: String?) = if (message == null) "" else "$message. "