Rewrites WearUtilTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-08 19:59:12 -07:00
parent 89bd176996
commit 6e72ffed01

View file

@ -1,8 +1,8 @@
package info.nightscout.androidaps.interaction.utils package info.nightscout.androidaps.interaction.utils
import com.google.common.truth.Truth.assertThat
import info.nightscout.androidaps.WearTestBase import info.nightscout.androidaps.WearTestBase
import info.nightscout.androidaps.testing.mockers.WearUtilMocker import info.nightscout.androidaps.testing.mockers.WearUtilMocker
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
/** /**
@ -14,21 +14,18 @@ class WearUtilTest : WearTestBase() {
@Test fun timestampAndTimeDiffsTest() { @Test fun timestampAndTimeDiffsTest() {
// smoke for mocks - since we freeze "now" to get stable tests // smoke for mocks - since we freeze "now" to get stable tests
Assertions.assertEquals(WearUtilMocker.REF_NOW, wearUtil.timestamp()) assertThat(wearUtil.timestamp()).isEqualTo(WearUtilMocker.REF_NOW)
Assertions.assertEquals(0L, wearUtil.msTill(WearUtilMocker.REF_NOW)) assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW)).isEqualTo(0L)
Assertions.assertEquals(3456L, wearUtil.msTill(WearUtilMocker.REF_NOW + 3456L)) assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW + 3456L)).isEqualTo(3456L)
Assertions.assertEquals(-6294L, wearUtil.msTill(WearUtilMocker.REF_NOW - 6294L)) assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW - 6294L)).isEqualTo(-6294L)
Assertions.assertEquals(0L, wearUtil.msTill(WearUtilMocker.REF_NOW)) assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW)).isEqualTo(0L)
Assertions.assertEquals(-3456L, wearUtil.msSince(WearUtilMocker.REF_NOW + 3456L)) assertThat(wearUtil.msSince(WearUtilMocker.REF_NOW + 3456L)).isEqualTo(-3456L)
Assertions.assertEquals(6294L, wearUtil.msSince(WearUtilMocker.REF_NOW - 6294L)) assertThat(wearUtil.msSince(WearUtilMocker.REF_NOW - 6294L)).isEqualTo(6294L)
} }
@Test fun joinSetTest() { @Test fun joinSetTest() {
// GIVEN // GIVEN
val refSet: MutableSet<String> = HashSet() val refSet = setOf("element1", "second-elem", "3rd")
refSet.add("element1")
refSet.add("second-elem")
refSet.add("3rd")
// WHEN // WHEN
val joined = persistence.joinSet(refSet, "|") val joined = persistence.joinSet(refSet, "|")
@ -36,10 +33,10 @@ class WearUtilTest : WearTestBase() {
// THEN // THEN
// we cannot guarantee order of items in joined string // we cannot guarantee order of items in joined string
// but all items have to be there // but all items have to be there
Assertions.assertEquals(joined.length, "element1".length + "second-elem".length + "3rd".length + "|".length * 2) assertThat(joined).hasLength(refSet.sumOf { it.length } + (refSet.size-1))
Assertions.assertTrue("|$joined|".contains("|" + "element1" + "|")) assertThat("|$joined|").contains("|element1|")
Assertions.assertTrue("|$joined|".contains("|" + "second-elem" + "|")) assertThat("|$joined|").contains("|second-elem|")
Assertions.assertTrue("|$joined|".contains("|" + "3rd" + "|")) assertThat("|$joined|").contains("|3rd|")
} }
@Test fun explodeSetTest() { @Test fun explodeSetTest() {
@ -50,10 +47,7 @@ class WearUtilTest : WearTestBase() {
val set = persistence.explodeSet(serializedSet, ":") val set = persistence.explodeSet(serializedSet, ":")
// THEN // THEN
Assertions.assertEquals(set.size, 3) assertThat(set).containsExactly( "element1", "second-elem", "3rd")
Assertions.assertTrue(set.contains("element1"))
Assertions.assertTrue(set.contains("second-elem"))
Assertions.assertTrue(set.contains("3rd"))
} }
@Test fun explodeSetEmptyElemsTest() { @Test fun explodeSetEmptyElemsTest() {
@ -64,27 +58,25 @@ class WearUtilTest : WearTestBase() {
val set = persistence.explodeSet(serializedSet, ",") val set = persistence.explodeSet(serializedSet, ",")
// THEN // THEN
Assertions.assertEquals(set.size, 2) assertThat(set).containsExactly("real", "another")
Assertions.assertEquals(true, set.contains("real"))
Assertions.assertEquals(true, set.contains("another"))
} }
@Test fun joinExplodeStabilityTest() { @Test fun joinExplodeStabilityTest() {
// GIVEN // GIVEN
val refSet: MutableSet<String> = HashSet() val refSet = setOf(
refSet.add("element1") "element1",
refSet.add("second-elem") "second-elem",
refSet.add("3rd") "3rd",
refSet.add("czwarty") "czwarty",
refSet.add("V") "V",
refSet.add("6") "6")
// WHEN // WHEN
val joinedSet = persistence.joinSet(refSet, "#") val joinedSet = persistence.joinSet(refSet, "#")
val explodedSet = persistence.explodeSet(joinedSet, "#") val explodedSet = persistence.explodeSet(joinedSet, "#")
// THEN // THEN
Assertions.assertEquals(explodedSet, refSet) assertThat(explodedSet).containsExactlyElementsIn(refSet)
} }
/* Mike: failing with new mockito /* Mike: failing with new mockito
@ -100,8 +92,8 @@ class WearUtilTest : WearTestBase() {
// THEN // THEN
// we cannot guarantee to be exact to the millisecond - we add some margin of error // we cannot guarantee to be exact to the millisecond - we add some margin of error
Assertions.assertTrue(60L > measuredSleepDuration) assertThat(measuredSleepDuration).isLessThan(60L)
Assertions.assertTrue(requestedSleepDuration + measuringMargin < measuredSleepDuration) assertThat(measuredSleepDuration).isGreaterThan(requestedSleepDuration + measuringMargin)
} }
*/ */
@Test fun rateLimitTest() { @Test fun rateLimitTest() {
@ -115,9 +107,9 @@ class WearUtilTest : WearTestBase() {
val callAfterRateLimit = wearUtil.isBelowRateLimit("test-limit", 3) val callAfterRateLimit = wearUtil.isBelowRateLimit("test-limit", 3)
// THEN // THEN
Assertions.assertTrue(firstCall) assertThat(firstCall).isTrue()
Assertions.assertFalse(callAfterward) assertThat(callAfterward).isFalse()
Assertions.assertFalse(callTooSoon) assertThat(callTooSoon).isFalse()
Assertions.assertTrue(callAfterRateLimit) assertThat(callAfterRateLimit).isTrue()
} }
} }