Skip to content

Commit

Permalink
Merge pull request #17 from jaavier/feature/count-list
Browse files Browse the repository at this point in the history
Add function to count list
  • Loading branch information
jaavier committed Nov 4, 2022
2 parents 58edd9a + d3c805b commit b208ea5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions tests/lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})
}

0 comments on commit b208ea5

Please sign in to comment.