2019-02-08 23:44:20 +00:00
|
|
|
package mctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
. "testing"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAnnotate(t *T) {
|
|
|
|
parent := context.Background()
|
|
|
|
parent = Annotate(parent, "a", "foo")
|
|
|
|
parent = Annotate(parent, "b", "bar")
|
|
|
|
|
|
|
|
child := NewChild(parent, "child")
|
2019-02-09 19:08:30 +00:00
|
|
|
child = Annotate(child, "a", "Foo")
|
2019-02-08 23:44:20 +00:00
|
|
|
child = Annotate(child, "a", "FOO")
|
|
|
|
child = Annotate(child, "c", "BAZ")
|
|
|
|
parent = WithChild(parent, child)
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
parentAnnotations := Annotations(parent)
|
|
|
|
childAnnotations := Annotations(child)
|
2019-02-08 23:44:20 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Len(parentAnnotations, 2),
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Has(parentAnnotations, Annotation{Key: "a", Value: "foo"}),
|
|
|
|
massert.Has(parentAnnotations, Annotation{Key: "b", Value: "bar"}),
|
|
|
|
|
|
|
|
massert.Len(childAnnotations, 4),
|
|
|
|
massert.Has(childAnnotations, Annotation{Key: "a", Value: "foo"}),
|
|
|
|
massert.Has(childAnnotations, Annotation{Key: "b", Value: "bar"}),
|
|
|
|
massert.Has(childAnnotations,
|
|
|
|
Annotation{Key: "a", Path: []string{"child"}, Value: "FOO"}),
|
|
|
|
massert.Has(childAnnotations,
|
|
|
|
Annotation{Key: "c", Path: []string{"child"}, Value: "BAZ"}),
|
2019-02-08 23:44:20 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
func TestAnnotationsStringMap(t *T) {
|
2019-02-08 23:44:20 +00:00
|
|
|
type A int
|
|
|
|
type B int
|
2019-02-09 19:08:30 +00:00
|
|
|
aa := AnnotationSet{
|
|
|
|
{Key: 0, Path: nil, Value: "zero"},
|
|
|
|
{Key: 1, Path: nil, Value: "one"},
|
|
|
|
{Key: 1, Path: []string{"foo"}, Value: "ONE"},
|
|
|
|
{Key: A(2), Path: []string{"foo"}, Value: "two"},
|
|
|
|
{Key: B(2), Path: []string{"foo"}, Value: "TWO"},
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal(map[string]string{
|
|
|
|
"0": "zero",
|
|
|
|
"1(/)": "one",
|
|
|
|
"1(/foo)": "ONE",
|
|
|
|
"2(/foo)(mctx.A)": "two",
|
|
|
|
"2(/foo)(mctx.B)": "TWO",
|
|
|
|
}, aa.StringMap()),
|
|
|
|
massert.Equal(map[string]map[string]string{
|
|
|
|
"/": {
|
|
|
|
"0": "zero",
|
|
|
|
"1": "one",
|
|
|
|
},
|
|
|
|
"/foo": {
|
|
|
|
"1": "ONE",
|
|
|
|
"2(mctx.A)": "two",
|
|
|
|
"2(mctx.B)": "TWO",
|
|
|
|
},
|
|
|
|
}, aa.StringMapByPath()),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-02-08 23:44:20 +00:00
|
|
|
err := massert.Equal(map[string]string{
|
2019-02-09 19:08:30 +00:00
|
|
|
"0": "ZERO",
|
|
|
|
"1": "ONE",
|
|
|
|
"2": "TWO",
|
|
|
|
}, Annotations(ctx).StringMap()).Assert()
|
2019-02-08 23:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|