Add tests for New

This commit is contained in:
Dan Jones 2025-04-21 11:25:56 -05:00
commit 40ed936c47
3 changed files with 62 additions and 0 deletions

44
ezcache_test.go Normal file
View file

@ -0,0 +1,44 @@
package ezcache_test
import (
"fmt"
"testing"
"time"
"codeberg.org/danjones000/ezcache"
"github.com/stretchr/testify/assert"
)
var fetcher ezcache.Fetcher[uint8, string] = func(key uint8) (string, error) { return fmt.Sprintf("%d", key), nil }
func TestNewHappy(t *testing.T) {
cache, err := ezcache.New(fetcher, 1)
assert.NoError(t, err)
assert.NotNil(t, cache)
}
func TestNewNilFetcher(t *testing.T) {
cache, err := ezcache.New[uint8, string](nil, 1)
assert.ErrorIs(t, err, ezcache.ErrInvalidFetcher)
assert.Nil(t, cache)
}
func TestNewBadExpiry(tt *testing.T) {
testcases := []struct {
name string
exp time.Duration
}{
{"zero", 0},
{"negative", -5},
}
for _, tc := range testcases {
tt.Run(tc.name, func(t *testing.T) {
cache, err := ezcache.New(fetcher, tc.exp)
assert.ErrorIs(t, err, ezcache.ErrInvalidExpiry)
assert.Nil(t, cache)
})
}
}

8
go.mod
View file

@ -1,3 +1,11 @@
module codeberg.org/danjones000/ezcache module codeberg.org/danjones000/ezcache
go 1.23.7 go 1.23.7
require github.com/stretchr/testify v1.10.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum Normal file
View file

@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=