forked from digineo/go-uci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes_test.go
119 lines (105 loc) · 3.5 KB
/
types_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package uci
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnmangleSectionName(t *testing.T) {
tt := map[string]struct {
typ string
idx int
err string
}{
// simple test cases
"": {err: "implausible section selector: must be at least 5 characters long"},
"aa[0]": {err: "invalid syntax: section selector must start with @ sign"},
"@@[0]": {err: "invalid syntax: multiple @ signs found"},
"@@@@@@@@@@@": {err: "invalid syntax: multiple @ signs found"},
"@[[0]": {err: "invalid syntax: multiple open brackets found"},
"@][0]": {err: "invalid syntax: multiple closed brackets found"},
"@aa0]": {err: "invalid syntax: section selector must have format '@type[index]'"},
"@a[b]": {err: `invalid syntax: index must be numeric: strconv.Atoi: parsing "b": invalid syntax`},
// valid test cases
"@a[0]": {typ: "a", idx: 0},
"@a[4223]": {typ: "a", idx: 4223},
"@a[-1]": {typ: "a", idx: -1},
// longer types/indices
"@abcdEFGHijkl[-255]": {typ: "abcdEFGHijkl", idx: -255},
"@abcdEFGHijkl[0xff]": {err: `invalid syntax: index must be numeric: strconv.Atoi: parsing "0xff": invalid syntax`},
}
for input := range tt {
input, tc := input, tt[input]
t.Run(input, func(t *testing.T) {
assert := assert.New(t)
typ, idx, err := unmangleSectionName(input)
if tc.err != "" {
assert.EqualError(err, tc.err)
} else {
assert.NoError(err)
assert.Equal(tc.idx, idx)
assert.Equal(tc.typ, typ)
}
})
}
}
func TestConfigGet(t *testing.T) { //nolint:funlen
config, err := Parse("unnamed", tcUnnamedInput)
assert.NoError(t, err)
cases := []*section{
// for fun, tcUnnamedInput starts with a named section. for extra
// fun, tcUnnamedInput extends the named section at the end.
{"named", "foo", []*option{
newOption("pos", TypeOption, "3"), // gets overwritten by last section
newOption("unnamed", TypeOption, "0"),
newOption("list", TypeList, "0", "30"), // gets merged with last section
}},
// the @foo[0] selector only compares type (foo) and index (0)
{"@foo[0]", "foo", []*option{ // alias for "named"
newOption("pos", TypeOption, "3"),
newOption("unnamed", TypeOption, "0"),
newOption("list", TypeList, "0", "30"),
}},
{"@foo[1]", "foo", []*option{
newOption("pos", TypeOption, "1"),
newOption("unnamed", TypeOption, "1"),
newOption("list", TypeOption, "10"),
}},
{"@foo[2]", "foo", []*option{
newOption("pos", TypeOption, "2"),
newOption("unnamed", TypeOption, "1"),
newOption("list", TypeList, "20"),
}},
// negative indices count from the end
{"@foo[-3]", "foo", []*option{ // alias for "@foo[0]" == "named"
newOption("pos", TypeOption, "3"),
newOption("unnamed", TypeOption, "0"),
newOption("list", TypeList, "0", "30"),
}},
{"@foo[-2]", "foo", []*option{ // alias for "@foo[1]"
newOption("pos", TypeOption, "1"),
newOption("unnamed", TypeOption, "1"),
newOption("list", TypeList, "10"),
}},
{"@foo[-1]", "foo", []*option{ // alias for "@foo[2]"
newOption("pos", TypeOption, "2"),
newOption("unnamed", TypeOption, "1"),
newOption("list", TypeList, "20"),
}},
}
for i := range cases {
s := cases[i]
for j := range s.Options {
o := s.Options[j]
t.Run("unnamed."+s.Name+"."+o.Name, func(t *testing.T) {
sec := config.Get(s.Name)
if !assert.NotNil(t, sec) {
return
}
opt := sec.Get(o.Name)
if !assert.NotNil(t, opt) {
return
}
assert.EqualValues(t, o.Values, opt.Values)
})
}
}
}