From 28e40cb84a2123b6cf6aca81b64553ed387be2f6 Mon Sep 17 00:00:00 2001 From: WheeskyJack <32392032+WheeskyJack@users.noreply.github.com> Date: Sun, 25 Aug 2024 19:41:19 +0530 Subject: [PATCH] Update tree_test.go with TestTreeFindNode test case this adds test case for func (t Tree) Find(name string) Node --- mux/tree_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/mux/tree_test.go b/mux/tree_test.go index 884a334..a84a4b8 100644 --- a/mux/tree_test.go +++ b/mux/tree_test.go @@ -43,3 +43,39 @@ func TestTreeMatch(t *testing.T) { t.Fatalf("route did not match expected %s (%s)", "pl/blog/comments/123/new", commentNew.Name()) } } + +func TestTreeFindNode(t *testing.T) { + blog := NewNode("blog", 0) + + search := NewNode("search", blog.MaxParamsSize()) + page := NewNode("page", blog.MaxParamsSize()) + posts := NewNode("posts", blog.MaxParamsSize()) + + blog.WithChildren(blog.Tree().withNode(search).sort()) + blog.WithChildren(blog.Tree().withNode(page).sort()) + blog.WithChildren(blog.Tree().withNode(posts).sort()) + + blog.WithChildren(blog.Tree().Compile()) + + tests := []struct { + name string + input string + expected Node + }{ + {"Find existing node 1", "search", search}, + {"Find existing node 2", "page", page}, + {"Find existing node 3", "posts", posts}, + {"Find non-existing node", "comments", nil}, + {"Find with empty name", "", nil}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := blog.Tree().Find(tt.input) + if result != tt.expected { + t.Errorf("expected %v, got %v", tt.expected, result) + } + }) + } +} +