Rewrites TriggerLocationTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-10-04 22:08:24 -07:00
parent 83cee8558d
commit 7fa8b87f6d

View file

@ -6,7 +6,6 @@ import app.aaps.plugins.automation.elements.InputLocationMode
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
@ -24,10 +23,10 @@ class TriggerLocationTest : TriggerTestBase() {
t.distance.setValue(2.0) t.distance.setValue(2.0)
t.modeSelected.value = InputLocationMode.Mode.INSIDE t.modeSelected.value = InputLocationMode.Mode.INSIDE
val t1 = t.duplicate() as TriggerLocation val t1 = t.duplicate() as TriggerLocation
Assertions.assertEquals(213.0, t1.latitude.value, 0.01) assertThat(t1.latitude.value).isWithin(0.01).of(213.0)
Assertions.assertEquals(212.0, t1.longitude.value, 0.01) assertThat(t1.longitude.value).isWithin(0.01).of(212.0)
Assertions.assertEquals(2.0, t1.distance.value, 0.01) assertThat(t1.distance.value).isWithin(0.01).of(2.0)
Assertions.assertEquals(InputLocationMode.Mode.INSIDE, t1.modeSelected.value) assertThat(t1.modeSelected.value).isEqualTo(InputLocationMode.Mode.INSIDE)
} }
@Test fun shouldRunTest() { @Test fun shouldRunTest() {
@ -37,12 +36,12 @@ class TriggerLocationTest : TriggerTestBase() {
t.distance.setValue(2.0) t.distance.setValue(2.0)
// t.modeSelected.setValue(InputLocationMode.Mode.OUTSIDE); // t.modeSelected.setValue(InputLocationMode.Mode.OUTSIDE);
`when`(locationDataContainer.lastLocation).thenReturn(null) `when`(locationDataContainer.lastLocation).thenReturn(null)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
`when`(locationDataContainer.lastLocation).thenReturn(mockedLocation()) `when`(locationDataContainer.lastLocation).thenReturn(mockedLocation())
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerLocation(injector) t = TriggerLocation(injector)
t.distance.setValue(-500.0) t.distance.setValue(-500.0)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
//Test of GOING_IN - last mode should be OUTSIDE, and current mode should be INSIDE //Test of GOING_IN - last mode should be OUTSIDE, and current mode should be INSIDE
t = TriggerLocation(injector) t = TriggerLocation(injector)
@ -51,9 +50,9 @@ class TriggerLocationTest : TriggerTestBase() {
`when`(locationDataContainer.lastLocation).thenReturn(null) `when`(locationDataContainer.lastLocation).thenReturn(null)
`when`(locationDataContainer.lastLocation).thenReturn(mockedLocationOut()) `when`(locationDataContainer.lastLocation).thenReturn(mockedLocationOut())
t.modeSelected.value = InputLocationMode.Mode.GOING_IN t.modeSelected.value = InputLocationMode.Mode.GOING_IN
Assertions.assertEquals(t.lastMode, InputLocationMode.Mode.OUTSIDE) assertThat(InputLocationMode.Mode.OUTSIDE).isEqualTo(t.lastMode)
Assertions.assertEquals(t.currentMode(5.0), InputLocationMode.Mode.INSIDE) assertThat(InputLocationMode.Mode.INSIDE).isEqualTo(t.currentMode(5.0))
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
//Test of GOING_OUT - last mode should be INSIDE, and current mode should be OUTSIDE //Test of GOING_OUT - last mode should be INSIDE, and current mode should be OUTSIDE
// Currently unavailable due to problems with Location mocking // Currently unavailable due to problems with Location mocking
@ -67,7 +66,7 @@ class TriggerLocationTest : TriggerTestBase() {
t.distance.setValue(2.0) t.distance.setValue(2.0)
t.modeSelected.value = InputLocationMode.Mode.OUTSIDE t.modeSelected.value = InputLocationMode.Mode.OUTSIDE
// t.modeSelected = t.modeSelected.value // t.modeSelected = t.modeSelected.value
Assertions.assertEquals(locationJson, t.toJSON()) assertThat(t.toJSON()).isEqualTo(locationJson)
} }
@Test @Throws(JSONException::class) fun fromJSONTest() { @Test @Throws(JSONException::class) fun fromJSONTest() {
@ -77,18 +76,18 @@ class TriggerLocationTest : TriggerTestBase() {
t.distance.setValue(2.0) t.distance.setValue(2.0)
t.modeSelected.value = InputLocationMode.Mode.INSIDE t.modeSelected.value = InputLocationMode.Mode.INSIDE
val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerLocation val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerLocation
Assertions.assertEquals(t.latitude.value, t2.latitude.value, 0.01) assertThat(t2.latitude.value).isWithin(0.01).of(t.latitude.value)
Assertions.assertEquals(t.longitude.value, t2.longitude.value, 0.01) assertThat(t2.longitude.value).isWithin(0.01).of(t.longitude.value)
Assertions.assertEquals(t.distance.value, t2.distance.value, 0.01) assertThat(t2.distance.value).isWithin(0.01).of(t.distance.value)
Assertions.assertEquals(t.modeSelected.value, t2.modeSelected.value) assertThat(t2.modeSelected.value).isEqualTo(t.modeSelected.value)
} }
@Test fun friendlyNameTest() { @Test fun friendlyNameTest() {
Assertions.assertEquals(R.string.location, TriggerLocation(injector).friendlyName()) assertThat(TriggerLocation(injector).friendlyName()).isEqualTo(R.string.location)
} }
@Test fun friendlyDescriptionTest() { @Test fun friendlyDescriptionTest() {
Assertions.assertEquals(null, TriggerLocation(injector).friendlyDescription()) //not mocked } assertThat(TriggerLocation(injector).friendlyDescription()).isNull() //not mocked }
} }
@Test fun iconTest() { @Test fun iconTest() {