|
@@ -0,0 +1,100 @@
|
|
1
|
+package profile
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "testing"
|
|
5
|
+)
|
|
6
|
+
|
|
7
|
+func TestSampleIndexByName(t *testing.T) {
|
|
8
|
+ for _, c := range []struct {
|
|
9
|
+ desc string
|
|
10
|
+ sampleTypes []string
|
|
11
|
+ defaultSampleType string
|
|
12
|
+ index string
|
|
13
|
+ want int
|
|
14
|
+ wantError bool
|
|
15
|
+ }{
|
|
16
|
+ {
|
|
17
|
+ desc: "use last by default",
|
|
18
|
+ index: "",
|
|
19
|
+ want: 1,
|
|
20
|
+ sampleTypes: []string{"zero", "default"},
|
|
21
|
+ },
|
|
22
|
+ {
|
|
23
|
+ desc: "honour specified default",
|
|
24
|
+ index: "",
|
|
25
|
+ want: 1,
|
|
26
|
+ defaultSampleType: "default",
|
|
27
|
+ sampleTypes: []string{"zero", "default", "two"},
|
|
28
|
+ },
|
|
29
|
+ {
|
|
30
|
+ desc: "invalid default is ignored",
|
|
31
|
+ index: "",
|
|
32
|
+ want: 2,
|
|
33
|
+ defaultSampleType: "non-existent",
|
|
34
|
+ sampleTypes: []string{"zero", "one", "default"},
|
|
35
|
+ },
|
|
36
|
+ {
|
|
37
|
+ desc: "index by int",
|
|
38
|
+ index: "0",
|
|
39
|
+ want: 0,
|
|
40
|
+ sampleTypes: []string{"zero", "one", "two"},
|
|
41
|
+ },
|
|
42
|
+ {
|
|
43
|
+ desc: "index by int ignores default",
|
|
44
|
+ index: "0",
|
|
45
|
+ want: 0,
|
|
46
|
+ defaultSampleType: "default",
|
|
47
|
+ sampleTypes: []string{"zero", "default", "two"},
|
|
48
|
+ },
|
|
49
|
+ {
|
|
50
|
+ desc: "index by name",
|
|
51
|
+ index: "two",
|
|
52
|
+ want: 2,
|
|
53
|
+ sampleTypes: []string{"zero", "one", "two", "three"},
|
|
54
|
+ },
|
|
55
|
+ {
|
|
56
|
+ desc: "index by name ignores default",
|
|
57
|
+ index: "zero",
|
|
58
|
+ want: 0,
|
|
59
|
+ defaultSampleType: "default",
|
|
60
|
+ sampleTypes: []string{"zero", "default", "two"},
|
|
61
|
+ },
|
|
62
|
+ {
|
|
63
|
+ desc: "out of bound int causes error",
|
|
64
|
+ index: "100",
|
|
65
|
+ wantError: true,
|
|
66
|
+ sampleTypes: []string{"zero", "default"},
|
|
67
|
+ },
|
|
68
|
+ {
|
|
69
|
+ desc: "unknown name causes error",
|
|
70
|
+ index: "does not exist",
|
|
71
|
+ wantError: true,
|
|
72
|
+ sampleTypes: []string{"zero", "default"},
|
|
73
|
+ },
|
|
74
|
+ {
|
|
75
|
+ desc: "'inused_{x}' recognized for legacy '{x}'",
|
|
76
|
+ index: "inuse_zero",
|
|
77
|
+ want: 0,
|
|
78
|
+ sampleTypes: []string{"zero", "default"},
|
|
79
|
+ },
|
|
80
|
+ } {
|
|
81
|
+ p := &Profile{
|
|
82
|
+ DefaultSampleType: c.defaultSampleType,
|
|
83
|
+ SampleType: []*ValueType{},
|
|
84
|
+ }
|
|
85
|
+ for _, st := range c.sampleTypes {
|
|
86
|
+ p.SampleType = append(p.SampleType, &ValueType{Type: st, Unit: "milliseconds"})
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ got, err := p.SampleIndexByName(c.index)
|
|
90
|
+
|
|
91
|
+ switch {
|
|
92
|
+ case c.wantError && err == nil:
|
|
93
|
+ t.Errorf("%s: error should have been returned not index=%d, err=%v", c.desc, got, err)
|
|
94
|
+ case !c.wantError && err != nil:
|
|
95
|
+ t.Errorf("%s: unexpected got index=%d, err=%v; wanted index=%d, err=nil", c.desc, got, err, c.want)
|
|
96
|
+ case !c.wantError && got != c.want:
|
|
97
|
+ t.Errorf("%s: got index=%d, want index=%d", c.desc, got, c.want)
|
|
98
|
+ }
|
|
99
|
+ }
|
|
100
|
+}
|