micro/options/options_test.go

87 lines
1.4 KiB
Go
Raw Normal View History

package options_test
import (
"testing"
"go.unistack.org/micro/v4/codec"
"go.unistack.org/micro/v4/options"
)
func TestAddress(t *testing.T) {
var err error
type s struct {
Address []string
}
src := &s{}
var opts []options.Option
opts = append(opts, options.Address("host:port"))
for _, o := range opts {
if err = o(src); err != nil {
t.Fatal(err)
}
}
if src.Address[0] != "host:port" {
t.Fatal("failed to set Address")
}
}
func TestCodecs(t *testing.T) {
var err error
type s struct {
Codecs map[string]codec.Codec
}
src := &s{}
var opts []options.Option
c := codec.NewCodec()
opts = append(opts, options.Codecs("text/html", c))
for _, o := range opts {
if err = o(src); err != nil {
t.Fatal(err)
}
}
for k, v := range src.Codecs {
if k != "text/html" || v != c {
continue
}
return
}
t.Fatalf("failed to set Codecs")
}
func TestLabels(t *testing.T) {
type str1 struct {
Labels []string
}
type str2 struct {
Labels []interface{}
}
x1 := &str1{}
if err := options.Labels("one", "two")(x1); err != nil {
t.Fatal(err)
}
if len(x1.Labels) != 2 {
t.Fatal("failed to set labels")
}
x2 := &str2{}
if err := options.Labels("key", "val")(x2); err != nil {
t.Fatal(err)
}
if len(x2.Labels) != 2 {
t.Fatal("failed to set labels")
}
if x2.Labels[0] != "key" {
t.Fatal("failed to set labels")
}
}