Skip to content

Commit

Permalink
Add tests to validate equality of profile classes
Browse files Browse the repository at this point in the history
  • Loading branch information
twyatt committed Jan 29, 2025
1 parent e22873a commit 4e301f2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ androidx-core = { module = "androidx.core:core-ktx", version = "1.15.0" }
androidx-startup = { module = "androidx.startup:startup-runtime", version = "1.2.0" }
atomicfu = { module = "org.jetbrains.kotlinx:atomicfu", version.ref = "atomicfu" }
datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version = "0.6.1" }
equalsverifier = { module = "nl.jqno.equalsverifier:equalsverifier", version = "3.18.1" }
khronicle = { module = "com.juul.khronicle:khronicle-core", version = "0.5.1" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
kotlinx-io = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version = "0.6.0" }
mockk = { module = "io.mockk:mockk", version = "1.13.16" }
tuulbox-collections = { module = "com.juul.tuulbox:collections", version.ref = "tuulbox" }
tuulbox-coroutines = { module = "com.juul.tuulbox:coroutines", version.ref = "tuulbox" }
wrappers-bom = { module = "org.jetbrains.kotlin-wrappers:kotlin-wrappers-bom", version = "2025.1.6" }
Expand Down
5 changes: 5 additions & 0 deletions kable-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ kotlin {
implementation(libs.tuulbox.coroutines)
}

androidUnitTest.dependencies {
implementation(libs.equalsverifier)
implementation(libs.mockk)
}

jsMain.dependencies {
api(libs.wrappers.web)
api(project.dependencies.platform(libs.wrappers.bom))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.juul.kable

import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothGattService
import io.mockk.every
import io.mockk.mockk
import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi
import kotlin.test.Test
import kotlin.uuid.toJavaUuid

class ProfileTests {

@Test
fun PlatformDiscoveredService_equals_verified() {
EqualsVerifier
.forClass(PlatformDiscoveredService::class.java)
.withIgnoredFields("characteristics")
.withMocks()
.verify()
}

@Test
fun PlatformDiscoveredCharacteristic_equals_verified() {
EqualsVerifier
.forClass(PlatformDiscoveredCharacteristic::class.java)
.withIgnoredFields("descriptors")
.withMocks()
.verify()
}

@Test
fun PlatformDiscoveredDescriptor_equals_verified() {
EqualsVerifier
.forClass(PlatformDiscoveredDescriptor::class.java)
.withMocks()
.verify()
}
}

private val redService = mockk<BluetoothGattService> {
every { instanceId } returns 1
every { uuid } returns (Bluetooth.BaseUuid + 0x1).toJavaUuid()
}
private val blueService = mockk<BluetoothGattService> {
every { instanceId } returns 2
every { uuid } returns (Bluetooth.BaseUuid + 0x2).toJavaUuid()
}

private val redCharacteristic = mockk<BluetoothGattCharacteristic> {
every { instanceId } returns 3
every { service } returns redService
every { uuid } returns (Bluetooth.BaseUuid + 0x3).toJavaUuid()
}
private val blueCharacteristic = mockk<BluetoothGattCharacteristic> {
every { instanceId } returns 4
every { service } returns redService
every { uuid } returns (Bluetooth.BaseUuid + 0x4).toJavaUuid()
}

private val redDescriptor = mockk<BluetoothGattDescriptor> {
every { characteristic } returns redCharacteristic
every { uuid } returns (Bluetooth.BaseUuid + 0x5).toJavaUuid()
}
private val blueDescriptor = mockk<BluetoothGattDescriptor> {
every { characteristic } returns blueCharacteristic
every { uuid } returns (Bluetooth.BaseUuid + 0x6).toJavaUuid()
}

private fun <T> SingleTypeEqualsVerifierApi<T>.withMocks(): SingleTypeEqualsVerifierApi<T> =
withPrefabValues(BluetoothGattService::class.java, redService, blueService)
.withPrefabValues(BluetoothGattCharacteristic::class.java, redCharacteristic, blueCharacteristic)
.withPrefabValues(BluetoothGattDescriptor::class.java, redDescriptor, blueDescriptor)

0 comments on commit 4e301f2

Please sign in to comment.