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
@@ -7,9 +7,6 @@ package test
public expect fun assertTypeEquals(expected: Any?, actual: Any?)
public expect fun testOnJvm(action: () -> Unit)
public expect fun testOnJs(action: () -> Unit)
public expect val isFloat32RangeEnforced: Boolean
public expect val supportsNamedCapturingGroup: Boolean
+1 -2
View File
@@ -12,8 +12,7 @@ public actual fun assertTypeEquals(expected: Any?, actual: Any?) {
}
public actual fun testOnJvm(action: () -> Unit) { }
public actual fun testOnJs(action: () -> Unit) = action()
public actual val TestPlatform.Companion.current: TestPlatform get() = TestPlatform.Js
// TODO: should be true at least in JS IR after implementing KT-24975
public actual val isFloat32RangeEnforced: Boolean = false
+1 -2
View File
@@ -12,8 +12,7 @@ public actual fun assertTypeEquals(expected: Any?, actual: Any?) {
assertEquals(expected?.javaClass, actual?.javaClass)
}
public actual fun testOnJvm(action: () -> Unit) = action()
public actual fun testOnJs(action: () -> Unit) {}
public actual val TestPlatform.Companion.current: TestPlatform get() = TestPlatform.Jvm
@Suppress("HasPlatformType", "UNCHECKED_CAST")
public fun <T> platformNull() = Collections.singletonList(null as T).first()
+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
}
}
}
+1 -3
View File
@@ -12,9 +12,7 @@ public actual fun assertTypeEquals(expected: Any?, actual: Any?) {
assertEquals(expected?.let { it::class }, actual?.let { it::class })
}
public actual fun testOnJvm(action: () -> Unit) { }
public actual fun testOnJs(action: () -> Unit) { }
public actual val TestPlatform.Companion.current: TestPlatform get() = TestPlatform.Wasm
// TODO: See KT-24975
public actual val isFloat32RangeEnforced: Boolean = false