Tests: use helper function to assert compile-time and run-time type check

To cleanup warnings about useless cast or type check that is always true.
This commit is contained in:
Ilya Gorbunov
2017-12-25 21:07:20 +03:00
parent ecd42f14b3
commit 053f3b6ac0
7 changed files with 79 additions and 55 deletions
+30 -26
View File
@@ -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<Int>, ps: Promise<String>) {
var _p: Promise<Int>
_p = p.then {
1
}
assertStaticTypeIs<Promise<Int>>(p.then { 1 })
_p = p.then({
1
})
assertStaticTypeIs<Promise<Int>>(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<Promise<Int>>(p.then(f, ft))
_p = p.then({
assertStaticTypeIs<Promise<Int>>(
p.then({
1
} , {
1
})
)
_p = p.then({
assertStaticTypeIs<Promise<Int>>(
p.then({
1
}) {
1
}
1
}
)
_p = p.then(onFulfilled = {
1
})
assertStaticTypeIs<Promise<Int>>(
p.then(onFulfilled = {
1
})
)
_p = p.then(onFulfilled = {
1
}) {
1
}
assertStaticTypeIs<Promise<Int>>(
p.then(onFulfilled = {
1
}) {
1
}
)
p.then {
ps
}.then {
var s: String = it
assertTrue((s as Any) is String)
assertStaticAndRuntimeTypeIs<String>(it)
ps
}.then(
{
var s: String = it
assertTrue((s as Any) is String)
assertStaticAndRuntimeTypeIs<String>(it)
},
{
@@ -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<String>
}
assertStaticAndRuntimeTypeIs<LinkedList<String>>(foo)
}
@Test fun filterNotIntoLinkedListOf() {
@@ -72,9 +67,7 @@ class CollectionJVMTest {
assertEquals(1, foo.size)
assertEquals(listOf("bar"), foo)
assertTrue {
foo is LinkedList<String>
}
assertStaticAndRuntimeTypeIs<LinkedList<String>>(foo)
}
@Test fun filterNotNullIntoLinkedListOf() {
@@ -84,9 +77,7 @@ class CollectionJVMTest {
assertEquals(2, foo.size)
assertEquals(LinkedList(listOf("foo", "bar")), foo)
assertTrue {
foo is LinkedList<String>
}
assertStaticAndRuntimeTypeIs<LinkedList<String>>(foo)
}
@Test fun filterIntoSortedSet() {
@@ -94,9 +85,8 @@ class CollectionJVMTest {
val sorted = data.filterTo(sortedSetOf<String>()) { it.length == 3 }
assertEquals(2, sorted.size)
assertEquals(sortedSetOf("bar", "foo"), sorted)
assertTrue {
sorted is TreeSet<String>
}
assertStaticAndRuntimeTypeIs<TreeSet<String>>(sorted)
}
@Test fun first() {
@@ -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<String>
}
assertStaticAndRuntimeTypeIs<List<String>>(foo)
}
/*
@@ -93,9 +92,7 @@ class CollectionTest {
assertEquals(1, foo.size)
assertEquals(hashSetOf("foo"), foo)
assertTrue {
foo is HashSet<String>
}
assertStaticAndRuntimeTypeIs<HashSet<String>>(foo)
}
@Test fun filterIsInstanceList() {
@@ -244,7 +244,7 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
@Test
fun filter() {
val foo = data.filter { it.startsWith("f") }
expect(true) { foo is List<String> }
assertStaticAndRuntimeTypeIs<List<String>>(foo)
expect(true) { foo.all { it.startsWith("f") } }
expect(1) { foo.size }
assertEquals(listOf("foo"), foo)
@@ -258,7 +258,7 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
@Test
fun drop() {
val foo = data.drop(1)
expect(true) { foo is List<String> }
assertStaticAndRuntimeTypeIs<List<String>>(foo)
expect(true) { foo.all { it.startsWith("b") } }
expect(1) { foo.size }
assertEquals(listOf("bar"), foo)
@@ -267,7 +267,7 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
@Test
fun dropWhile() {
val foo = data.dropWhile { it[0] == 'f' }
expect(true) { foo is List<String> }
assertStaticAndRuntimeTypeIs<List<String>>(foo)
expect(true) { foo.all { it.startsWith("b") } }
expect(1) { foo.size }
assertEquals(listOf("bar"), foo)
@@ -276,7 +276,7 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
@Test
fun filterNot() {
val notFoo = data.filterNot { it.startsWith("f") }
expect(true) { notFoo is List<String> }
assertStaticAndRuntimeTypeIs<List<String>>(notFoo)
expect(true) { notFoo.none { it.startsWith("f") } }
expect(1) { notFoo.size }
assertEquals(listOf("bar"), notFoo)
@@ -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<BadClass>(getBadClassViaSuspend())
assertStaticAndRuntimeTypeIs<BadClass>(getBadClassViaSuspend())
}
fun <T> suspend(block: suspend () -> T) = block
@@ -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
+11 -1
View File
@@ -1,4 +1,14 @@
package test
import kotlin.internal.NoInfer
import kotlin.test.fail
// just a static type check
fun <T> assertStaticTypeIs(@Suppress("UNUSED_PARAMETER") value: T) {}
fun <T> assertStaticTypeIs(@Suppress("UNUSED_PARAMETER") value: @NoInfer T) {}
inline fun <reified T> assertStaticAndRuntimeTypeIs(value: @NoInfer T) {
@Suppress("USELESS_CAST")
if ((value as Any?) !is T) {
fail("Expected value $value to have ${T::class} type")
}
}