-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
group_test.go
87 lines (80 loc) · 2.75 KB
/
group_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
package res
import (
"testing"
)
// Test parseGroup panics when expected
func TestParseGroup(t *testing.T) {
tbl := []struct {
Group string
Pattern string
Panic bool
}{
// Valid groups
{"", "test", false},
{"test", "test", false},
{"test", "test.$foo", false},
{"test.${foo}", "test.$foo", false},
{"${foo}", "test.$foo", false},
{"${foo}.test", "test.$foo", false},
{"${foo}${bar}", "test.$foo.$bar", false},
{"${bar}${foo}", "test.$foo.$bar", false},
{"${foo}.${bar}", "test.$foo.$bar.>", false},
{"${foo}${foo}", "test.$foo.$bar", false},
// Invalid groups
{"$", "test.$foo", true},
{"${", "test.$foo", true},
{"${foo", "test.$foo", true},
{"${}", "test.$foo", true},
{"${$foo}", "test.$foo", true},
{"${bar}", "test.$foo", true},
}
for _, l := range tbl {
func() {
defer func() {
if r := recover(); r != nil {
if !l.Panic {
t.Errorf("expected parseGroup not to panic, but it did:\n\tpanic : %s\n\tgroup : %s\n\tpattern : %s", r, l.Group, l.Pattern)
}
} else {
if l.Panic {
t.Errorf("expected parseGroup to panic, but it didn't\n\tgroup : %s\n\tpattern : %s", l.Group, l.Pattern)
}
}
}()
parseGroup(l.Group, l.Pattern)
}()
}
}
// Test group toString
func TestGroupToString(t *testing.T) {
tbl := []struct {
Group string
Pattern string
ResourceName string
Tokens []string
Expected string
}{
{"", "test", "test", []string{"test"}, "test"},
{"test", "test", "test", []string{"test"}, "test"},
{"foo", "test", "test", []string{"test"}, "foo"},
{"test", "test.$foo", "test.42", []string{"test", "42"}, "test"},
{"test.${foo}", "test.$foo", "test.42", []string{"test", "42"}, "test.42"},
{"bar.${foo}", "test.$foo", "test.42", []string{"test", "42"}, "bar.42"},
{"${foo}", "test.$foo", "test.42", []string{"test", "42"}, "42"},
{"${foo}.test", "test.$foo", "test.42", []string{"test", "42"}, "42.test"},
{"${foo}${bar}", "test.$foo.$bar", "test.42.baz", []string{"test", "42", "baz"}, "42baz"},
{"${bar}${foo}", "test.$foo.$bar", "test.42.baz", []string{"test", "42", "baz"}, "baz42"},
{"${foo}.${bar}", "test.$foo.$bar.>", "test.42.baz.extra.all", []string{"test", "42", "baz", "extra", "all"}, "42.baz"},
{"${foo}${foo}", "test.$foo.$bar", "test.42.baz", []string{"test", "42", "baz"}, "4242"},
{"${foo}.test.this.${bar}", "test.$foo.$bar", "test.42.baz", []string{"test", "42", "baz"}, "42.test.this.baz"},
}
for _, l := range tbl {
func() {
gr := parseGroup(l.Group, l.Pattern)
wid := gr.toString(l.ResourceName, l.Tokens)
if wid != l.Expected {
t.Errorf("expected parseGroup(%#v, %#v).toString(%#v, %#v) to return:\n\t%#v\nbut got:\n\t%#v", l.Group, l.Pattern, l.ResourceName, l.Tokens, l.Expected, wid)
}
}()
}
}