2019-02-08 23:44:20 +00:00
|
|
|
package mctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-26 15:08:03 +00:00
|
|
|
"reflect"
|
2019-02-08 23:44:20 +00:00
|
|
|
. "testing"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2023-12-26 15:08:03 +00:00
|
|
|
wantAA := Annotations{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "BAR",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(wantAA, aa) {
|
|
|
|
t.Fatalf("%#v != %#v", wantAA, aa)
|
|
|
|
}
|
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
|
2023-12-26 15:08:03 +00:00
|
|
|
got := Annotations{
|
2021-02-07 23:04:41 +00:00
|
|
|
0: "zero",
|
|
|
|
1: "one",
|
|
|
|
A(2): "two",
|
|
|
|
B(2): "TWO",
|
2023-12-26 15:08:03 +00:00
|
|
|
}.StringMap()
|
|
|
|
|
|
|
|
want := map[string]string{
|
|
|
|
"0": "zero",
|
|
|
|
"1": "one",
|
|
|
|
"mctx.A(2)": "two",
|
|
|
|
"mctx.B(2)": "TWO",
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 15:08:03 +00:00
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
|
|
t.Fatalf("%#v != %#v", want, got)
|
|
|
|
}
|
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)
|
|
|
|
|
2023-12-26 15:08:03 +00:00
|
|
|
got := aa.StringMap()
|
|
|
|
want := map[string]string{
|
2019-02-09 19:08:30 +00:00
|
|
|
"0": "ZERO",
|
|
|
|
"1": "ONE",
|
|
|
|
"2": "TWO",
|
2023-12-26 15:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
|
|
t.Fatalf("%#v != %#v", want, got)
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
|
|
|
}
|