2019-02-08 23:44:20 +00:00
|
|
|
package mctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
. "testing"
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mtest/massert"
|
2019-02-08 23:44:20 +00:00
|
|
|
)
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
type testAnnotator [2]string
|
|
|
|
|
|
|
|
func (t testAnnotator) Annotate(aa Annotations) {
|
|
|
|
aa[t[0]] = t[1]
|
|
|
|
}
|
|
|
|
|
2019-02-08 23:44:20 +00:00
|
|
|
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")
|
2021-02-07 23:04:41 +00:00
|
|
|
ctx = WithAnnotator(ctx, testAnnotator{"b", "BAR"})
|
|
|
|
|
|
|
|
aa := Annotations{}
|
|
|
|
EvaluateAnnotations(ctx, aa)
|
2019-02-08 23:44:20 +00:00
|
|
|
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2021-02-07 23:04:41 +00:00
|
|
|
massert.Equal(Annotations{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "BAR",
|
|
|
|
}, aa),
|
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
|
2021-02-07 23:04:41 +00:00
|
|
|
aa := Annotations{
|
|
|
|
0: "zero",
|
|
|
|
1: "one",
|
|
|
|
A(2): "two",
|
|
|
|
B(2): "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)
|
2021-02-07 23:04:41 +00:00
|
|
|
aa := Annotations{}
|
|
|
|
EvaluateAnnotations(ctx, aa)
|
|
|
|
|
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",
|
2021-02-07 23:04:41 +00:00
|
|
|
}, aa.StringMap()).Assert()
|
2019-02-08 23:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|