impl move scheduled to todo when schedule triggers

master
Bel LaPointe 2021-12-31 16:26:52 -05:00
parent 76008647c8
commit 88affea0e8
2 changed files with 88 additions and 0 deletions

View File

@ -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
}
}
}
}

View File

@ -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)
}
})
}
}