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) {
|
2019-06-15 23:28:29 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = Annotate(ctx, "a", "foo")
|
|
|
|
ctx = Annotate(ctx, "b", "bar")
|
|
|
|
ctx = Annotate(ctx, "b", "BAR")
|
2019-02-08 23:44:20 +00:00
|
|
|
|
2019-06-15 23:28:29 +00:00
|
|
|
annotations := Annotations(ctx)
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-06-15 23:28:29 +00:00
|
|
|
massert.Length(annotations, 2),
|
|
|
|
massert.HasValue(annotations, Annotation{Key: "a", Value: "foo"}),
|
|
|
|
massert.HasValue(annotations, Annotation{Key: "b", Value: "BAR"}),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
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{
|
2019-06-15 23:28:29 +00:00
|
|
|
{Key: 0, Value: "zero"},
|
|
|
|
{Key: 1, Value: "one"},
|
|
|
|
{Key: A(2), Value: "two"},
|
|
|
|
{Key: B(2), Value: "TWO"},
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Equal(map[string]string{
|
2019-06-15 23:28:29 +00:00
|
|
|
"0": "zero",
|
|
|
|
"1": "one",
|
|
|
|
"mctx.A(2)": "two",
|
|
|
|
"mctx.B(2)": "TWO",
|
2019-02-09 19:08:30 +00:00
|
|
|
}, aa.StringMap()),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|