mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-10 02:58:51 -06:00
Repetition: Replace toggle by setValue
This commit is contained in:
@@ -30,6 +30,7 @@ import java.util.*;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.isoron.uhabits.core.models.Frequency.*;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -54,9 +55,9 @@ public class ImportTest extends BaseUnitTest
|
||||
assertThat(habit.getName(), equalTo("Breed dragons"));
|
||||
assertThat(habit.getDescription(), equalTo("with love and fire"));
|
||||
assertThat(habit.getFrequency(), equalTo(Frequency.DAILY));
|
||||
assertTrue(containsRepetition(habit, 2016, 3, 18));
|
||||
assertTrue(containsRepetition(habit, 2016, 3, 19));
|
||||
assertFalse(containsRepetition(habit, 2016, 3, 20));
|
||||
assertTrue(isChecked(habit, 2016, 3, 18));
|
||||
assertTrue(isChecked(habit, 2016, 3, 19));
|
||||
assertFalse(isChecked(habit, 2016, 3, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,9 +70,9 @@ public class ImportTest extends BaseUnitTest
|
||||
Habit habit = habitList.getByPosition(0);
|
||||
assertThat(habit.getName(), equalTo("Wake up early"));
|
||||
assertThat(habit.getFrequency(), equalTo(THREE_TIMES_PER_WEEK));
|
||||
assertTrue(containsRepetition(habit, 2016, 3, 14));
|
||||
assertTrue(containsRepetition(habit, 2016, 3, 16));
|
||||
assertFalse(containsRepetition(habit, 2016, 3, 17));
|
||||
assertTrue(isChecked(habit, 2016, 3, 14));
|
||||
assertTrue(isChecked(habit, 2016, 3, 16));
|
||||
assertFalse(isChecked(habit, 2016, 3, 17));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -85,10 +86,10 @@ public class ImportTest extends BaseUnitTest
|
||||
assertThat(habit.getName(), equalTo("Wake up early"));
|
||||
assertThat(habit.getFrequency(), equalTo(THREE_TIMES_PER_WEEK));
|
||||
assertFalse(habit.hasReminder());
|
||||
assertFalse(containsRepetition(habit, 2015, 12, 31));
|
||||
assertTrue(containsRepetition(habit, 2016, 1, 18));
|
||||
assertTrue(containsRepetition(habit, 2016, 1, 28));
|
||||
assertFalse(containsRepetition(habit, 2016, 3, 10));
|
||||
assertFalse(isChecked(habit, 2015, 12, 31));
|
||||
assertTrue(isChecked(habit, 2016, 1, 18));
|
||||
assertTrue(isChecked(habit, 2016, 1, 28));
|
||||
assertFalse(isChecked(habit, 2016, 3, 10));
|
||||
|
||||
habit = habitList.getByPosition(2);
|
||||
assertThat(habit.getName(), equalTo("brush teeth"));
|
||||
@@ -111,17 +112,18 @@ public class ImportTest extends BaseUnitTest
|
||||
|
||||
Habit h = habitList.getByPosition(2);
|
||||
assertThat(h.getName(), equalTo("Vegan"));
|
||||
assertTrue(containsRepetition(h, 2016, 1, 24));
|
||||
assertTrue(containsRepetition(h, 2016, 2, 5));
|
||||
assertTrue(containsRepetition(h, 2016, 3, 18));
|
||||
assertFalse(containsRepetition(h, 2016, 3, 14));
|
||||
assertTrue(isChecked(h, 2016, 1, 24));
|
||||
assertTrue(isChecked(h, 2016, 2, 5));
|
||||
assertTrue(isChecked(h, 2016, 3, 18));
|
||||
assertFalse(isChecked(h, 2016, 3, 14));
|
||||
}
|
||||
|
||||
private boolean containsRepetition(Habit h, int year, int month, int day)
|
||||
private boolean isChecked(Habit h, int year, int month, int day)
|
||||
{
|
||||
GregorianCalendar date = DateUtils.getStartOfTodayCalendar();
|
||||
date.set(year, month - 1, day);
|
||||
return h.getRepetitions().containsTimestamp(new Timestamp(date));
|
||||
Timestamp timestamp = new Timestamp(date);
|
||||
return h.getRepetitions().getValue(timestamp) == YES_MANUAL;
|
||||
}
|
||||
|
||||
private void importFromFile(String assetFilename) throws IOException
|
||||
|
||||
@@ -89,7 +89,7 @@ public class HabitTest extends BaseUnitTest
|
||||
{
|
||||
Habit h = modelFactory.buildHabit();
|
||||
assertFalse(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getToday());
|
||||
h.getRepetitions().setValue(getToday(), Checkmark.YES_MANUAL);
|
||||
assertTrue(h.isCompletedToday());
|
||||
}
|
||||
|
||||
@@ -102,19 +102,19 @@ public class HabitTest extends BaseUnitTest
|
||||
h.setTargetValue(100.0);
|
||||
assertFalse(h.isCompletedToday());
|
||||
|
||||
h.getRepetitions().toggle(getToday(), 200_000);
|
||||
h.getRepetitions().setValue(getToday(), 200_000);
|
||||
assertTrue(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getToday(), 100_000);
|
||||
h.getRepetitions().setValue(getToday(), 100_000);
|
||||
assertTrue(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getToday(), 50_000);
|
||||
h.getRepetitions().setValue(getToday(), 50_000);
|
||||
assertFalse(h.isCompletedToday());
|
||||
|
||||
h.setTargetType(Habit.AT_MOST);
|
||||
h.getRepetitions().toggle(getToday(), 200_000);
|
||||
h.getRepetitions().setValue(getToday(), 200_000);
|
||||
assertFalse(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getToday(), 100_000);
|
||||
h.getRepetitions().setValue(getToday(), 100_000);
|
||||
assertTrue(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getToday(), 50_000);
|
||||
h.getRepetitions().setValue(getToday(), 50_000);
|
||||
assertTrue(h.isCompletedToday());
|
||||
}
|
||||
|
||||
|
||||
@@ -28,11 +28,9 @@ import org.junit.*;
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Calendar.*;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class RepetitionListTest extends BaseUnitTest
|
||||
@@ -60,11 +58,11 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
|
||||
today = DateUtils.getToday();
|
||||
|
||||
reps.toggle(today.minus(3));
|
||||
reps.toggle(today.minus(2));
|
||||
reps.toggle(today);
|
||||
reps.toggle(today.minus(7));
|
||||
reps.toggle(today.minus(5));
|
||||
reps.setValue(today.minus(3), YES_MANUAL);
|
||||
reps.setValue(today.minus(2), YES_MANUAL);
|
||||
reps.setValue(today, YES_MANUAL);
|
||||
reps.setValue(today.minus(7), YES_MANUAL);
|
||||
reps.setValue(today.minus(5), YES_MANUAL);
|
||||
|
||||
listener = mock(ModelObservable.Listener.class);
|
||||
reps.getObservable().addListener(listener);
|
||||
@@ -78,17 +76,6 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_contains()
|
||||
{
|
||||
assertTrue(reps.containsTimestamp(today));
|
||||
assertTrue(reps.containsTimestamp(today.minus(2)));
|
||||
assertTrue(reps.containsTimestamp(today.minus(3)));
|
||||
|
||||
assertFalse(reps.containsTimestamp(today.minus(1)));
|
||||
assertFalse(reps.containsTimestamp(today.minus(4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getOldest()
|
||||
{
|
||||
@@ -126,7 +113,7 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
// Leave the month of March empty, to check that it returns null
|
||||
if (month == MARCH) continue;
|
||||
|
||||
reps.toggle(new Timestamp(day));
|
||||
reps.setValue(new Timestamp(day), YES_MANUAL);
|
||||
|
||||
// Repetitions in December should not be counted
|
||||
if (month == DECEMBER) continue;
|
||||
@@ -155,32 +142,22 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_toggle()
|
||||
public void test_setValue()
|
||||
{
|
||||
assertTrue(reps.containsTimestamp(today));
|
||||
reps.toggle(today);
|
||||
assertFalse(reps.containsTimestamp(today));
|
||||
verify(listener).onModelChange();
|
||||
reset(listener);
|
||||
|
||||
assertFalse(reps.containsTimestamp(today.minus(1)));
|
||||
reps.toggle(today.minus(1));
|
||||
assertTrue(reps.containsTimestamp(today.minus(1)));
|
||||
verify(listener).onModelChange();
|
||||
assertThat(reps.getValue(today), equalTo(YES_MANUAL));
|
||||
reps.setValue(today, NO);
|
||||
assertThat(reps.getValue(today), equalTo(NO));
|
||||
verify(listener, times(2)).onModelChange();
|
||||
reset(listener);
|
||||
|
||||
habit.setType(Habit.NUMBER_HABIT);
|
||||
reps.toggle(today, 100);
|
||||
Repetition check = reps.getByTimestamp(today);
|
||||
assertNotNull(check);
|
||||
assertThat(check.getValue(), equalTo(100));
|
||||
verify(listener).onModelChange();
|
||||
reps.setValue(today, 100);
|
||||
assertThat(reps.getValue(today), equalTo(100));
|
||||
verify(listener, times(2)).onModelChange();
|
||||
reset(listener);
|
||||
|
||||
reps.toggle(today, 500);
|
||||
check = reps.getByTimestamp(today);
|
||||
assertNotNull(check);
|
||||
assertThat(check.getValue(), equalTo(500));
|
||||
reps.setValue(today, 500);
|
||||
assertThat(reps.getValue(today), equalTo(500));
|
||||
verify(listener, times(2)).onModelChange();
|
||||
reset(listener);
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
@Test
|
||||
public void test_getAll()
|
||||
{
|
||||
toggle(0, 20);
|
||||
check(0, 20);
|
||||
|
||||
double expectedValues[] = {
|
||||
0.655747,
|
||||
@@ -82,7 +82,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
@Test
|
||||
public void test_getTodayValue()
|
||||
{
|
||||
toggle(0, 20);
|
||||
check(0, 20);
|
||||
double actual = habit.getScores().getTodayValue();
|
||||
assertThat(actual, closeTo(0.655747, E));
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
@Test
|
||||
public void test_getValue()
|
||||
{
|
||||
toggle(0, 20);
|
||||
check(0, 20);
|
||||
|
||||
double expectedValues[] = {
|
||||
0.655747,
|
||||
@@ -124,7 +124,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
@Test
|
||||
public void test_getValueWithSkip()
|
||||
{
|
||||
toggle(0, 20);
|
||||
check(0, 20);
|
||||
addSkip(5);
|
||||
addSkip(10);
|
||||
addSkip(11);
|
||||
@@ -161,7 +161,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
@Test
|
||||
public void test_getValueWithSkip2()
|
||||
{
|
||||
toggle(5);
|
||||
check(5);
|
||||
addSkip(4);
|
||||
|
||||
double[] expectedValues = {
|
||||
@@ -180,7 +180,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
@Test
|
||||
public void test_getValues()
|
||||
{
|
||||
toggle(0, 20);
|
||||
check(0, 20);
|
||||
|
||||
Timestamp today = DateUtils.getToday();
|
||||
Timestamp from = today.minus(4);
|
||||
@@ -214,7 +214,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
values.add(NO);
|
||||
values.add(NO);
|
||||
}
|
||||
toggle(values);
|
||||
check(values);
|
||||
assertThat(habit.getScores().getTodayValue(), closeTo(2/3.0, E));
|
||||
|
||||
// Missing 2 repetitions out of 4 per week, the score should converge to 50%
|
||||
@@ -249,7 +249,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
values.add(NO);
|
||||
values.add(YES_MANUAL);
|
||||
}
|
||||
toggle(values);
|
||||
check(values);
|
||||
assertThat(habit.getScores().getTodayValue(), closeTo(1.0, 1e-3));
|
||||
}
|
||||
|
||||
@@ -259,18 +259,18 @@ public class ScoreListTest extends BaseUnitTest
|
||||
// Daily habits should achieve at least 99% in 3 months
|
||||
habit = fixtures.createEmptyHabit();
|
||||
habit.setFrequency(Frequency.DAILY);
|
||||
for (int i = 0; i < 90; i++) toggle(i);
|
||||
for (int i = 0; i < 90; i++) check(i);
|
||||
assertThat(habit.getScores().getTodayValue(), greaterThan(0.99));
|
||||
|
||||
// Weekly habits should achieve at least 99% in 9 months
|
||||
habit = fixtures.createEmptyHabit();
|
||||
habit.setFrequency(Frequency.WEEKLY);
|
||||
for (int i = 0; i < 39; i++) toggle(7 * i);
|
||||
for (int i = 0; i < 39; i++) check(7 * i);
|
||||
assertThat(habit.getScores().getTodayValue(), greaterThan(0.99));
|
||||
|
||||
// Monthly habits should achieve at least 99% in 18 months
|
||||
habit.setFrequency(new Frequency(1, 30));
|
||||
for (int i = 0; i < 18; i++) toggle(30 * i);
|
||||
for (int i = 0; i < 18; i++) check(30 * i);
|
||||
assertThat(habit.getScores().getTodayValue(), greaterThan(0.99));
|
||||
}
|
||||
|
||||
@@ -292,7 +292,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
{
|
||||
assertThat(habit.getScores().getTodayValue(), closeTo(0.0, E));
|
||||
|
||||
toggle(0, 2);
|
||||
check(0, 2);
|
||||
assertThat(habit.getScores().getTodayValue(), closeTo(0.101149, E));
|
||||
|
||||
habit.setFrequency(new Frequency(1, 2));
|
||||
@@ -324,36 +324,36 @@ public class ScoreListTest extends BaseUnitTest
|
||||
assertThat(writer.toString(), equalTo(expectedCSV));
|
||||
}
|
||||
|
||||
private void toggle(final int offset)
|
||||
private void check(final int offset)
|
||||
{
|
||||
RepetitionList reps = habit.getRepetitions();
|
||||
Timestamp today = DateUtils.getToday();
|
||||
reps.toggle(today.minus(offset));
|
||||
reps.setValue(today.minus(offset), YES_MANUAL);
|
||||
}
|
||||
|
||||
private void toggle(final int from, final int to)
|
||||
private void check(final int from, final int to)
|
||||
{
|
||||
RepetitionList reps = habit.getRepetitions();
|
||||
Timestamp today = DateUtils.getToday();
|
||||
|
||||
for (int i = from; i < to; i++)
|
||||
reps.toggle(today.minus(i));
|
||||
reps.setValue(today.minus(i), YES_MANUAL);
|
||||
}
|
||||
|
||||
private void toggle(ArrayList<Integer> values)
|
||||
private void check(ArrayList<Integer> values)
|
||||
{
|
||||
RepetitionList reps = habit.getRepetitions();
|
||||
Timestamp today = DateUtils.getToday();
|
||||
for (int i = 0; i < values.size(); i++)
|
||||
if (values.get(i) == YES_MANUAL)
|
||||
reps.toggle(today.minus(i));
|
||||
reps.setValue(today.minus(i), YES_MANUAL);
|
||||
}
|
||||
|
||||
private void addSkip(final int day)
|
||||
{
|
||||
RepetitionList reps = habit.getRepetitions();
|
||||
Timestamp today = DateUtils.getToday();
|
||||
reps.toggle(today.minus(day), Checkmark.SKIP);
|
||||
reps.setValue(today.minus(day), Checkmark.SKIP);
|
||||
}
|
||||
|
||||
private void checkScoreValues(double[] expectedValues)
|
||||
|
||||
Reference in New Issue
Block a user