kotlin-test: rename shared module to common.

Do not use shared kotlin-test while building js stdlib.
This commit is contained in:
Ilya Gorbunov
2017-01-24 01:23:46 +03:00
parent 5ef27f7ad3
commit 9b9852cdc9
18 changed files with 4 additions and 8 deletions
@@ -0,0 +1,46 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>sources</id>
<formats>
<format>jar</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<filtered>true</filtered>
<lineEnding>unix</lineEnding>
<directory>src/main/kotlin</directory>
<outputDirectory/>
<includes>
<include>**/*.kt</include>
</includes>
</fileSet>
<fileSet>
<filtered>true</filtered>
<lineEnding>unix</lineEnding>
<directory>src/main/kotlin.jvm</directory>
<outputDirectory/>
<includes>
<include>**/*.kt</include>
</includes>
</fileSet>
<fileSet>
<filtered>true</filtered>
<lineEnding>unix</lineEnding>
<directory>src/main/kotlin.js</directory>
<outputDirectory/>
<includes>
<include>**/*.kt</include>
</includes>
</fileSet>
</fileSets>
</assembly>
@@ -0,0 +1,18 @@
package kotlin.internal
/**
* The value of this type parameter should be mentioned in input types (argument types, receiver type or expected type).
*/
@Suppress("HEADER_WITHOUT_IMPLEMENTATION")
@Target(AnnotationTarget.TYPE_PARAMETER)
@Retention(AnnotationRetention.BINARY)
internal header annotation class OnlyInputTypes
/**
* Specifies that this function should not be called directly without inlining
*/
@Suppress("HEADER_WITHOUT_IMPLEMENTATION")
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
internal header annotation class InlineOnly
@@ -0,0 +1,9 @@
package kotlin.jvm
@Suppress("HEADER_WITHOUT_IMPLEMENTATION")
@Target(AnnotationTarget.FILE)
internal header annotation class JvmMultifileClass
@Suppress("HEADER_WITHOUT_IMPLEMENTATION")
@Target(AnnotationTarget.FILE)
internal header annotation class JvmName(val name: String)
@@ -0,0 +1,186 @@
/*
* Copyright 2010-2016 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.
*/
/**
* A number of helper methods for writing unit tests.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("AssertionsKt")
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package kotlin.test
import kotlin.internal.*
val asserter: Asserter
get() = lookupAsserter()
/** Asserts that the given [block] returns `true`. */
fun assertTrue(message: String? = null, block: () -> Boolean): Unit = assertTrue(block(), message)
/** Asserts that the expression is `true` with an optional [message]. */
fun assertTrue(actual: Boolean, message: String? = null) {
return asserter.assertTrue(message ?: "Expected value to be true.", actual)
}
/** Asserts that the given [block] returns `false`. */
fun assertFalse(message: String? = null, block: () -> Boolean): Unit = assertFalse(block(), message)
/** Asserts that the expression is `false` with an optional [message]. */
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]. */
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]. */
fun <@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]. */
fun <T : Any> 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. */
fun <T : Any, R> 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]. */
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]. */
fun fail(message: String? = null): Nothing {
asserter.fail(message)
}
/** Asserts that given function [block] returns the given [expected] value. */
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. */
fun <@OnlyInputTypes T> expect(expected: T, message: String?, block: () -> T) {
assertEquals(expected, block(), message)
}
/** Asserts that given function [block] fails by throwing an exception. */
fun assertFails(block: () -> Unit): Throwable = assertFails(null, block)
/** Asserts that given function [block] fails by throwing an exception. */
@SinceKotlin("1.1")
fun assertFails(message: String?, block: () -> Unit): Throwable {
try {
block()
} catch (e: Throwable) {
return e
}
asserter.fail(messagePrefix(message) + "Expected an exception to be thrown, but was completed successfully.")
}
/**
* Abstracts the logic for performing assertions. Specific implementations of [Asserter] can use JUnit
* or TestNG assertion facilities.
*/
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 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
*/
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?
}
@@ -0,0 +1,17 @@
package kotlin.test
/**
* Default [Asserter] implementation to avoid dependency on JUnit or TestNG.
*/
class DefaultAsserter() : Asserter {
override fun fail(message: String?): Nothing {
if (message == null)
throw AssertionError()
else
throw AssertionError(message)
}
}
header fun AssertionError(message: String): Throwable
header fun AssertionError(): Throwable
@@ -0,0 +1,4 @@
package kotlin.test
internal fun messagePrefix(message: String?) = if (message == null) "" else "$message. "
internal header fun lookupAsserter(): Asserter
@@ -0,0 +1,4 @@
package org.junit
@Suppress("HEADER_WITHOUT_IMPLEMENTATION")
header annotation class Test
@@ -0,0 +1,34 @@
package kotlin.test.tests
import kotlin.test.*
import org.junit.*
class BasicAssertionsJVMTest {
@Test
fun testFailsWith() {
assertFailsWith(IllegalArgumentException::class) {
throw IllegalArgumentException()
}
}
@Test(expected = AssertionError::class)
fun testFailsWithFails() {
assertFailsWith(IllegalArgumentException::class) {
throw IllegalStateException()
}
}
@Test
fun testFailsWithMessage() {
assertFailsWith<IllegalArgumentException>() {
throw IllegalArgumentException()
}
}
@Test
fun testFailsWithClassMessage() {
@Suppress("UNCHECKED_CAST")
(assertFailsWith((Class.forName("java.lang.IllegalArgumentException") as Class<Throwable>).kotlin) {
throw IllegalArgumentException()
})
}
}
@@ -0,0 +1,23 @@
package kotlin.test.tests
import org.junit.Test
import kotlin.test.*
// NOTE: These tests verify line numbers of stack frames, so they might be quite fragile to reordering etc
class StackTraceJVMTest {
@Test
fun testCurrentStackTrace() {
/* <-- line number */ val topFrame = currentStackTrace()[0]
assertEquals("StackTraceJVMTest.kt", topFrame.fileName)
assertEquals(12, topFrame.lineNumber)
}
@Test
fun testToDo() {
todo {
fail("Shouldn't pass here")
}
}
}
@@ -0,0 +1,108 @@
/*
* 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) {
try {
block()
}
catch (e: AssertionError) {
checkAssertion(e.message)
return
}
fail("Expected AssertionError to be thrown.")
}
private val message = "Some details"
private val expected = object {
override fun toString() = "expected"
}
private val actual = object {
override fun toString() = "actual"
}
@Test
fun assertEqualsMessage() {
expectAssertion( { msg ->
assertNotNull(msg); msg!!
assertTrue { msg.contains(expected.toString()) }
assertTrue { msg.contains(actual.toString()) }
assertFalse { msg.startsWith(".") }
}, { assertEquals<Any>(expected, actual) })
expectAssertion( { msg ->
assertNotNull(msg); msg!!
assertTrue { msg.contains(message) }
assertTrue { msg.contains(expected.toString()) }
assertTrue { msg.contains(actual.toString()) }
} , { assertEquals<Any>(expected, actual, message) })
}
@Test
fun assertNotEqualsMessage() {
expectAssertion( { msg ->
assertNotNull(msg); msg!!
assertTrue { msg.contains(actual.toString()) }
assertFalse { msg.startsWith(".") }
}, { assertNotEquals(actual, actual) })
expectAssertion( { msg ->
assertNotNull(msg); msg!!
assertTrue { msg.contains(message) }
assertTrue { msg.contains(actual.toString()) }
} , { assertNotEquals(actual, actual, message) })
}
@Test
fun assertTrueMessage() {
expectAssertion( { msg -> assertNotNull(msg) }, { assertTrue(false) })
expectAssertion( { msg -> assertNotNull(msg) }, { assertTrue { false } })
expectAssertion( { msg -> assertNotNull(msg) }, { assertTrue(null) { false } })
expectAssertion( { msg -> msg!!.contains(message)}, { assertTrue(false, message)})
expectAssertion( { msg -> msg!!.contains(message)}, { assertTrue(message) { false }})
}
@Test
fun assertFalseMessage() {
expectAssertion( { msg -> assertNotNull(msg) }, { assertFalse(true) })
expectAssertion( { msg -> assertNotNull(msg) }, { assertFalse { true } })
expectAssertion( { msg -> assertNotNull(msg) }, { assertFalse(null) { true } })
expectAssertion( { msg -> assertTrue(msg!!.contains(message)) }, { assertFalse(true, message)})
expectAssertion( { msg -> assertTrue(msg!!.contains(message)) }, { assertFalse(message) { true }})
}
@Test
fun assertNotNullMessage() {
expectAssertion( { msg -> msg!! }, { assertNotNull(null) })
expectAssertion( { msg -> assertTrue(msg!!.contains(message)) }, { assertNotNull(null, message)})
}
@Test
fun assertNullMessage() {
expectAssertion( { msg -> msg!! }, { assertNull(actual) })
expectAssertion( { msg -> msg!!
assertTrue(msg.contains(message))
assertTrue(msg.contains(actual.toString()))
}, { assertNull(actual, message)})
}
}
@@ -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)
@@ -0,0 +1,131 @@
package kotlin.test.tests
import org.junit.*
import kotlin.test.*
class BasicAssertionsTest {
@Test
fun testAssertEquals() {
assertEquals(1, 1)
}
@Test
fun testAssertEqualsString() {
assertEquals("a", "a")
}
@Test(expected = AssertionError::class)
fun testAssertEqualsFails() {
assertEquals(1, 2)
}
@Test
fun testAssertTrue() {
assertTrue(true)
}
@Test
fun testAssertTrueWithLambda() {
assertTrue { true }
}
@Test(expected = AssertionError::class)
fun testAssertTrueFails() {
assertTrue(false)
}
@Test(expected = AssertionError::class)
fun testAssertTrueWithLambdaFails() {
assertTrue { false }
}
@Test
fun testAssertFalse() {
assertFalse(false)
}
@Test
fun testAssertFalseLambda() {
assertFalse { false }
}
@Test(expected = AssertionError::class)
fun testAssertFalseFails() {
assertFalse(true)
}
@Test(expected = AssertionError::class)
fun testAssertFalseWithLambdaFails() {
assertFalse { true }
}
@Test
fun testAssertFails() {
assertFails { throw IllegalStateException() }
}
@Test(expected = AssertionError::class)
fun testAssertFailsFails() {
assertFails { }
Assert.fail("Shouldn't pass here")
}
@Test
fun testAssertNotEquals() {
assertNotEquals(1, 2)
}
@Test(expected = AssertionError::class)
fun testAssertNotEqualsFails() {
assertNotEquals(1, 1)
}
@Test
fun testAssertNotNull() {
assertNotNull(true)
}
@Test(expected = AssertionError::class)
fun testAssertNotNullFails() {
assertNotNull(null)
}
@Test
fun testAssertNotNullLambda() {
assertNotNull("") { assertEquals("", it) }
}
@Test(expected = AssertionError::class)
fun testAssertNotNullLambdaFails() {
assertNotNull(null) {
@Suppress("UNREACHABLE_CODE")
assertNotNull(it)
}
}
@Test
fun testAssertNull() {
assertNull(null)
}
@Test(expected = AssertionError::class)
fun testAssertNullFails() {
assertNull(true)
}
@Test(expected = AssertionError::class)
fun testFail() {
fail("should fail")
}
@Test
fun testExpect() {
expect(1) { 1 }
}
@Test(expected = AssertionError::class)
fun testExpectFails() {
expect(1) { 2 }
}
}
@@ -0,0 +1,137 @@
@file:Suppress("DEPRECATION")
package kotlin.test.tests
import org.junit.*
import java.util.*
import kotlin.test.*
class CollectionAssertionTest {
@Test
fun testList() {
assert(listOf(1, 2, 3)) {
sizeShouldBe(3)
elementAtShouldBe(0, 1)
elementAtShouldComply(0) { it > 0 }
lastElementShouldBe(3)
containsAll(1, 2)
shouldBe(listOf(1, 2, 3))
}
}
@Test
fun testSet() {
assert(setOf(1, 2, 3)) {
sizeShouldBe(3)
elementAtShouldBe(0, 1)
elementAtShouldComply(0) { it > 0 }
lastElementShouldBe(3)
containsAll(1, 2)
shouldBeSet(setOf(1, 2, 3))
}
}
@Test(expected = AssertionError::class)
fun testSizeShouldBeFails() {
assert(listOf(1, 2, 3)) {
sizeShouldBe(1)
}
}
@Test(expected = AssertionError::class)
fun testElementAtShouldBeFail() {
assert(listOf(1, 2, 3)) {
elementAtShouldBe(0, 0)
}
}
@Test(expected = AssertionError::class)
fun testElementAtShouldComplyFail() {
assert(listOf(1, 2, 3)) {
elementAtShouldComply(0) { it < 0 }
}
}
@Test(expected = AssertionError::class)
fun testLastElementFail() {
assert(listOf(1, 2, 3)) {
lastElementShouldBe(0)
}
}
@Test(expected = NoSuchElementException::class)
fun testLastElementOnEmptyFail() {
assert(listOf<Int>()) {
lastElementShouldBe(0)
}
}
@Test(expected = AssertionError::class)
fun testContainsAll() {
assert(listOf(1, 2, 3)) {
containsAll(1, 8)
}
}
@Test(expected = AssertionError::class)
fun testContainsAllWithSet() {
assert(setOf(1, 2, 3)) {
containsAll(1, 8)
}
}
@Test
fun testShouldBeLess() {
try {
assert(listOf(1, 2, 3)) {
shouldBe(listOf(1, 2, 3, 4))
}
Assert.fail("It shouldn't pass here")
} catch (e: AssertionError) {
assertTrue { "[4]" in e.message!! }
assertTrue { "shorter" in e.message!! }
}
}
@Test
fun testShouldBeLonger() {
try {
assert(listOf(1, 2, 3)) {
shouldBe(listOf(1, 2))
}
Assert.fail("It shouldn't pass here")
} catch (e: AssertionError) {
assertTrue { "[3]" in e.message!! }
assertTrue { "longer" in e.message!! }
}
}
@Test(expected = AssertionError::class)
fun testShouldBeSetExtra() {
assert(setOf(1, 2, 3)) {
shouldBeSet(setOf(1, 2))
}
}
@Test
fun testShouldBeSetExact() {
assert(setOf(1, 2, 3)) {
shouldBeSet(setOf(1, 2, 3))
}
}
@Test
fun testShouldBeSetExactVararg() {
assert(setOf(1, 2, 3)) {
shouldBeSet(1, 2, 3)
}
}
@Test(expected = AssertionError::class)
fun testShouldBeSetMissing() {
assert(setOf(1, 2, 3)) {
shouldBeSet(setOf(1, 2, 3, 4))
}
}
}
@@ -0,0 +1,22 @@
package kotlin.test.tests
import java.util.*
internal fun <T> listOf(vararg elements: T): List<T> {
val result = ArrayList<T>(elements.size)
for (e in elements) {
result.add(e)
}
return result
}
internal fun <T> setOf(vararg elements: T): Set<T> {
val result = HashSet<T>(elements.size)
for (e in elements) {
result.add(e)
}
return result
}