1
0
bind/lib_test.go
2025-09-03 23:52:33 +02:00

95 lines
1.8 KiB
Go

package bind_test
import (
"testing"
"code.squareroundforest.org/arpio/bind"
)
func TestLib(t *testing.T) {
t.Run("field", func(t *testing.T) {
t.Run("type", func(t *testing.T) {
t.Run("not from input", func(t *testing.T) {
v := struct{Foo int}{42}
f := bind.FieldValues(v)
if len(f) != 1 || f[0].Type() != bind.Int {
t.Fatal()
}
})
t.Run("nil", func(t *testing.T) {
f := bind.NamedValue("foo", nil)
if f.Type() != bind.Any {
t.Fatal()
}
})
t.Run("circular type", func(t *testing.T) {
type p *p
var v p
v = &v
f := bind.NamedValue("foo", v)
if f.Type() != bind.Any {
t.Fatal()
}
})
t.Run("direct", func(t *testing.T) {
f := bind.NamedValue("foo", 42)
if f.Type() != bind.Int {
t.Fatal()
}
})
t.Run("unpacked", func(t *testing.T) {
var v int
f := bind.NamedValue("foo", &v)
if f.Type() != bind.Int {
t.Fatal()
}
})
})
t.Run("name and path", func(t *testing.T) {
t.Run("returns path", func(t *testing.T) {
f := bind.ValueByPath([]string{"Foo", "Bar"}, 42)
p := f.Path()
if len(p) != 2 || p[0] != "Foo" || p[1] != "Bar" {
t.Fatal()
}
})
t.Run("cannot change path", func(t *testing.T) {
f := bind.ValueByPath([]string{"Foo", "Bar"}, 42)
p := f.Path()
p[1] = "Baz"
p = f.Path()
if p[1] != "Bar" {
t.Fatal()
}
})
t.Run("has name", func(t *testing.T) {
f := bind.NamedValue("foo-bar", 42)
if f.Name() != "foo-bar" {
t.Fatal()
}
})
t.Run("has path", func(t *testing.T) {
f := bind.ValueByPath([]string{"Foo", "Bar"}, 42)
if f.Name() != "foo-bar" {
t.Fatal()
}
})
t.Run("no name and no path", func(t *testing.T) {
f := bind.ValueByPath(nil, 42)
if f.Name() != "" {
t.Fatal()
}
})
})
})
}