metrics/validator_test.go

62 lines
1011 B
Go
Raw Normal View History

2019-04-08 16:29:16 +03:00
package metrics
import (
"testing"
)
func TestValidateMetricSuccess(t *testing.T) {
f := func(s string) {
t.Helper()
if err := validateMetric(s); err != nil {
t.Fatalf("cannot validate %q: %s", s, err)
}
}
f("a")
f("_9:8")
f("a{}")
f(`a{foo="bar"}`)
f(`foo{bar="baz", x="y\"z"}`)
f(`foo{bar="b}az"}`)
2019-04-11 12:59:53 +03:00
f(`:foo:bar{bar="a",baz="b"}`)
2021-07-07 15:36:08 +03:00
f(`some.foo{bar="baz"}`)
2019-04-08 16:29:16 +03:00
}
func TestValidateMetricError(t *testing.T) {
f := func(s string) {
t.Helper()
if err := validateMetric(s); err == nil {
t.Fatalf("expecting non-nil error when validating %q", s)
}
}
f("")
f("{}")
// superflouos space
f("a ")
f(" a")
f(" a ")
f("a {}")
f("a{} ")
f("a{ }")
f(`a{foo ="bar"}`)
f(`a{ foo="bar"}`)
f(`a{foo= "bar"}`)
f(`a{foo="bar" }`)
f(`a{foo="bar" ,baz="a"}`)
// invalid tags
f("a{foo}")
f("a{=}")
f(`a{=""}`)
f(`a{`)
f(`a}`)
f(`a{foo=}`)
f(`a{foo="`)
f(`a{foo="}`)
f(`a{foo="bar",}`)
f(`a{foo="bar", x`)
f(`a{foo="bar", x=`)
f(`a{foo="bar", x="`)
f(`a{foo="bar", x="}`)
}