add root.equals and todo.equals

Bel LaPointe 2022-01-09 10:17:44 -05:00
parent 05f5244cd1
commit fc12e0550d
3 changed files with 54 additions and 0 deletions

View File

@ -6,6 +6,20 @@ type Root struct {
Done []Todo Done []Todo
} }
func (root Root) Equals(other Root) bool {
for i, slice := range [][2][]Todo{
[2][]Todo{root.Todo, other.Todo},
[2][]Todo{root.Scheduled, other.Scheduled},
[2][]Todo{root.Done, other.Done},
} {
_ = i
if !equalTodoSlices(slice[0], slice[1]) {
return false
}
}
return true
}
func (root *Root) MoveScheduledToTodo() { func (root *Root) MoveScheduledToTodo() {
for i := len(root.Scheduled) - 1; i >= 0; i-- { for i := len(root.Scheduled) - 1; i >= 0; i-- {
if root.Scheduled[i].Triggered() { if root.Scheduled[i].Triggered() {

View File

@ -24,6 +24,8 @@ func TestJSONRoot(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} else if fmt.Sprint(root) != fmt.Sprint(root2) { } else if fmt.Sprint(root) != fmt.Sprint(root2) {
t.Fatal(root, root2) t.Fatal(root, root2)
} else if !root.Equals(root2) {
t.Fatal(root2)
} }
} }
@ -95,6 +97,8 @@ func TestRootMoveScheduledToTodo(t *testing.T) {
got.MoveScheduledToTodo() got.MoveScheduledToTodo()
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) { if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) {
t.Fatalf("want \n\t%+v, got \n\t%+v", want, got) t.Fatalf("want \n\t%+v, got \n\t%+v", want, got)
} else if !got.Equals(want) {
t.Fatalf("want \n\t%+v, got \n\t%+v", want, got)
} }
}) })
} }
@ -144,6 +148,8 @@ todo:
root0.MergeIn(root1) root0.MergeIn(root1)
if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) { if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) {
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0) t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
} else if !root0.Equals(rootWant) {
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
} }
} }

View File

@ -43,3 +43,37 @@ func (todo *Todo) UnmarshalYAML(unmarshal func(interface{}) error) error {
alt := (*Alt)(todo) alt := (*Alt)(todo)
return unmarshal(alt) return unmarshal(alt)
} }
func (todo Todo) Equals(other Todo) bool {
if !equalTodoSlices(todo.Subtasks, other.Subtasks) {
return false
}
if todo.TS != other.TS {
return false
}
if todo.Tags != other.Tags {
return false
}
if todo.Schedule != other.Schedule {
return false
}
if todo.Details != other.Details {
return false
}
if todo.Todo != other.Todo {
return false
}
return true
}
func equalTodoSlices(a, b []Todo) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !a[i].Equals(b[i]) {
return false
}
}
return true
}