Added unit tests for subtasks helper functions

pull/14/head
Sam Bosley 12 years ago
parent a0d7ac427c
commit 575bc4b135

@ -85,7 +85,7 @@ public class SubtasksHelper {
@SuppressWarnings("nls")
public static Long[] getIdArray(String serializedTree) {
ArrayList<Long> ids = new ArrayList<Long>();
String[] digitsOnly = serializedTree.split("\\D+");
String[] digitsOnly = serializedTree.split("[\\[\\],\\s]"); // Split on [ ] , or whitespace chars
for (String idString : digitsOnly) {
try {
if (!TextUtils.isEmpty(idString))
@ -108,6 +108,7 @@ public class SubtasksHelper {
private static String convertIdTree(String treeString, boolean localToRemote) {
Long[] ids = getIdArray(treeString);
HashMap<Long, Long> idMap = buildIdMap(ids, localToRemote);
idMap.put(-1L, -1L);
Node tree = AstridOrderedListUpdater.buildTreeModel(treeString, null);
remapTree(tree, idMap);

@ -0,0 +1,59 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.Task;
public class SubtasksHelperTest extends SubtasksTestCase {
private Task A, B, C, D, E, F;
@Override
protected void setUp() throws Exception {
super.setUp();
createTasks();
updater.initializeFromSerializedTree(null, filter, DEFAULT_SERIALIZED_TREE);
}
private Task createTask(String title, long remoteId) {
Task t = new Task();
t.setValue(Task.TITLE, title);
t.setValue(Task.REMOTE_ID, remoteId);
PluginServices.getTaskService().save(t);
return t;
}
private void createTasks() {
A = createTask("A", 6); // Local id 1
B = createTask("B", 4); // Local id 2
C = createTask("C", 3); // Local id 3
D = createTask("D", 1); // Local id 4
E = createTask("E", 2); // Local id 5
F = createTask("F", 5); // Local id 6
}
private static final Long[] EXPECTED_ORDER = {-1L, 1L, 2L, 3L, 4L, 5L, 6L };
public void testOrderedIdArray() {
Long[] ids = SubtasksHelper.getIdArray(DEFAULT_SERIALIZED_TREE);
assertEquals(EXPECTED_ORDER.length, ids.length);
for (int i = 0; i < EXPECTED_ORDER.length; i++) {
assertEquals(EXPECTED_ORDER[i], ids[i]);
}
}
// Default order: "[-1, [1, 2, [3, 4]], 5, 6]"
private static String EXPECTED_REMOTE = "[-1, [6, 4, [3, 1]], 2, 5]".replaceAll("\\s", "");
public void testLocalToRemoteIdMapping() {
String mapped = SubtasksHelper.convertTreeToRemoteIds(DEFAULT_SERIALIZED_TREE).replaceAll("\\s", "");
assertEquals(EXPECTED_REMOTE, mapped);
}
private static String EXPECTED_LOCAL = "[-1, [4, 5, [3, 2]], 6, 1]".replaceAll("\\s", "");
public void testRemoteToLocalIdMapping() {
String mapped = SubtasksHelper.convertTreeToLocalIds(DEFAULT_SERIALIZED_TREE).replaceAll("\\s", "");
assertEquals(EXPECTED_LOCAL, mapped);
}
}

@ -12,6 +12,14 @@ public class SubtasksMovingTest extends SubtasksTestCase {
super.setUp();
createTasks();
updater.initializeFromSerializedTree(null, filter, DEFAULT_SERIALIZED_TREE);
// Assert initial state is correct
expectParentAndPosition(A, null, 0);
expectParentAndPosition(B, A, 0);
expectParentAndPosition(C, A, 1);
expectParentAndPosition(D, C, 0);
expectParentAndPosition(E, null, 1);
expectParentAndPosition(F, null, 2);
}
private void createTasks() {

@ -27,7 +27,6 @@ public class SubtasksTestCase extends DatabaseTestCase {
* F
*/
public static final String DEFAULT_SERIALIZED_TREE = "[-1, [1, 2, [3, 4]], 5, 6]".replaceAll("\\s", "");
//"[{\"1\":[{\"2\":[]}, {\"3\":[{\"4\":[]}]}]}, {\"5\":[]}, {\"6\":[]}]".replaceAll("\\s", "");
@Override
protected void setUp() throws Exception {

Loading…
Cancel
Save