-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattribs_test.go
93 lines (84 loc) · 3.8 KB
/
attribs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package golog
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestAttribs_AppendUnique(t *testing.T) {
// stringVals := func(valPrefix string, keys ...string) (nv Attribs) {
// for _, key := range keys {
// nv = append(nv, Int{Key: key, Val: valPrefix + key})
// }
// return nv
// }
// mergedStringVals := func(keyVals ...string) (nv Attribs) {
// for i := 0; i < len(keyVals); i += 2 {
// nv = append(nv, String{Key: keyVals[i], Val: keyVals[i+1]})
// }
// return nv
// }
intValsEqual := func(left, right Attribs) bool {
if len(left) != len(right) {
return false
}
for i := range left {
l, ok := left[i].(Int)
if !ok {
return false
}
r, ok := right[i].(Int)
if !ok {
return false
}
if l != r {
return false
}
}
return true
}
type args struct {
left Attribs
right Attribs
}
tests := []struct {
name string
args args
want Attribs
}{
{name: "nil / nil", args: args{left: nil, right: nil}, want: nil},
{name: "empty / empty", args: args{left: Attribs{}, right: Attribs{}}, want: Attribs{}},
// {name: "nil / 1", args: args{left: nil, right: stringVals("b", "1")}, want: stringVals("b", "1")},
// {name: "1 / nil", args: args{left: stringVals("a", "1"), right: nil}, want: stringVals("a", "1")},
// {name: "1 / 2", args: args{left: stringVals("a", "1"), right: stringVals("b", "2")}, want: mergedStringVals("1", "a1", "2", "b2")},
// {name: "1 2 / 1", args: args{left: stringVals("a", "1", "2"), right: stringVals("b", "2")}, want: mergedStringVals("1", "a1", "2", "b2")},
// {name: "1 / 1 2", args: args{left: stringVals("a", "1"), right: stringVals("b", "1", "2")}, want: mergedStringVals("1", "b1", "2", "b2")},
// {name: "1 2 3 / 1", args: args{left: stringVals("a", "1", "2", "3"), right: stringVals("b", "1")}, want: mergedStringVals("2", "a2", "3", "a3", "1", "b1")},
// {name: "1 2 3 / 2", args: args{left: stringVals("a", "1", "2", "3"), right: stringVals("b", "2")}, want: mergedStringVals("1", "a1", "3", "a3", "2", "b2")},
// {name: "1 2 3 / 3", args: args{left: stringVals("a", "1", "2", "3"), right: stringVals("b", "3")}, want: mergedStringVals("1", "a1", "2", "a2", "3", "b3")},
// {name: "1 / 1 2 3", args: args{left: stringVals("a", "1"), right: stringVals("b", "1", "2", "3")}, want: mergedStringVals("1", "b1", "2", "b2", "3", "b3")},
// {name: "2 / 1 2 3", args: args{left: stringVals("a", "2"), right: stringVals("b", "1", "2", "3")}, want: mergedStringVals("1", "b1", "2", "b2", "3", "b3")},
// {name: "3 / 1 2 3", args: args{left: stringVals("a", "3"), right: stringVals("b", "1", "2", "3")}, want: mergedStringVals("1", "b1", "2", "b2", "3", "b3")},
// {name: "nil / Values{nil}", args: args{left: nil, right: Attribs{nil}}, want: nil},
// {name: "Values{nil} / nil", args: args{left: Attribs{nil}, right: nil}, want: nil},
// {name: "Values{nil} / Values{nil, nil}", args: args{left: Attribs{nil}, right: Attribs{nil, nil}}, want: nil},
// {name: "1 / 2 nil", args: args{left: stringVals("a", "1"), right: append(stringVals("b", "2"), nil)}, want: mergedStringVals("1", "a1", "2", "b2")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.args.left.AppendUnique(tt.args.right...)
if !intValsEqual(got, tt.want) {
t.Errorf("MergeAttribs() = %v, want %v", got, tt.want)
}
})
}
}
func TestAttribFromContext(t *testing.T) {
_, ok := AttribFromContext[Int](context.Background(), "invalid")
require.False(t, ok, "attrib not added to context")
ctx := ContextWithAttribs(context.Background(), Int{Key: "Int", Val: 1})
_, ok = AttribFromContext[Int](ctx, "invalid")
require.False(t, ok, "attrib not added to context")
attrib, ok := AttribFromContext[Int](ctx, "Int")
require.True(t, ok, "attrib added to context")
require.Equal(t, attrib, Int{Key: "Int", Val: 1})
}