-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
89 lines (80 loc) · 1.3 KB
/
validator_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
package metrics
import (
"testing"
"go.withmatt.com/metrics/internal/assert"
)
func TestValidateIdentSuccess(t *testing.T) {
for _, s := range []string{
"a",
"_9:8",
`:foo:bar`,
`some.foo`,
`aB`,
} {
assert.Equal(t, MustIdent(s).String(), s)
}
}
func TestValidateIdentError(t *testing.T) {
for _, s := range []string{
"",
"1abc",
"a{}",
"a b",
"a=b",
"ü",
"🍖",
} {
assert.Panics(t, func() { MustIdent(s) })
}
}
func TestValidateValueSuccess(t *testing.T) {
for _, s := range []string{
"",
"1abc",
"a{}",
"a b",
"a=b",
"ü",
"🍖",
`\n`,
`\\`,
`foo\nbar`,
`foo\"bar`,
`foo\\bar`,
} {
assert.Equal(t, MustValue(s).String(), s)
}
}
func TestValidateValueError(t *testing.T) {
for _, s := range []string{
`"`,
"\n",
`\`,
"foo\nbar",
`foo"bar`,
`foo\`,
`foo\bar`,
} {
assert.Panics(t, func() { MustValue(s) })
}
}
func BenchmarkValidate(b *testing.B) {
b.Run("MustIdent", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
MustIdent(`go_memstats_mspan_inuse_bytes`)
}
})
b.Run("validateIdent", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
validateIdent(`go_memstats_mspan_inuse_bytes`)
}
})
b.Run("MustValue", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
MustValue(`some.other.value`)
}
})
}