AndroidAPS/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java

72 lines
3 KiB
Java
Raw Normal View History

package info.nightscout.androidaps.interfaces;
2018-03-19 13:11:25 +01:00
import junit.framework.Assert;
2019-03-21 21:29:09 +01:00
import org.junit.Before;
2018-03-19 13:11:25 +01:00
import org.junit.Test;
import org.junit.runner.RunWith;
2018-07-30 20:23:48 +02:00
import org.powermock.core.classloader.annotations.PrepareForTest;
2018-03-19 13:11:25 +01:00
import org.powermock.modules.junit4.PowerMockRunner;
2018-07-30 20:23:48 +02:00
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
2019-03-21 21:29:09 +01:00
import info.nightscout.androidaps.logging.L;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.SP;
2018-07-30 20:23:48 +02:00
2018-03-19 13:11:25 +01:00
/**
* Created by mike on 19.03.2018.
*/
@RunWith(PowerMockRunner.class)
2019-03-21 21:29:09 +01:00
@PrepareForTest({MainApp.class, SP.class, L.class})
2018-03-19 17:38:48 +01:00
public class ConstraintTest {
2018-03-19 13:11:25 +01:00
@Test
2018-07-30 20:23:48 +02:00
public void doTests() {
2018-03-20 22:09:22 +01:00
Constraint<Boolean> b = new Constraint<>(true);
Assert.assertEquals(Boolean.TRUE, b.value());
Assert.assertEquals("", b.getReasons());
Assert.assertEquals("", b.getMostLimitedReasons());
2018-03-20 22:09:22 +01:00
b.set(false);
Assert.assertEquals(Boolean.FALSE, b.value());
Assert.assertEquals("", b.getReasons());
Assert.assertEquals("", b.getMostLimitedReasons());
b.set(true, "Set true", this);
2018-03-20 22:09:22 +01:00
Assert.assertEquals(Boolean.TRUE, b.value());
Assert.assertEquals("ConstraintTest: Set true", b.getReasons());
Assert.assertEquals("ConstraintTest: Set true", b.getMostLimitedReasons());
b.set(false, "Set false", this);
2018-03-20 22:09:22 +01:00
Assert.assertEquals(Boolean.FALSE, b.value());
Assert.assertEquals("ConstraintTest: Set true\nConstraintTest: Set false", b.getReasons());
Assert.assertEquals("ConstraintTest: Set true\nConstraintTest: Set false", b.getMostLimitedReasons());
2018-03-19 13:11:25 +01:00
2018-03-20 22:09:22 +01:00
Constraint<Double> d = new Constraint<>(10d);
d.set(5d, "Set 5d", this);
2018-03-20 23:47:37 +01:00
Assert.assertEquals(5d, d.value());
Assert.assertEquals("ConstraintTest: Set 5d", d.getReasons());
Assert.assertEquals("ConstraintTest: Set 5d", d.getMostLimitedReasons());
d.setIfSmaller(6d, "Set 6d", this);
2018-03-20 23:47:37 +01:00
Assert.assertEquals(5d, d.value());
Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d", d.getReasons());
Assert.assertEquals("ConstraintTest: Set 5d", d.getMostLimitedReasons());
d.setIfSmaller(4d, "Set 4d", this);
2018-03-20 23:47:37 +01:00
Assert.assertEquals(4d, d.value());
Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d\nConstraintTest: Set 4d", d.getReasons());
Assert.assertEquals("ConstraintTest: Set 4d", d.getMostLimitedReasons());
Assert.assertEquals(10d, d.originalValue());
2019-03-21 21:29:09 +01:00
d.setIfDifferent(7d, "Set 7d", this);
Assert.assertEquals(7d, d.value());
Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d\nConstraintTest: Set 4d\nConstraintTest: Set 7d", d.getReasons());
Assert.assertEquals("ConstraintTest: Set 4d\nConstraintTest: Set 7d", d.getMostLimitedReasons());
Assert.assertEquals(10d, d.originalValue());
}
@Before
public void prepareMock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
2018-03-19 13:11:25 +01:00
}
}