2019-01-14 01:08:38 +00:00
|
|
|
package merr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
. "testing"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestKV(t *T) {
|
2019-01-24 21:45:38 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Nil(WithValue(nil, "foo", "bar", true)),
|
|
|
|
massert.Nil(WithValue(nil, "foo", "bar", false)),
|
|
|
|
massert.Nil(GetValue(nil, "foo")),
|
|
|
|
massert.Len(KV(nil).KV(), 0),
|
|
|
|
))
|
|
|
|
|
2019-01-25 22:32:53 +00:00
|
|
|
er := New("foo", "bar", "baz")
|
2019-01-14 01:08:38 +00:00
|
|
|
kv := KV(er).KV()
|
|
|
|
massert.Fatal(t, massert.Comment(
|
|
|
|
massert.All(
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Len(kv, 3),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal("foo", kv["err"]),
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Equal("baz", kv["bar"]),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal(true,
|
|
|
|
strings.HasPrefix(kv["errSrc"].(string), "merr/kv_test.go:")),
|
|
|
|
),
|
|
|
|
"kv: %#v", kv,
|
|
|
|
))
|
|
|
|
|
|
|
|
type A string
|
|
|
|
type B string
|
|
|
|
type C string
|
|
|
|
|
|
|
|
er = WithValue(er, "invisible", "you can't see me", false)
|
|
|
|
er = WithValue(er, A("k"), "1", true)
|
|
|
|
kv = KV(er).KV()
|
|
|
|
massert.Fatal(t, massert.Comment(
|
|
|
|
massert.All(
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Len(kv, 4),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal("foo", kv["err"]),
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Equal("baz", kv["bar"]),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal(true,
|
|
|
|
strings.HasPrefix(kv["errSrc"].(string), "merr/kv_test.go:")),
|
|
|
|
massert.Equal("1", kv["k"]),
|
|
|
|
),
|
|
|
|
"kv: %#v", kv,
|
|
|
|
))
|
|
|
|
|
|
|
|
er = WithValue(er, B("k"), "2", true)
|
|
|
|
kv = KV(er).KV()
|
|
|
|
massert.Fatal(t, massert.Comment(
|
|
|
|
massert.All(
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Len(kv, 5),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal("foo", kv["err"]),
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Equal("baz", kv["bar"]),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal(true,
|
|
|
|
strings.HasPrefix(kv["errSrc"].(string), "merr/kv_test.go:")),
|
|
|
|
massert.Equal("1", kv["merr.A(k)"]),
|
|
|
|
massert.Equal("2", kv["merr.B(k)"]),
|
|
|
|
),
|
|
|
|
"kv: %#v", kv,
|
|
|
|
))
|
|
|
|
|
|
|
|
er = WithValue(er, C("k"), "3", true)
|
|
|
|
kv = KV(er).KV()
|
|
|
|
massert.Fatal(t, massert.Comment(
|
|
|
|
massert.All(
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Len(kv, 6),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal("foo", kv["err"]),
|
2019-01-25 22:32:53 +00:00
|
|
|
massert.Equal("baz", kv["bar"]),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal(true,
|
|
|
|
strings.HasPrefix(kv["errSrc"].(string), "merr/kv_test.go:")),
|
|
|
|
massert.Equal("1", kv["merr.A(k)"]),
|
|
|
|
massert.Equal("2", kv["merr.B(k)"]),
|
|
|
|
massert.Equal("3", kv["merr.C(k)"]),
|
|
|
|
),
|
|
|
|
"kv: %#v", kv,
|
|
|
|
))
|
2019-02-03 01:18:39 +00:00
|
|
|
|
|
|
|
er = WithKV(er, map[string]interface{}{"D": 4, "k": 5})
|
|
|
|
kv = KV(er).KV()
|
|
|
|
massert.Fatal(t, massert.Comment(
|
|
|
|
massert.All(
|
|
|
|
massert.Len(kv, 8),
|
|
|
|
massert.Equal("foo", kv["err"]),
|
|
|
|
massert.Equal("baz", kv["bar"]),
|
|
|
|
massert.Equal(true,
|
|
|
|
strings.HasPrefix(kv["errSrc"].(string), "merr/kv_test.go:")),
|
|
|
|
massert.Equal("1", kv["merr.A(k)"]),
|
|
|
|
massert.Equal("2", kv["merr.B(k)"]),
|
|
|
|
massert.Equal("3", kv["merr.C(k)"]),
|
|
|
|
massert.Equal(4, kv["D"]),
|
|
|
|
massert.Equal(5, kv["merr.kvKey(k)"]),
|
|
|
|
),
|
|
|
|
"kv: %#v", kv,
|
|
|
|
))
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|