Skip to content

Commit

Permalink
WIP multiplatform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsmb13 committed May 11, 2024
1 parent 68e41bd commit 7957d46
Show file tree
Hide file tree
Showing 17 changed files with 2,495 additions and 12 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ jobs:
- name: Debug build
run: ./gradlew assembleDebug --stacktrace
- name: Unit test
run: ./gradlew testDebugUnitTest --stacktrace
run: ./gradlew allTests --stacktrace
- name: Upload test results
uses: actions/upload-artifact@v4
with:
name: test-results
path: '*/build/reports/tests'
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.testing.internal.KotlinTestReport

class KmpLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
Expand All @@ -18,6 +20,8 @@ class KmpLibraryConventionPlugin : Plugin<Project> {
}

configure<KotlinMultiplatformExtension> {
applyDefaultHierarchyTemplate()

androidTarget {
publishLibraryVariants("release")
compilations.all {
Expand Down Expand Up @@ -50,6 +54,24 @@ class KmpLibraryConventionPlugin : Plugin<Project> {
watchosX64()
}

val commonTest by sourceSets.commonTest
val jbTest by sourceSets.creating {
dependsOn(commonTest)
}

targets.forEach { target ->
if (target.platformType !in listOf(KotlinPlatformType.androidJvm, KotlinPlatformType.common)) {
target.compilations.getByName("test").defaultSourceSet {
dependsOn(jbTest)
}
}
}

val allTestsTask = tasks.findByName("allTests")
allTestsTask?.doFirst {
(allTestsTask as KotlinTestReport).ignoreFailures = true
}

explicitApi()

@OptIn(ExperimentalKotlinGradlePluginApi::class)
Expand All @@ -76,7 +98,7 @@ class KmpLibraryConventionPlugin : Plugin<Project> {
pom {
name.set("requireKTX")
description.set("Kotlin utilities for easily grabbing required values")
inceptionYear.set("2020")
inceptionYear.set("2021")
url.set("https://github.com/zsmb13/requireKTX")
licenses {
license {
Expand Down
1,995 changes: 1,995 additions & 0 deletions kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ public inline fun Bundle.requireParcelableArrayList(key: String): ArrayList<Parc
* or the stored value is of the wrong type.
*/
public inline fun Bundle.getParcelableArrayListOrNull(key: String): ArrayList<Parcelable>? = getOrNullImpl(key)

/**
* Returns the value associated with the given key, or null if the key doesn't exist,
* or the stored value is of the wrong type.
*/
public inline fun Bundle.getCharSequenceArrayListOrNull(key: String): ArrayList<CharSequence>? = getOrNullImpl(key)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.robolectric.RobolectricTestRunner
import java.util.ArrayList

@RunWith(RobolectricTestRunner::class)
internal class BundleArrayListTest {
internal class BundleArrayListTestAndroid {

// Parcelables
private val testParcelable1: Uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class BundleArraysTest {
internal class BundleArraysTestAndroid {

// Parcelables
private val testParcelable1: Uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class BundlePrimitivesTest {
internal class BundlePrimitivesTestAndroid {

private val testBundle = Bundle().apply {
putBoolean("boolean", true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.util.Calendar
import java.util.UUID

@RunWith(RobolectricTestRunner::class)
internal class BundleReferenceTest {
internal class BundleReferenceTestAndroid {

// Parcelables
private val testParcelable1: Uri
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.zsmb.requirektx

import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
actual abstract class RoboTest
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ public inline fun Bundle.getIntegerArrayListOrNull(key: String): ArrayList<Int>?
*/
public inline fun Bundle.requireCharSequenceArrayList(key: String): ArrayList<CharSequence> = requireImpl(key)

/**
* Returns the value associated with the given key, or null if the key doesn't exist,
* or the stored value is of the wrong type.
*/
public inline fun Bundle.getCharSequenceArrayListOrNull(key: String): ArrayList<CharSequence>? = getOrNullImpl(key)


/**
* Returns the value associated with the given key.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package co.zsmb.requirektx

import androidx.core.bundle.Bundle
import kotlin.test.*

class BundleArrayListTest : RoboTest() {

private val testBundle = Bundle().apply {
putIntegerArrayList("int-arraylist", arrayListOf(1, 2, -3))
putStringArrayList("string-arraylist", arrayListOf("a", "b", "c"))

// Example for invalid types
putString("string", "a")
}

@Test
fun testIntegerArrayList() {
assertEquals(arrayListOf(1, 2, -3), testBundle.getIntegerArrayListOrNull("int-arraylist"))
assertEquals(null, testBundle.getIntegerArrayListOrNull("string"))
assertEquals(null, testBundle.getIntegerArrayListOrNull("invalid"))

assertEquals(arrayListOf(1, 2, -3), testBundle.requireIntegerArrayList("int-arraylist"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireIntegerArrayList("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireIntegerArrayList("string")
}
}

@Test
fun testStringArrayList() {
assertEquals(arrayListOf("a", "b", "c"), testBundle.getStringArrayListOrNull("string-arraylist"))
assertEquals(null, testBundle.getStringArrayListOrNull("string"))
assertEquals(null, testBundle.getStringArrayListOrNull("invalid"))

assertEquals(arrayListOf("a", "b", "c"), testBundle.requireStringArrayList("string-arraylist"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireStringArrayList("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireStringArrayList("string")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package co.zsmb.requirektx

import androidx.core.bundle.Bundle
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertFailsWith

class BundleArraysTest : RoboTest() {

private val testBundle = Bundle().apply {
putBooleanArray("boolean-array", booleanArrayOf(true, false, true))
putByteArray("byte-array", byteArrayOf(1, 2, -3))
putCharArray("char-array", charArrayOf('a', 'b', 'c'))
putDoubleArray("double-array", doubleArrayOf(1.0, 2.0, -3.0))
putFloatArray("float-array", floatArrayOf(1f, 2f, -3f))
putIntArray("int-array", intArrayOf(1, 2, -3))
putLongArray("long-array", longArrayOf(1L, 2L, -3L))
putShortArray("short-array", shortArrayOf(1, 2, -3))

putStringArray("string-array", arrayOf("a", "b", "c"))
putCharSequenceArray("charsequence-array", arrayOf("a" as CharSequence, "b" as CharSequence, "c" as CharSequence))

// Example for invalid types
putString("string", "a")
}

@Test
fun testBooleanArray() {
assertContentEquals(booleanArrayOf(true, false, true), testBundle.getBooleanArrayOrNull("boolean-array"))
assertContentEquals(null, testBundle.getBooleanArrayOrNull("string"))
assertContentEquals(null, testBundle.getBooleanArrayOrNull("invalid"))

assertContentEquals(booleanArrayOf(true, false, true), testBundle.requireBooleanArray("boolean-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireBooleanArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireBooleanArray("string")
}
}

@Test
fun testByteArray() {
assertContentEquals(byteArrayOf(1, 2, -3), testBundle.getByteArrayOrNull("byte-array"))
assertContentEquals(null, testBundle.getByteArrayOrNull("int-array"))
assertContentEquals(null, testBundle.getByteArrayOrNull("invalid"))

assertContentEquals(byteArrayOf(1, 2, -3), testBundle.requireByteArray("byte-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireByteArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireByteArray("string")
}
}

@Test
fun testCharArray() {
assertContentEquals(charArrayOf('a', 'b', 'c'), testBundle.getCharArrayOrNull("char-array"))
assertContentEquals(null, testBundle.getCharArrayOrNull("string"))
assertContentEquals(null, testBundle.getCharArrayOrNull("invalid"))

assertContentEquals(charArrayOf('a', 'b', 'c'), testBundle.requireCharArray("char-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireCharArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireCharArray("string")
}
}

@Test
fun testDoubleArray() {
assertContentEquals(doubleArrayOf(1.0, 2.0, -3.0), testBundle.getDoubleArrayOrNull("double-array"))
assertContentEquals(null, testBundle.getDoubleArrayOrNull("string"))
assertContentEquals(null, testBundle.getDoubleArrayOrNull("invalid"))

assertContentEquals(doubleArrayOf(1.0, 2.0, -3.0), testBundle.requireDoubleArray("double-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireDoubleArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireDoubleArray("string")
}
}

@Test
fun testFloatArray() {
assertContentEquals(floatArrayOf(1f, 2f, -3f), testBundle.getFloatArrayOrNull("float-array"))
assertContentEquals(null, testBundle.getFloatArrayOrNull("string"))
assertContentEquals(null, testBundle.getFloatArrayOrNull("invalid"))

assertContentEquals(floatArrayOf(1f, 2f, -3f), testBundle.requireFloatArray("float-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireFloatArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireFloatArray("string")
}
}

@Test
fun testIntArray() {
assertContentEquals(intArrayOf(1, 2, -3), testBundle.getIntArrayOrNull("int-array"))
assertContentEquals(null, testBundle.getIntArrayOrNull("string"))
assertContentEquals(null, testBundle.getIntArrayOrNull("invalid"))

assertContentEquals(intArrayOf(1, 2, -3), testBundle.requireIntArray("int-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireIntArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireIntArray("string")
}
}

@Test
fun testLongArray() {
assertContentEquals(longArrayOf(1L, 2L, -3L), testBundle.getLongArrayOrNull("long-array"))
assertContentEquals(null, testBundle.getLongArrayOrNull("string"))
assertContentEquals(null, testBundle.getLongArrayOrNull("invalid"))

assertContentEquals(longArrayOf(1L, 2L, -3L), testBundle.requireLongArray("long-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireLongArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireLongArray("string")
}
}

@Test
fun testShortArray() {
assertContentEquals(shortArrayOf(1, 2, -3), testBundle.getShortArrayOrNull("short-array"))
assertContentEquals(null, testBundle.getShortArrayOrNull("string"))
assertContentEquals(null, testBundle.getShortArrayOrNull("invalid"))

assertContentEquals(shortArrayOf(1, 2, -3), testBundle.requireShortArray("short-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireShortArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireShortArray("string")
}
}

@Test
fun testStringArray() {
assertContentEquals(arrayOf("a", "b", "c"), testBundle.getStringArrayOrNull("string-array"))
assertContentEquals(null, testBundle.getStringArrayOrNull("string"))
assertContentEquals(null, testBundle.getStringArrayOrNull("invalid"))

assertContentEquals(arrayOf("a", "b", "c"), testBundle.requireStringArray("string-array"))
assertFailsWith<IllegalArgumentException> {
testBundle.requireStringArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireStringArray("string")
}
}

@Test
fun testCharSequenceArray() {
assertContentEquals(
arrayOf("a" as CharSequence, "b" as CharSequence, "c" as CharSequence),
testBundle.getCharSequenceArrayOrNull("charsequence-array")
)
assertContentEquals(null, testBundle.getCharSequenceArrayOrNull("string"))
assertContentEquals(null, testBundle.getCharSequenceArrayOrNull("invalid"))

assertContentEquals(
arrayOf("a" as CharSequence, "b" as CharSequence, "c" as CharSequence),
testBundle.requireCharSequenceArray("charsequence-array")
)
assertFailsWith<IllegalArgumentException> {
testBundle.requireCharSequenceArray("invalid")
}
assertFailsWith<IllegalStateException> {
testBundle.requireCharSequenceArray("string")
}
}
}

0 comments on commit 7957d46

Please sign in to comment.