1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-25 21:41:20 +00:00
go_helpers/fieldcollection/fieldcollection_test.go
Knut Ahlers 6efbc2dd11
Add validation to fieldcollection
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-04-03 19:03:45 +02:00

58 lines
1.1 KiB
Go

package fieldcollection
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExpect(t *testing.T) {
var f *FieldCollection
assert.NoError(t, f.Expect())
assert.Error(t, f.Expect("foo"))
}
func TestFieldCollectionNilClone(*testing.T) {
var f *FieldCollection
f.Clone()
}
func TestFieldCollectionNilDataGet(t *testing.T) {
var f *FieldCollection
for name, fn := range map[string]func(name string) bool{
"bool": f.CanBool,
"duration": f.CanDuration,
"int64": f.CanInt64,
"string": f.CanString,
"stringSlice": f.CanStringSlice,
} {
assert.False(t, fn("foo"), "%s key is available", name)
}
}
func TestGet(t *testing.T) {
f := &FieldCollection{}
_, err := f.Get("foo")
assert.Error(t, err)
f.Set("foo", "bar")
_, err = f.Get("bar")
assert.ErrorIs(t, err, ErrValueNotSet)
v, err := f.Get("foo")
assert.NoError(t, err)
assert.Equal(t, "bar", v)
}
func TestKeys(t *testing.T) {
f := FieldCollectionFromData(map[string]any{
"foo": "bar",
})
assert.Equal(t, []string{"foo"}, f.Keys())
}
func TestSetOnNew(*testing.T) {
f := new(FieldCollection)
f.Set("foo", "bar")
}