From 575bc4b135c60933c7b3d483078da31323618a14 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 6 Nov 2012 11:33:45 -0800 Subject: [PATCH] Added unit tests for subtasks helper functions --- .../astrid/subtasks/SubtasksHelper.java | 3 +- .../astrid/subtasks/SubtasksHelperTest.java | 59 +++++++++++++++++++ .../astrid/subtasks/SubtasksMovingTest.java | 8 +++ .../astrid/subtasks/SubtasksTestCase.java | 1 - 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/src/com/todoroo/astrid/subtasks/SubtasksHelperTest.java diff --git a/astrid/plugin-src/com/todoroo/astrid/subtasks/SubtasksHelper.java b/astrid/plugin-src/com/todoroo/astrid/subtasks/SubtasksHelper.java index e4530878c..99bc1ea49 100644 --- a/astrid/plugin-src/com/todoroo/astrid/subtasks/SubtasksHelper.java +++ b/astrid/plugin-src/com/todoroo/astrid/subtasks/SubtasksHelper.java @@ -85,7 +85,7 @@ public class SubtasksHelper { @SuppressWarnings("nls") public static Long[] getIdArray(String serializedTree) { ArrayList ids = new ArrayList(); - 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 idMap = buildIdMap(ids, localToRemote); + idMap.put(-1L, -1L); Node tree = AstridOrderedListUpdater.buildTreeModel(treeString, null); remapTree(tree, idMap); diff --git a/tests/src/com/todoroo/astrid/subtasks/SubtasksHelperTest.java b/tests/src/com/todoroo/astrid/subtasks/SubtasksHelperTest.java new file mode 100644 index 000000000..18c7b8aa4 --- /dev/null +++ b/tests/src/com/todoroo/astrid/subtasks/SubtasksHelperTest.java @@ -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); + } + +} diff --git a/tests/src/com/todoroo/astrid/subtasks/SubtasksMovingTest.java b/tests/src/com/todoroo/astrid/subtasks/SubtasksMovingTest.java index 897b05421..0aeb30978 100644 --- a/tests/src/com/todoroo/astrid/subtasks/SubtasksMovingTest.java +++ b/tests/src/com/todoroo/astrid/subtasks/SubtasksMovingTest.java @@ -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() { diff --git a/tests/src/com/todoroo/astrid/subtasks/SubtasksTestCase.java b/tests/src/com/todoroo/astrid/subtasks/SubtasksTestCase.java index bd0112b56..90bacfc1e 100644 --- a/tests/src/com/todoroo/astrid/subtasks/SubtasksTestCase.java +++ b/tests/src/com/todoroo/astrid/subtasks/SubtasksTestCase.java @@ -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 {