pttodo/pttodo/root_test.go

148 lines
3.0 KiB
Go

package pttodo
import (
"encoding/json"
"fmt"
"strconv"
"testing"
"time"
yaml "gopkg.in/yaml.v2"
)
func TestJSONRoot(t *testing.T) {
root := Root{
Todo: []Todo{Todo{Todo: "todo", Details: "detail", Schedule: "teehee", TS: 1}},
Scheduled: []Todo{Todo{Todo: "scheduled", Details: "detail", Schedule: "teehee", TS: 1}},
Done: []Todo{Todo{Todo: "done", Details: "detail", Schedule: "teehee", TS: 1}},
}
var root2 Root
if b, err := json.Marshal(root); err != nil {
t.Fatal(err)
} else if err := json.Unmarshal(b, &root2); err != nil {
t.Fatal(err)
} else if fmt.Sprint(root) != fmt.Sprint(root2) {
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)
}
})
}
}
func TestMergeRoots(t *testing.T) {
root0yaml := `
todo:
- a
- b
- todo: c
- todo: d
tags: a
- exclusive to 0
`
root1yaml := `
todo:
- a
- b
- todo: c
- todo: d
tags: b
- exclusive to 1
`
rootWantyaml := `
todo:
- a
- b
- todo: c
- todo: d
tags: a
- exclusive to 0
- exclusive to 1
`
var root0, root1, rootWant Root
if err := yaml.Unmarshal([]byte(root0yaml), &root0); err != nil {
t.Fatal(err)
}
if err := yaml.Unmarshal([]byte(root1yaml), &root1); err != nil {
t.Fatal(err)
}
if err := yaml.Unmarshal([]byte(rootWantyaml), &rootWant); err != nil {
t.Fatal(err)
}
root0.MergeIn(root1)
if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) {
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
}
}