From 88affea0e8e2710b459af5a1b1a16f0662d6aade Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 31 Dec 2021 16:26:52 -0500 Subject: [PATCH] impl move scheduled to todo when schedule triggers --- pttodo/root.go | 13 ++++++++ pttodo/root_test.go | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/pttodo/root.go b/pttodo/root.go index 8c74aea..eabba1d 100644 --- a/pttodo/root.go +++ b/pttodo/root.go @@ -5,3 +5,16 @@ type Root struct { Scheduled []Todo Done []Todo } + +func (root *Root) MoveScheduledToTodo() { + for i := len(root.Scheduled) - 1; i >= 0; i-- { + if root.Scheduled[i].Triggered() { + root.Todo = append(root.Todo, root.Scheduled[i]) + root.Scheduled[i] = root.Scheduled[len(root.Scheduled)-1] + root.Scheduled = root.Scheduled[:len(root.Scheduled)-1] + if i < len(root.Scheduled)-1 { + i += 1 + } + } + } +} diff --git a/pttodo/root_test.go b/pttodo/root_test.go index c70b8cd..c706c5b 100644 --- a/pttodo/root_test.go +++ b/pttodo/root_test.go @@ -3,7 +3,9 @@ package pttodo import ( "encoding/json" "fmt" + "strconv" "testing" + "time" ) func TestJSONRoot(t *testing.T) { @@ -21,3 +23,76 @@ func TestJSONRoot(t *testing.T) { t.Fatal(root, root2) } } + +func TestRootMoveScheduledToTodo(t *testing.T) { + scheduleFuture := Schedule(strconv.FormatInt(time.Now().Unix()+100, 10)) + schedulePast := Schedule(strconv.FormatInt(time.Now().Unix()-100, 10)) + cases := map[string]struct { + in Root + want Root + }{ + "empty": {}, + "no scheduled": { + in: Root{Todo: []Todo{Todo{}}}, + want: Root{Todo: []Todo{Todo{}}}, + }, + "one scheduled not ready": { + in: Root{Scheduled: []Todo{Todo{Schedule: scheduleFuture}}}, + want: Root{Scheduled: []Todo{Todo{Schedule: scheduleFuture}}}, + }, + "one scheduled ready": { + in: Root{Scheduled: []Todo{Todo{Schedule: schedulePast}}}, + want: Root{Todo: []Todo{Todo{Schedule: schedulePast}}}, + }, + "two scheduled one ready one not": { + in: Root{ + Scheduled: []Todo{ + Todo{Schedule: schedulePast}, + Todo{Schedule: scheduleFuture}, + }, + }, + want: Root{ + Todo: []Todo{ + Todo{Schedule: schedulePast}, + }, + Scheduled: []Todo{ + Todo{Schedule: scheduleFuture}, + }, + }, + }, + "two scheduled one not one ready": { + in: Root{ + Scheduled: []Todo{ + Todo{Schedule: scheduleFuture}, + Todo{Schedule: schedulePast}, + }, + }, + want: Root{ + Todo: []Todo{ + Todo{Schedule: schedulePast}, + }, + Scheduled: []Todo{ + Todo{Schedule: scheduleFuture}, + }, + }, + }, + } + + for name, d := range cases { + c := d + t.Run(name, func(t *testing.T) { + inB, _ := json.Marshal(c.in) + var got Root + json.Unmarshal(inB, &got) + + wantB, _ := json.Marshal(c.want) + var want Root + json.Unmarshal(wantB, &want) + + got.MoveScheduledToTodo() + if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) { + t.Fatalf("want \n\t%+v, got \n\t%+v", want, got) + } + }) + } +}