diff --git a/js/js.libraries/test/core/PromiseTest.kt b/js/js.libraries/test/core/PromiseTest.kt index 3c78884693c..b7fecd86982 100644 --- a/js/js.libraries/test/core/PromiseTest.kt +++ b/js/js.libraries/test/core/PromiseTest.kt @@ -16,58 +16,62 @@ package core +import test.assertStaticAndRuntimeTypeIs +import test.assertStaticTypeIs import kotlin.js.Promise import kotlin.test.* class PromiseTest { + // To be sure that some base cases can be written in Kotlin fun smokeTest(p: Promise, ps: Promise) { - var _p: Promise - _p = p.then { - 1 - } + assertStaticTypeIs>(p.then { 1 }) - _p = p.then({ - 1 - }) + assertStaticTypeIs>(p.then({ 1 })) - val f: (Int) -> Int = { 1 }; - val ft: (Throwable) -> Int = { 1 }; + val f: (Int) -> Int = { 1 } + val ft: (Throwable) -> Int = { 1 } - _p = p.then(f, ft); + assertStaticTypeIs>(p.then(f, ft)) - _p = p.then({ + assertStaticTypeIs>( + p.then({ 1 } , { 1 }) + ) - _p = p.then({ + assertStaticTypeIs>( + p.then({ 1 }) { - 1 - } + 1 + } + ) - _p = p.then(onFulfilled = { - 1 - }) + assertStaticTypeIs>( + p.then(onFulfilled = { + 1 + }) + ) - _p = p.then(onFulfilled = { - 1 - }) { - 1 - } + assertStaticTypeIs>( + p.then(onFulfilled = { + 1 + }) { + 1 + } + ) p.then { ps }.then { - var s: String = it - assertTrue((s as Any) is String) + assertStaticAndRuntimeTypeIs(it) ps }.then( { - var s: String = it - assertTrue((s as Any) is String) + assertStaticAndRuntimeTypeIs(it) }, { diff --git a/libraries/stdlib/test/collections/CollectionJVMTest.kt b/libraries/stdlib/test/collections/CollectionJVMTest.kt index 074e38f2623..b95c16563fa 100644 --- a/libraries/stdlib/test/collections/CollectionJVMTest.kt +++ b/libraries/stdlib/test/collections/CollectionJVMTest.kt @@ -1,10 +1,7 @@ @file:kotlin.jvm.JvmVersion package test.collections -import java.io.ByteArrayInputStream -import java.io.ByteArrayOutputStream -import java.io.ObjectInputStream -import java.io.ObjectOutputStream +import test.assertStaticAndRuntimeTypeIs import kotlin.test.* import kotlin.comparisons.* import java.util.* @@ -57,9 +54,7 @@ class CollectionJVMTest { assertEquals(1, foo.size) assertEquals(listOf("foo"), foo) - assertTrue { - foo is LinkedList - } + assertStaticAndRuntimeTypeIs>(foo) } @Test fun filterNotIntoLinkedListOf() { @@ -72,9 +67,7 @@ class CollectionJVMTest { assertEquals(1, foo.size) assertEquals(listOf("bar"), foo) - assertTrue { - foo is LinkedList - } + assertStaticAndRuntimeTypeIs>(foo) } @Test fun filterNotNullIntoLinkedListOf() { @@ -84,9 +77,7 @@ class CollectionJVMTest { assertEquals(2, foo.size) assertEquals(LinkedList(listOf("foo", "bar")), foo) - assertTrue { - foo is LinkedList - } + assertStaticAndRuntimeTypeIs>(foo) } @Test fun filterIntoSortedSet() { @@ -94,9 +85,8 @@ class CollectionJVMTest { val sorted = data.filterTo(sortedSetOf()) { it.length == 3 } assertEquals(2, sorted.size) assertEquals(sortedSetOf("bar", "foo"), sorted) - assertTrue { - sorted is TreeSet - } + + assertStaticAndRuntimeTypeIs>(sorted) } @Test fun first() { diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 39b954afbae..c5da57b7a13 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -16,6 +16,7 @@ package test.collections +import test.assertStaticAndRuntimeTypeIs import kotlin.test.* import test.collections.behaviors.* import test.comparisons.STRING_CASE_INSENSITIVE_ORDER @@ -53,9 +54,7 @@ class CollectionTest { assertEquals(2, foo.size) assertEquals(listOf("foo", "bar"), foo) - assertTrue { - foo is List - } + assertStaticAndRuntimeTypeIs>(foo) } /* @@ -93,9 +92,7 @@ class CollectionTest { assertEquals(1, foo.size) assertEquals(hashSetOf("foo"), foo) - assertTrue { - foo is HashSet - } + assertStaticAndRuntimeTypeIs>(foo) } @Test fun filterIsInstanceList() { diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index adc730e847a..db5a6163148 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -244,7 +244,7 @@ abstract class IterableTests>(val createFrom: (Array } + assertStaticAndRuntimeTypeIs>(foo) expect(true) { foo.all { it.startsWith("f") } } expect(1) { foo.size } assertEquals(listOf("foo"), foo) @@ -258,7 +258,7 @@ abstract class IterableTests>(val createFrom: (Array } + assertStaticAndRuntimeTypeIs>(foo) expect(true) { foo.all { it.startsWith("b") } } expect(1) { foo.size } assertEquals(listOf("bar"), foo) @@ -267,7 +267,7 @@ abstract class IterableTests>(val createFrom: (Array } + assertStaticAndRuntimeTypeIs>(foo) expect(true) { foo.all { it.startsWith("b") } } expect(1) { foo.size } assertEquals(listOf("bar"), foo) @@ -276,7 +276,7 @@ abstract class IterableTests>(val createFrom: (Array } + assertStaticAndRuntimeTypeIs>(notFoo) expect(true) { notFoo.none { it.startsWith("f") } } expect(1) { notFoo.size } assertEquals(listOf("bar"), notFoo) diff --git a/libraries/stdlib/test/coroutines/CoroutinesReferenceValuesTest.kt b/libraries/stdlib/test/coroutines/CoroutinesReferenceValuesTest.kt index f391606c130..8b4923dff4a 100644 --- a/libraries/stdlib/test/coroutines/CoroutinesReferenceValuesTest.kt +++ b/libraries/stdlib/test/coroutines/CoroutinesReferenceValuesTest.kt @@ -16,6 +16,7 @@ package test.coroutines +import test.assertStaticAndRuntimeTypeIs import kotlin.test.* import kotlin.coroutines.experimental.* @@ -39,8 +40,8 @@ class CoroutinesReferenceValuesTest { // state machine suspend fun checkBadClassTwice() { - assertTrue(getBadClassViaSuspend() is BadClass) - assertTrue(getBadClassViaSuspend() is BadClass) + assertStaticAndRuntimeTypeIs(getBadClassViaSuspend()) + assertStaticAndRuntimeTypeIs(getBadClassViaSuspend()) } fun suspend(block: suspend () -> T) = block diff --git a/libraries/stdlib/test/internalAnnotations.kt b/libraries/stdlib/test/internalAnnotations.kt new file mode 100644 index 00000000000..95fbdc8485a --- /dev/null +++ b/libraries/stdlib/test/internalAnnotations.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2017 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.internal + +// to compile tests in jre/jdk/7/8 projects where stdlib internals are unavailable +@Target(AnnotationTarget.TYPE) +@Retention(AnnotationRetention.BINARY) +internal annotation class NoInfer diff --git a/libraries/stdlib/test/testUtils.kt b/libraries/stdlib/test/testUtils.kt index c4c80180195..f40cb1305de 100644 --- a/libraries/stdlib/test/testUtils.kt +++ b/libraries/stdlib/test/testUtils.kt @@ -1,4 +1,14 @@ package test +import kotlin.internal.NoInfer +import kotlin.test.fail + // just a static type check -fun assertStaticTypeIs(@Suppress("UNUSED_PARAMETER") value: T) {} +fun assertStaticTypeIs(@Suppress("UNUSED_PARAMETER") value: @NoInfer T) {} + +inline fun assertStaticAndRuntimeTypeIs(value: @NoInfer T) { + @Suppress("USELESS_CAST") + if ((value as Any?) !is T) { + fail("Expected value $value to have ${T::class} type") + } +} \ No newline at end of file