From d3c805beceb69477eb969b9ea50989180c070059 Mon Sep 17 00:00:00 2001 From: Javier Guajardo Date: Sat, 5 Nov 2022 01:06:33 +0200 Subject: [PATCH] Add function to count list --- lists.go | 7 ++++++- tests/lists_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lists.go b/lists.go index b848a6d..682a5fd 100644 --- a/lists.go +++ b/lists.go @@ -11,7 +11,7 @@ func GetList(options ...string) ([]string, error) { var listName string if len(options) == 0 { - return nil, fmt.Errorf("not enough arguments") + return []string{}, fmt.Errorf("not enough arguments") } listName = options[0] @@ -140,3 +140,8 @@ func ReplaceList(listName string, index int, newElement string) (bool, error) { return true, nil } + +func CountList(listName string) (int, error) { + list, err := GetList(listName) + return len(list), err +} \ No newline at end of file diff --git a/tests/lists_test.go b/tests/lists_test.go index ecfdab8..383d551 100644 --- a/tests/lists_test.go +++ b/tests/lists_test.go @@ -153,4 +153,19 @@ func TestLists(t *testing.T) { } } }) + + t.Run("Test Count List", func(t *testing.T) { + var listName string = "countable" + var expected int = 3 + sider.RPush(listName, "1 item") + sider.RPush(listName, "2 item") + sider.RPush(listName, "3 item") + if count, err := sider.CountList(listName); err != nil { + t.Errorf("Error CountList function %v", err) + } else { + if count != expected { + t.Errorf("Value count %v != %v", count, expected) + } + } + }) }