add root MergeIn

Bel LaPointe 2022-01-01 17:43:20 -05:00
parent eb41f065a5
commit cd5ce0f0df
2 changed files with 67 additions and 0 deletions

View File

@ -18,3 +18,21 @@ func (root *Root) MoveScheduledToTodo() {
}
}
}
func (root *Root) MergeIn(root2 Root) {
for _, listPair := range [][2]*[]Todo{
[2]*[]Todo{&root.Todo, &root2.Todo},
[2]*[]Todo{&root.Scheduled, &root2.Scheduled},
[2]*[]Todo{&root.Done, &root2.Done},
} {
for _, candidate := range *listPair[1] {
found := false
for i := range *listPair[0] {
found = found || ((*listPair[0])[i].Todo == candidate.Todo)
}
if !found {
*listPair[0] = append(*listPair[0], candidate)
}
}
}
}

View File

@ -6,6 +6,8 @@ import (
"strconv"
"testing"
"time"
yaml "gopkg.in/yaml.v2"
)
func TestJSONRoot(t *testing.T) {
@ -96,3 +98,50 @@ func TestRootMoveScheduledToTodo(t *testing.T) {
})
}
}
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)
}
}