cacheinterface/options_test.go

96 lines
2.0 KiB
Go

package cache
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"meow.tf/go/cacheinterface/v2/encoder"
"net/url"
"time"
)
var _ = Describe("Cache Test", func() {
Context("Query parsing", func() {
type testOpts struct {
Encoder encoder.Encoder
String string
Slice []string
Duration time.Duration
}
var (
opts testOpts
v url.Values
)
BeforeEach(func() {
opts = testOpts{}
v = url.Values{}
})
It("Should parse custom interfaces", func() {
v.Set("encoder", "json")
err := decodeQuery(v, &opts)
Expect(err).To(BeNil())
Expect(opts.Encoder).To(Equal(encoder.JSON))
})
It("Should parse strings", func() {
v.Set("string", "testing")
err := decodeQuery(v, &opts)
Expect(err).To(BeNil())
Expect(opts.String).To(Equal("testing"))
})
It("Should parse slices", func() {
v["slice"] = []string{"bla", "bla"}
err := decodeQuery(v, &opts)
Expect(err).To(BeNil())
Expect(opts.Slice).To(Equal([]string{"bla", "bla"}))
})
It("Should parse durations", func() {
v.Set("duration", "5m")
err := decodeQuery(v, &opts)
Expect(err).To(BeNil())
Expect(opts.Duration).To(Equal(5 * time.Minute))
})
})
Context("Defaults", func() {
type testOpts struct {
String string `default:"Test"`
Slice []string `default:"bla,bla"`
Duration time.Duration `default:"5m"`
Test bool `default:"true"`
}
var (
opts testOpts
v url.Values
)
BeforeEach(func() {
opts = testOpts{}
v = url.Values{}
})
It("Should assign defaults to strings", func() {
err := decodeQuery(v, &opts)
Expect(err).To(BeNil())
Expect(opts.String).To(Equal("Test"))
})
It("Should assign defaults to string slices", func() {
err := decodeQuery(v, &opts)
Expect(err).To(BeNil())
Expect(opts.Slice).To(Equal([]string{"bla", "bla"}))
})
It("Should assign defaults to durations", func() {
err := decodeQuery(v, &opts)
Expect(err).To(BeNil())
Expect(opts.Duration).To(Equal(5 * time.Minute))
})
})
})