mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Add missing tests for RepetitionList and Habit
This commit is contained in:
@@ -43,7 +43,7 @@ public class ToggleRepetitionCommand extends Command
|
||||
@Override
|
||||
public void execute()
|
||||
{
|
||||
habit.getRepetitions().toggleTimestamp(timestamp);
|
||||
habit.getRepetitions().toggle(timestamp);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
||||
@@ -322,7 +322,13 @@ public class Habit
|
||||
public synchronized boolean isCompletedToday()
|
||||
{
|
||||
int todayCheckmark = getCheckmarks().getTodayValue();
|
||||
if (isNumerical()) return todayCheckmark >= data.targetValue;
|
||||
if (isNumerical())
|
||||
{
|
||||
if(getTargetType() == AT_LEAST)
|
||||
return todayCheckmark >= data.targetValue;
|
||||
else
|
||||
return todayCheckmark <= data.targetValue;
|
||||
}
|
||||
else return (todayCheckmark != UNCHECKED);
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,7 @@ public abstract class RepetitionList
|
||||
*/
|
||||
@Nullable
|
||||
public abstract Repetition getOldest();
|
||||
|
||||
@Nullable
|
||||
/**
|
||||
* Returns the newest repetition in the list.
|
||||
@@ -184,8 +185,11 @@ public abstract class RepetitionList
|
||||
* @return the repetition that has been added or removed.
|
||||
*/
|
||||
@NonNull
|
||||
public Repetition toggleTimestamp(long timestamp)
|
||||
public Repetition toggle(long timestamp)
|
||||
{
|
||||
if(habit.isNumerical())
|
||||
throw new IllegalStateException("habit must NOT be numerical");
|
||||
|
||||
timestamp = DateUtils.getStartOfDay(timestamp);
|
||||
Repetition rep = getByTimestamp(timestamp);
|
||||
|
||||
@@ -207,4 +211,15 @@ public abstract class RepetitionList
|
||||
*/
|
||||
@NonNull
|
||||
public abstract long getTotalCount();
|
||||
|
||||
public void toggle(long timestamp, int value)
|
||||
{
|
||||
if(!habit.isNumerical())
|
||||
throw new IllegalStateException("habit must be numerical");
|
||||
|
||||
Repetition rep = getByTimestamp(timestamp);
|
||||
if(rep != null) remove(rep);
|
||||
add(new Repetition(timestamp, value));
|
||||
habit.invalidateNewerThan(timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class HabitFixtures
|
||||
81, 83, 89, 90, 91, 95, 102, 103, 108, 109, 120};
|
||||
|
||||
for (int mark : marks)
|
||||
habit.getRepetitions().toggleTimestamp(today - mark * day);
|
||||
habit.getRepetitions().toggle(today - mark * day);
|
||||
|
||||
return habit;
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class HabitFixtures
|
||||
long timestamp = DateUtils.getStartOfToday();
|
||||
for (boolean c : NON_DAILY_HABIT_CHECKS)
|
||||
{
|
||||
if (c) habit.getRepetitions().toggleTimestamp(timestamp);
|
||||
if (c) habit.getRepetitions().toggle(timestamp);
|
||||
timestamp -= DateUtils.millisecondsInOneDay;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,17 @@ package org.isoron.uhabits.core.models;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.isoron.uhabits.core.utils.DateUtils.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HabitTest extends BaseUnitTest
|
||||
{
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
{
|
||||
@@ -76,4 +81,49 @@ public class HabitTest extends BaseUnitTest
|
||||
h.clearReminder();
|
||||
assertThat(h.hasReminder(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_isCompleted() throws Exception
|
||||
{
|
||||
Habit h = modelFactory.buildHabit();
|
||||
assertFalse(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getStartOfToday());
|
||||
assertTrue(h.isCompletedToday());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_isCompleted_numerical() throws Exception
|
||||
{
|
||||
Habit h = modelFactory.buildHabit();
|
||||
h.setType(Habit.NUMBER_HABIT);
|
||||
h.setTargetType(Habit.AT_LEAST);
|
||||
h.setTargetValue(100.0);
|
||||
assertFalse(h.isCompletedToday());
|
||||
|
||||
h.getRepetitions().toggle(getStartOfToday(), 200);
|
||||
assertTrue(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getStartOfToday(), 100);
|
||||
assertTrue(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getStartOfToday(), 50);
|
||||
assertFalse(h.isCompletedToday());
|
||||
|
||||
h.setTargetType(Habit.AT_MOST);
|
||||
h.getRepetitions().toggle(getStartOfToday(), 200);
|
||||
assertFalse(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getStartOfToday(), 100);
|
||||
assertTrue(h.isCompletedToday());
|
||||
h.getRepetitions().toggle(getStartOfToday(), 50);
|
||||
assertTrue(h.isCompletedToday());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testURI() throws Exception
|
||||
{
|
||||
assertTrue(habitList.isEmpty());
|
||||
Habit h = modelFactory.buildHabit();
|
||||
habitList.add(h);
|
||||
assertThat(h.getId(), equalTo(0L));
|
||||
assertThat(h.getUriString(),
|
||||
equalTo("content://org.isoron.uhabits/habit/0"));
|
||||
}
|
||||
}
|
||||
@@ -27,9 +27,12 @@ import org.junit.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.Is.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class RepetitionListTest extends BaseUnitTest
|
||||
@@ -58,14 +61,15 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
today = DateUtils.getStartOfToday();
|
||||
day = DateUtils.millisecondsInOneDay;
|
||||
|
||||
reps.toggleTimestamp(today - 3 * day);
|
||||
reps.toggleTimestamp(today - 2 * day);
|
||||
reps.toggleTimestamp(today);
|
||||
reps.toggleTimestamp(today - 7 * day);
|
||||
reps.toggleTimestamp(today - 5 * day);
|
||||
reps.toggle(today - 3 * day);
|
||||
reps.toggle(today - 2 * day);
|
||||
reps.toggle(today);
|
||||
reps.toggle(today - 7 * day);
|
||||
reps.toggle(today - 5 * day);
|
||||
|
||||
listener = mock(ModelObservable.Listener.class);
|
||||
reps.getObservable().addListener(listener);
|
||||
reset(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,7 +134,7 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
weekdayCount[month][week]++;
|
||||
monthCount[month]++;
|
||||
}
|
||||
reps.toggleTimestamp(day.getTimeInMillis());
|
||||
reps.toggle(day.getTimeInMillis());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,17 +159,33 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_toggleTimestamp()
|
||||
public void test_toggle()
|
||||
{
|
||||
assertThat(reps.containsTimestamp(today), equalTo(true));
|
||||
reps.toggleTimestamp(today);
|
||||
assertThat(reps.containsTimestamp(today), equalTo(false));
|
||||
assertTrue(reps.containsTimestamp(today));
|
||||
reps.toggle(today);
|
||||
assertFalse(reps.containsTimestamp(today));
|
||||
verify(listener).onModelChange();
|
||||
reset(listener);
|
||||
|
||||
assertThat(reps.containsTimestamp(today - day), equalTo(false));
|
||||
reps.toggleTimestamp(today - day);
|
||||
assertThat(reps.containsTimestamp(today - day), equalTo(true));
|
||||
assertFalse(reps.containsTimestamp(today - day));
|
||||
reps.toggle(today - day);
|
||||
assertTrue(reps.containsTimestamp(today - day));
|
||||
verify(listener).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();
|
||||
reset(listener);
|
||||
|
||||
reps.toggle(today, 500);
|
||||
check = reps.getByTimestamp(today);
|
||||
assertNotNull(check);
|
||||
assertThat(check.getValue(), equalTo(500));
|
||||
verify(listener, times(2)).onModelChange();
|
||||
reset(listener);
|
||||
}
|
||||
}
|
||||
@@ -205,6 +205,6 @@ public class ScoreListTest extends BaseUnitTest
|
||||
long day = DateUtils.millisecondsInOneDay;
|
||||
|
||||
for (int i = from; i < to; i++)
|
||||
reps.toggleTimestamp(today - i * day);
|
||||
reps.toggle(today - i * day);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user