From f6c9296952b062803a4991d3f97f76d61b0cc488 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Fri, 3 Mar 2017 17:42:27 -0500 Subject: Add garbage collection feature * Add `todo gc`, which will delete all archived todos. * `NextId` will now take the first available id, rather than just the MaxId + 1. --- todolist/todo_list_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'todolist/todo_list_test.go') diff --git a/todolist/todo_list_test.go b/todolist/todo_list_test.go index 8a40b14..eb0bc84 100644 --- a/todolist/todo_list_test.go +++ b/todolist/todo_list_test.go @@ -8,8 +8,42 @@ import ( func TestNextId(t *testing.T) { assert := assert.New(t) + todo := &Todo{Subject: "testing", Completed: false, Archived: false} list := &TodoList{} assert.Equal(1, list.NextId()) + list.Add(todo) + assert.Equal(2, list.NextId()) +} + +func TestNextIdWhenTodoDeleted(t *testing.T) { + assert := assert.New(t) + todo := &Todo{Subject: "testing", Completed: false, Archived: false} + todo2 := &Todo{Subject: "testing2", Completed: false, Archived: false} + todo3 := &Todo{Subject: "testing3", Completed: false, Archived: false} + list := &TodoList{} + + list.Add(todo) + list.Add(todo2) + list.Add(todo3) + + list.Delete(2) + assert.Equal(2, list.NextId()) + list.Add(todo2) + assert.Equal(4, list.NextId()) + list.Delete(1) + assert.Equal(1, list.NextId()) +} + +func TestMaxId(t *testing.T) { + assert := assert.New(t) + todo := &Todo{Subject: "testing", Completed: false, Archived: false} + todo2 := &Todo{Subject: "testing 2", Completed: false, Archived: false} + list := &TodoList{} + assert.Equal(0, list.MaxId()) + list.Add(todo) + assert.Equal(1, list.MaxId()) + list.Add(todo2) + assert.Equal(2, list.MaxId()) } func TestIndexOf(t *testing.T) { @@ -77,3 +111,20 @@ func TestUncomplete(t *testing.T) { list.Uncomplete(2) assert.Equal(false, list.FindById(2).Completed) } + +func TestGarbageCollect(t *testing.T) { + assert := assert.New(t) + list := &TodoList{} + todo := &Todo{Subject: "testing", Completed: false, Archived: true} + todo2 := &Todo{Subject: "testing2", Completed: false, Archived: false} + todo3 := &Todo{Subject: "testing3", Completed: false, Archived: true} + list.Add(todo) + list.Add(todo2) + list.Add(todo3) + + list.GarbageCollect() + + assert.Equal(len(list.Data), 1) + assert.Equal(1, list.NextId()) + assert.Equal(2, list.MaxId()) +} -- cgit v1.3