Rewrites TriggerProfilePercentTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-10-04 22:37:36 -07:00
parent 9f44f30f7d
commit de40468663

View file

@ -4,10 +4,10 @@ import app.aaps.plugins.automation.R
import app.aaps.plugins.automation.elements.Comparator
import com.google.common.truth.Truth.assertThat
import org.json.JSONObject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
import org.skyscreamer.jsonassert.JSONAssert
class TriggerProfilePercentTest : TriggerTestBase() {
@ -17,43 +17,43 @@ class TriggerProfilePercentTest : TriggerTestBase() {
@Test fun shouldRunTest() {
var t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(101.0).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertFalse(t.shouldRun())
assertThat(t.shouldRun()).isFalse()
t = TriggerProfilePercent(injector).setValue(100.0).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertTrue(t.shouldRun())
assertThat(t.shouldRun()).isTrue()
t = TriggerProfilePercent(injector).setValue(100.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertTrue(t.shouldRun())
assertThat(t.shouldRun()).isTrue()
t = TriggerProfilePercent(injector).setValue(90.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertTrue(t.shouldRun())
assertThat(t.shouldRun()).isTrue()
t = TriggerProfilePercent(injector).setValue(100.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertTrue(t.shouldRun())
assertThat(t.shouldRun()).isTrue()
t = TriggerProfilePercent(injector).setValue(101.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertTrue(t.shouldRun())
assertThat(t.shouldRun()).isTrue()
t = TriggerProfilePercent(injector).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertFalse(t.shouldRun())
assertThat(t.shouldRun()).isFalse()
t = TriggerProfilePercent(injector).setValue(110.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertFalse(t.shouldRun())
assertThat(t.shouldRun()).isFalse()
t = TriggerProfilePercent(injector).setValue(90.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertFalse(t.shouldRun())
assertThat(t.shouldRun()).isFalse()
}
@Test fun copyConstructorTest() {
val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
val t1 = t.duplicate() as TriggerProfilePercent
Assertions.assertEquals(213.0, t1.pct.value, 0.01)
Assertions.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
assertThat( t1.pct.value).isWithin(0.01).of(213.0)
assertThat(t.comparator.value).isEqualTo(Comparator.Compare.IS_EQUAL_OR_LESSER)
}
private val bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"percentage\":110},\"type\":\"TriggerProfilePercent\"}"
@Test fun toJSONTest() {
val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(110.0).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertEquals(bgJson, t.toJSON())
JSONAssert.assertEquals(bgJson, t.toJSON(), true)
}
@Test fun fromJSONTest() {
val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(120.0).comparator(Comparator.Compare.IS_EQUAL)
val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerProfilePercent
Assertions.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value)
Assertions.assertEquals(120.0, t2.pct.value, 0.01)
assertThat(t2.comparator.value).isEqualTo(Comparator.Compare.IS_EQUAL)
assertThat(t2.pct.value).isWithin(0.01).of(120.0)
}
@Test fun iconTest() {
@ -61,6 +61,6 @@ class TriggerProfilePercentTest : TriggerTestBase() {
}
@Test fun friendlyNameTest() {
Assertions.assertEquals(R.string.profilepercentage, TriggerProfilePercent(injector).friendlyName()) // not mocked
assertThat(TriggerProfilePercent(injector).friendlyName()).isEqualTo(R.string.profilepercentage) // not mocked
}
}
}