From fc12e0550da81389baa7441742bc20aebcc2a23e Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sun, 9 Jan 2022 10:17:44 -0500 Subject: [PATCH] add root.equals and todo.equals --- pttodo/root.go | 14 ++++++++++++++ pttodo/root_test.go | 6 ++++++ pttodo/todo.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/pttodo/root.go b/pttodo/root.go index 0ba432c..a4b2723 100644 --- a/pttodo/root.go +++ b/pttodo/root.go @@ -6,6 +6,20 @@ type Root struct { 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() { for i := len(root.Scheduled) - 1; i >= 0; i-- { if root.Scheduled[i].Triggered() { diff --git a/pttodo/root_test.go b/pttodo/root_test.go index 7fc6546..cad3efb 100644 --- a/pttodo/root_test.go +++ b/pttodo/root_test.go @@ -24,6 +24,8 @@ func TestJSONRoot(t *testing.T) { t.Fatal(err) } else if fmt.Sprint(root) != fmt.Sprint(root2) { t.Fatal(root, root2) + } else if !root.Equals(root2) { + t.Fatal(root2) } } @@ -95,6 +97,8 @@ func TestRootMoveScheduledToTodo(t *testing.T) { got.MoveScheduledToTodo() if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) { 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) if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) { 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) } } diff --git a/pttodo/todo.go b/pttodo/todo.go index a7d99ee..062b2cd 100644 --- a/pttodo/todo.go +++ b/pttodo/todo.go @@ -43,3 +43,37 @@ func (todo *Todo) UnmarshalYAML(unmarshal func(interface{}) error) error { alt := (*Alt)(todo) 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 +}