-
Notifications
You must be signed in to change notification settings - Fork 6
/
group_test.go
108 lines (81 loc) · 2.65 KB
/
group_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
package main
import (
"fmt"
"regexp"
"slices"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
var spaceRe = regexp.MustCompile(`\s+`)
var lowerSnakeRe = regexp.MustCompile(`^[a-z]+(?:_[_a-z]+)?$`)
func checkLowerSnake(t *testing.T, name, value string) {
t.Helper()
if !lowerSnakeRe.MatchString(value) {
t.Errorf("%s is %q, want match for %q", name, value, lowerSnakeRe.String())
}
}
func checkSingleField(t *testing.T, g *group, f field) {
t.Helper()
checkLowerSnake(t, "Name", f.Name())
checkLowerSnake(t, "MetricName", f.MetricName())
if wantPrefix := fmt.Sprintf("%s_", g.name); !strings.HasPrefix(f.MetricName(), wantPrefix) {
t.Errorf("Metric name %q must start with %q", f.MetricName(), wantPrefix)
} else if strings.HasPrefix(f.MetricName(), wantPrefix+wantPrefix) {
t.Errorf("Metric name %q has double %q prefix", f.MetricName(), wantPrefix)
}
wantHelp := spaceRe.ReplaceAllLiteralString(f.Help(), " ")
wantHelp = strings.TrimSpace(wantHelp)
wantHelp = strings.TrimSuffix(wantHelp, ".")
if diff := cmp.Diff(f.Help(), wantHelp); diff != "" {
t.Errorf("Help text not normalized (-got +want):\n%s", diff)
}
}
func checkGroupFields[T field](t *testing.T, g *group, add func(*testing.T, field), fields []T) {
t.Helper()
var names []string
for idx, f := range fields {
names = append(names, f.Name())
t.Run(fmt.Sprintf("%d:%s", idx, f.Name()), func(t *testing.T) {
add(t, f)
checkSingleField(t, g, f)
})
}
sortedNames := slices.Clone(names)
sort.Strings(sortedNames)
if diff := cmp.Diff(names, sortedNames); diff != "" {
t.Errorf("Fields not sorted (-got +want):\n%s", diff)
}
}
func checkGroup(t *testing.T, g *group) {
t.Helper()
fieldNameMap := map[string]struct{}{}
metricNameMap := map[string]struct{}{}
checkLowerSnake(t, "infoMetricName", g.infoMetricName)
metricNameMap[g.infoMetricName] = struct{}{}
process := func(t *testing.T, f field) {
if _, ok := fieldNameMap[f.Name()]; ok {
t.Errorf("Duplicate field name %q", f.Name())
}
if _, ok := metricNameMap[f.MetricName()]; ok {
t.Errorf("Duplicate metric name %q", f.MetricName())
}
fieldNameMap[f.Name()] = struct{}{}
metricNameMap[f.MetricName()] = struct{}{}
}
checkGroupFields(t, g, process, g.keyFields)
checkGroupFields(t, g, process, g.textFields)
checkGroupFields(t, g, process, g.numericFields)
}
func TestGroupValidation(t *testing.T) {
kindRe := regexp.MustCompile(`^[a-z]{2,6}$`)
for _, g := range allGroups {
t.Run(g.name.String(), func(t *testing.T) {
if !kindRe.MatchString(g.name.String()) {
t.Errorf("Group kind is %q, want match for %q", g.name.String(), kindRe.String())
}
checkGroup(t, g)
})
}
}