mediocre-go-lib/mctx/annotate_test.go

77 lines
1.3 KiB
Go
Raw Normal View History

package mctx
import (
"context"
"reflect"
. "testing"
)
type testAnnotator [2]string
func (t testAnnotator) Annotate(aa Annotations) {
aa[t[0]] = t[1]
}
func TestAnnotate(t *T) {
ctx := context.Background()
ctx = Annotate(ctx, "a", "foo")
ctx = Annotate(ctx, "b", "bar")
ctx = WithAnnotator(ctx, testAnnotator{"b", "BAR"})
aa := Annotations{}
EvaluateAnnotations(ctx, aa)
wantAA := Annotations{
"a": "foo",
"b": "BAR",
}
if !reflect.DeepEqual(wantAA, aa) {
t.Fatalf("%#v != %#v", wantAA, aa)
}
}
func TestAnnotationsStringMap(t *T) {
type A int
type B int
got := Annotations{
0: "zero",
1: "one",
A(2): "two",
B(2): "TWO",
}.StringMap()
want := map[string]string{
"0": "zero",
"1": "one",
"mctx.A(2)": "two",
"mctx.B(2)": "TWO",
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("%#v != %#v", want, got)
}
}
func TestMergeAnnotations(t *T) {
ctxA := Annotate(context.Background(), 0, "zero", 1, "one")
ctxA = Annotate(ctxA, 0, "ZERO")
ctxB := Annotate(context.Background(), 2, "two")
ctxB = Annotate(ctxB, 1, "ONE", 2, "TWO")
ctx := MergeAnnotations(ctxA, ctxB)
aa := Annotations{}
EvaluateAnnotations(ctx, aa)
got := aa.StringMap()
want := map[string]string{
"0": "ZERO",
"1": "ONE",
"2": "TWO",
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("%#v != %#v", want, got)
}
}