Rewrites SynchronizePacketTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-10-08 18:26:14 -07:00
parent b65512440c
commit 270d6fbdd2

View file

@ -1,12 +1,12 @@
package info.nightscout.pump.medtrum.comm.packets package info.nightscout.pump.medtrum.comm.packets
import com.google.common.truth.Truth.assertThat
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.pump.medtrum.MedtrumTestBase import info.nightscout.pump.medtrum.MedtrumTestBase
import info.nightscout.pump.medtrum.comm.enums.BasalType import info.nightscout.pump.medtrum.comm.enums.BasalType
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
import info.nightscout.pump.medtrum.extension.toByteArray import info.nightscout.pump.medtrum.extension.toByteArray
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
class SynchronizePacketTest : MedtrumTestBase() { class SynchronizePacketTest : MedtrumTestBase() {
@ -35,8 +35,7 @@ class SynchronizePacketTest : MedtrumTestBase() {
val result = packet.getRequest() val result = packet.getRequest()
// Expected values // Expected values
Assertions.assertEquals(1, result.size) assertThat(result).asList().containsExactly(opCode.toByte())
Assertions.assertEquals(opCode.toByte(), result[0])
} }
@Test fun handleResponseGivenResponseWhenMessageIsCorrectLengthThenResultTrue() { @Test fun handleResponseGivenResponseWhenMessageIsCorrectLengthThenResultTrue() {
@ -53,9 +52,9 @@ class SynchronizePacketTest : MedtrumTestBase() {
val result = packet.handleResponse(response) val result = packet.handleResponse(response)
// Expected values // Expected values
Assertions.assertEquals(true, result) assertThat(result).isTrue()
Assertions.assertEquals(false, packet.failed) assertThat(packet.failed).isFalse()
Assertions.assertEquals(state, packet.medtrumPump.pumpState.state) assertThat(packet.medtrumPump.pumpState.state).isEqualTo(state)
} }
@Test fun handleResponseGivenResponseWhenMessageTooShortThenResultFalse() { @Test fun handleResponseGivenResponseWhenMessageTooShortThenResultFalse() {
@ -71,13 +70,13 @@ class SynchronizePacketTest : MedtrumTestBase() {
val result = packet.handleResponse(response) val result = packet.handleResponse(response)
// Expected values // Expected values
Assertions.assertEquals(false, result) assertThat(result).isFalse()
Assertions.assertEquals(true, packet.failed) assertThat(packet.failed).isTrue()
} }
@Test fun handleResponseContainingSyncDataThenDataSaved() { @Test fun handleResponseContainingSyncDataThenDataSaved() {
// Inputs // Inputs
val data = val byteData =
byteArrayOf( byteArrayOf(
47, 47,
3, 3,
@ -130,21 +129,21 @@ class SynchronizePacketTest : MedtrumTestBase() {
// Call // Call
val packet = SynchronizePacket(packetInjector) val packet = SynchronizePacket(packetInjector)
val result = packet.handleResponse(data) val result = packet.handleResponse(byteData)
// Expected values // Expected values
Assertions.assertEquals(true, result) assertThat(result).isTrue()
Assertions.assertEquals(false, packet.failed) assertThat(packet.failed).isFalse()
Assertions.assertEquals(MedtrumPumpState.ACTIVE, packet.medtrumPump.pumpState) assertThat(packet.medtrumPump.pumpState).isEqualTo(MedtrumPumpState.ACTIVE)
Assertions.assertEquals(BasalType.ABSOLUTE_TEMP, packet.medtrumPump.lastBasalType) assertThat(packet.medtrumPump.lastBasalType).isEqualTo(BasalType.ABSOLUTE_TEMP)
Assertions.assertEquals(0.85, packet.medtrumPump.lastBasalRate, 0.01) assertThat(packet.medtrumPump.lastBasalRate).isWithin(0.01).of(0.85)
Assertions.assertEquals(25, packet.medtrumPump.lastBasalSequence) assertThat(packet.medtrumPump.lastBasalSequence).isEqualTo(25)
Assertions.assertEquals(14, packet.medtrumPump.lastBasalPatchId) assertThat(packet.medtrumPump.lastBasalPatchId).isEqualTo(14)
Assertions.assertEquals(1685126612000, packet.medtrumPump.lastBasalStartTime) assertThat(packet.medtrumPump.lastBasalStartTime).isEqualTo(1685126612000)
Assertions.assertEquals(8123, packet.medtrumPump.patchAge) assertThat(packet.medtrumPump.patchAge).isEqualTo(8123)
Assertions.assertEquals(186.80, packet.medtrumPump.reservoir, 0.01) assertThat(packet.medtrumPump.reservoir).isWithin(0.01).of(186.80)
Assertions.assertEquals(1685120120000, packet.medtrumPump.patchStartTime) assertThat(packet.medtrumPump.patchStartTime).isEqualTo(1685120120000)
Assertions.assertEquals(5.96875, packet.medtrumPump.batteryVoltage_A, 0.01) assertThat(packet.medtrumPump.batteryVoltage_A).isWithin(0.01).of(5.96875)
Assertions.assertEquals(2.8125, packet.medtrumPump.batteryVoltage_B, 0.01) assertThat(packet.medtrumPump.batteryVoltage_B).isWithin(0.01).of(2.8125)
} }
} }