File: main_test.go

package info (click to toggle)
golang-collectd 0.3.0%2Bgit20181025.f80706d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 312 kB
  • sloc: makefile: 3
file content (86 lines) | stat: -rw-r--r-- 1,828 bytes parent folder | download | duplicates (3)
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
package api // import "collectd.org/api"

import (
	"testing"
)

func TestParseIdentifier(t *testing.T) {
	cases := []struct {
		Input string
		Want  Identifier
	}{
		{
			Input: "example.com/golang/gauge",
			Want: Identifier{
				Host:   "example.com",
				Plugin: "golang",
				Type:   "gauge",
			},
		},
		{
			Input: "example.com/golang-foo/gauge-bar",
			Want: Identifier{
				Host:           "example.com",
				Plugin:         "golang",
				PluginInstance: "foo",
				Type:           "gauge",
				TypeInstance:   "bar",
			},
		},
		{
			Input: "example.com/golang-a-b/gauge-b-c",
			Want: Identifier{
				Host:           "example.com",
				Plugin:         "golang",
				PluginInstance: "a-b",
				Type:           "gauge",
				TypeInstance:   "b-c",
			},
		},
	}

	for i, c := range cases {
		if got, err := ParseIdentifier(c.Input); got != c.Want || err != nil {
			t.Errorf("case %d: got (%v, %v), want (%v, %v)", i, got, err, c.Want, nil)
		}
	}

	failures := []string{
		"example.com/golang",
		"example.com/golang/gauge/extra",
	}

	for _, c := range failures {
		if got, err := ParseIdentifier(c); err == nil {
			t.Errorf("got (%v, %v), want (%v, !%v)", got, err, Identifier{}, nil)
		}
	}
}

func TestIdentifierString(t *testing.T) {
	id := Identifier{
		Host:   "example.com",
		Plugin: "golang",
		Type:   "gauge",
	}

	cases := []struct {
		PluginInstance, TypeInstance string
		Want                         string
	}{
		{"", "", "example.com/golang/gauge"},
		{"foo", "", "example.com/golang-foo/gauge"},
		{"", "foo", "example.com/golang/gauge-foo"},
		{"foo", "bar", "example.com/golang-foo/gauge-bar"},
	}

	for _, c := range cases {
		id.PluginInstance = c.PluginInstance
		id.TypeInstance = c.TypeInstance

		got := id.String()
		if got != c.Want {
			t.Errorf("got %q, want %q", got, c.Want)
		}
	}
}