-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathattributes_test.go
129 lines (119 loc) · 2.6 KB
/
attributes_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
import (
"fmt"
"testing"
"github.com/stretchr/testify/suite"
)
type testAttributes map[string]any
func (ta testAttributes) Get(key string) (v any, ok bool) {
v, ok = ta[key]
return
}
type AttributesTestSuite struct {
suite.Suite
}
func (suite *AttributesTestSuite) testAttributesAccessor() AttributesAccessor {
return testAttributes{
"value": 123,
"untypedNil": nil,
"emptyMap": map[string]any{},
"nestedMap": map[string]any{
"value": 123,
"nestedMap": map[string]any{
"value": 123,
},
"nestedAttributes": AttributesAccessor(testAttributes{
"value": 123,
}),
},
"nestedAttributes": AttributesAccessor(testAttributes{
"value": 123,
"nestedMap": map[string]any{
"value": 123,
},
"nestedAttributes": AttributesAccessor(testAttributes{
"value": 123,
}),
}),
}
}
func (suite *AttributesTestSuite) TestGetAttribute() {
testCases := []struct {
keys []string
expectedValue int
expectedOK bool
}{
{
keys: nil,
},
{
keys: []string{"missing"},
},
{
keys: []string{"untypedNil"},
},
{
keys: []string{"untypedNil", "value"},
},
{
keys: []string{"value"},
expectedValue: 123,
expectedOK: true,
},
{
keys: []string{"emptyMap"},
},
{
keys: []string{"emptyMap", "value"},
},
{
keys: []string{"nestedMap"},
},
{
keys: []string{"nestedMap", "missing"},
},
{
keys: []string{"nestedMap", "value"},
expectedValue: 123,
expectedOK: true,
},
{
keys: []string{"nestedMap", "nestedMap", "missing"},
},
{
keys: []string{"nestedMap", "nestedMap", "value"},
expectedValue: 123,
expectedOK: true,
},
{
keys: []string{"nestedMap", "nestedAttributes", "value"},
expectedValue: 123,
expectedOK: true,
},
{
keys: []string{"nestedAttributes", "nestedMap", "missing"},
},
{
keys: []string{"nestedAttributes", "nestedMap", "value"},
expectedValue: 123,
expectedOK: true,
},
{
keys: []string{"nestedAttributes", "nestedAttributes", "value"},
expectedValue: 123,
expectedOK: true,
},
}
for _, testCase := range testCases {
suite.Run(fmt.Sprintf("%v", testCase.keys), func() {
actual, ok := GetAttribute[int](suite.testAttributesAccessor(), testCase.keys...)
suite.Equal(testCase.expectedValue, actual)
suite.Equal(testCase.expectedOK, ok)
})
}
}
func TestAttributes(t *testing.T) {
suite.Run(t, new(AttributesTestSuite))
}