aboutsummaryrefslogtreecommitdiff
path: root/pkg/metricsx/metricsx_test.go
blob: 5bcf8aba838759b08e8b4b859d1356b71bf55b9f (plain)
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
package metricsx

import "testing"

func TestSplitName(t *testing.T) {
	for _, c := range [][3]string{
		// valid
		{`test`, `test`, ``},
		{`test{}`, `test`, ``},
		{`test{test=""}`, `test`, `test=""`},
		{`test{test="{}"}`, `test`, `test="{}"`},

		// invalid
		{``, ``, ``},
		{`test{`, `test{`, ``},
		{`test}`, `test}`, ``},
		{`test}{`, `test}{`, ``},
		{`test{}{}`, `test`, `}{`},
		{`test{}{test}`, `test`, `}{test`},
		{`test{test{}}`, `test`, `test{}`},
		{`test{}{test{}}`, `test`, `}{test{}`},
	} {
		name, xbase, xarg := c[0], c[1], c[2]
		if base, arg := splitName(name); base != xbase || arg != xarg {
			t.Errorf("split %#q: expected (%#q, %#q), got (%#q, %#q)", name, xbase, xarg, base, arg)
		}
	}
}

func TestFormatName(t *testing.T) {
	for _, c := range [][]string{
		{`test{}`, `test`, ``},
		{`test{a="1"}`, `test`, `a="1"`},
		{`test{a="1",b="2"}`, `test`, `a="1"`, `b`, `2`},
		{`test{a="1",b="2"}`, `test`, `a="1",b="2"`},
		{`test{a="1",b="2",c="3"}`, `test`, `a="1"`, `b`, `2`, `c`, `3`},
		{`test{a="1",b="2",c="3"}`, `test`, `a="1",b="2"`, `c`, `3`},
	} {
		exp, base, arg, args := c[0], c[1], c[2], c[3:]
		if act := formatName(base, arg, args...); act != exp {
			t.Errorf("format (%#q, %#q, %#q, %#q): expected %#q, got %#q", exp, base, arg, args, exp, act)
		}
	}
}