Refactor helper function to run/not run block of common code on specific platform

Use it in StringBuilder tests
This commit is contained in:
Ilya Gorbunov
2023-04-22 06:41:51 +02:00
committed by Space Team
parent 454e963aa7
commit ace2279631
7 changed files with 32 additions and 19 deletions
+17
View File
@@ -10,6 +10,23 @@ import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail
public enum class TestPlatform {
Jvm,
Js,
Native,
Wasm;
companion object
}
public expect val TestPlatform.Companion.current: TestPlatform
public fun testOn(platformPredicate: (TestPlatform) -> Boolean, action: () -> Unit) {
if (platformPredicate(TestPlatform.current)) action()
}
public fun testOnlyOn(platform: TestPlatform, action: () -> Unit) = testOn({ it == platform }, action)
public fun testExceptOn(platform: TestPlatform, action: () -> Unit) = testOn({ it != platform}, action)
// just a static type check
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
fun <T> assertStaticTypeIs(@Suppress("UNUSED_PARAMETER") value: @kotlin.internal.NoInfer T) {}
@@ -5,7 +5,9 @@
package test.text
import test.testOnJvm
import test.TestPlatform
import test.testOnlyOn
import test.testExceptOn
import kotlin.random.Random
import kotlin.test.*
import kotlin.text.*
@@ -39,11 +41,11 @@ class StringBuilderTest {
fun deprecatedAppend() {
val chars = charArrayOf('a', 'b', 'c', 'd')
val sb = StringBuilder()
testOnJvm {
testOnlyOn(TestPlatform.Jvm) {
sb.append(chars, 1, 2) // Should fail after KT-15220 gets fixed
assertEquals("bc", sb.toString())
}
if (sb.isEmpty()) {
testExceptOn(TestPlatform.Jvm) {
assertFailsWith<NotImplementedError> {
sb.append(chars, 1, 2)
}
@@ -256,8 +258,9 @@ class StringBuilderTest {
@Test
@Suppress("DEPRECATION")
fun capacityTest() {
// assertEquals(100, StringBuilder(100).capacity()) // not implemented in JS
testExceptOn(TestPlatform.Js) {
assertEquals(100, StringBuilder(100).capacity()) // not implemented in JS
}
StringBuilder("string builder from string capacity test").let { sb ->
assertTrue(sb.capacity() >= sb.length)
}
@@ -270,7 +273,9 @@ class StringBuilderTest {
sb.ensureCapacity(1)
assertTrue(sb.capacity() >= sb.length)
sb.ensureCapacity(sb.length * 10)
// assertTrue(sb.capacity() >= sb.length * 10) // not implemented in JS
testExceptOn(TestPlatform.Js) {
assertTrue(sb.capacity() >= sb.length * 10) // not implemented in JS
}
}
}