Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/util/subset/subset_test.go
5318 views
1
package subset
2
3
import (
4
"testing"
5
6
"github.com/stretchr/testify/require"
7
)
8
9
func TestAssert(t *testing.T) {
10
tt := []struct {
11
name string
12
source, target string
13
expect string
14
}{
15
// Plain values
16
{
17
name: "values match",
18
source: `true`,
19
target: `true`,
20
expect: "",
21
},
22
{
23
name: "values mismatch",
24
source: `true`,
25
target: `false`,
26
expect: "true != false",
27
},
28
{
29
name: "type mismatch",
30
source: `true`,
31
target: `5`,
32
expect: "type mismatch: bool != int",
33
},
34
35
// Arrays
36
{
37
name: "arrays match",
38
source: `[1, 2, 3]`,
39
target: `[1, 2, 3]`,
40
expect: "",
41
},
42
{
43
name: "arrays mismatch",
44
source: `[1, 2, 3]`,
45
target: `[1, 2, 4]`,
46
expect: "element 2: 3 != 4",
47
},
48
{
49
name: "array element type mismatch",
50
source: `[1, 2, 3]`,
51
target: `[1, 2, true]`,
52
expect: "element 2: type mismatch: int != bool",
53
},
54
55
// Maps
56
{
57
name: "maps match",
58
source: `{"hello": "world"}`,
59
target: `{"hello": "world"}`,
60
expect: "",
61
},
62
{
63
name: "maps mismatch",
64
source: `{"hello": "world", "year": 2000}`,
65
target: `{"hello": "world", "year": 2001}`,
66
expect: "year: 2000 != 2001",
67
},
68
{
69
name: "maps subset",
70
source: `{"hello": "world"}`,
71
target: `{"hello": "world", "year": 2001}`,
72
expect: "",
73
},
74
{
75
name: "maps type mismatch",
76
source: `{"hello": "world", "year": 2000}`,
77
target: `{"hello": "world", "year": "yes"}`,
78
expect: "year: type mismatch: int != string",
79
},
80
}
81
82
for _, tc := range tt {
83
t.Run(tc.name, func(t *testing.T) {
84
err := YAMLAssert([]byte(tc.source), []byte(tc.target))
85
if tc.expect == "" {
86
require.NoError(t, err)
87
} else {
88
require.EqualError(t, err, tc.expect)
89
}
90
})
91
}
92
}
93
94