mediocre-go-lib/mctx/annotate_test.go
Brian Picciano 9567a98606 Move massert into internal, and simplify it a lot
The previous massert isn't that useful compared to testify, but it's
nice to have some assertion helpers in the repo which don't require a
sub-dependency.
2023-12-26 16:27:41 +01:00

77 lines
1.3 KiB
Go

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)
}
}