-
Notifications
You must be signed in to change notification settings - Fork 31
/
convenience_test.go
171 lines (146 loc) · 4.4 KB
/
convenience_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package uci
import (
"errors"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// mockTree implements the Tree interface and an instance replaces the
// global defaultTree (see TestMain).
type mockTree struct {
mock.Mock
}
func (m *mockTree) LoadConfig(name string, forceReload bool) error {
args := m.Called(name, forceReload)
return args.Error(0)
}
func (m *mockTree) Commit() error {
args := m.Called()
return args.Error(0)
}
func (m *mockTree) Revert(configs ...string) {
m.Called(configs)
}
func (m *mockTree) GetSections(config string, secType string) ([]string, error) {
args := m.Called(config, secType)
return []string{args.String(0)}, args.Error(1)
}
func (m *mockTree) Get(config, section, option string) ([]string, bool) {
args := m.Called(config, section, option)
return []string{args.String(0)}, args.Bool(1)
}
func (m *mockTree) GetLast(config, section, option string) (string, bool) {
args := m.Called(config, section, option)
return args.String(0), args.Bool(1)
}
func (m *mockTree) GetBool(config, section, option string) (bool, bool) {
args := m.Called(config, section, option)
return args.Bool(0), args.Bool(1)
}
func (m *mockTree) SetType(config, section, option string, typ OptionType, values ...string) error {
args := m.Called(config, section, option, typ, values)
return args.Error(0)
}
func (m *mockTree) Del(config, section, option string) error {
m.Called(config, section, option)
return nil
}
func (m *mockTree) AddSection(config, section, typ string) error {
args := m.Called(config, section, typ)
return args.Error(0)
}
func (m *mockTree) DelSection(config, section string) error {
m.Called(config, section)
return nil
}
func TestMain(m *testing.M) {
defaultTree = &mockTree{}
os.Exit(m.Run())
}
func TestConvenienceLoadConfig(t *testing.T) {
assert := assert.New(t)
m := defaultTree.(*mockTree)
m.On("LoadConfig", "foo", true).Return(nil)
m.On("LoadConfig", "bar", false).Return(io.ErrUnexpectedEOF)
assert.NoError(LoadConfig("foo", true))
err := LoadConfig("bar", false)
assert.Error(err, io.ErrUnexpectedEOF)
m.AssertExpectations(t)
}
func TestConvenienceCommit(t *testing.T) {
assert := assert.New(t)
m := defaultTree.(*mockTree)
m.On("Commit").Return(nil)
assert.NoError(Commit())
m.AssertExpectations(t)
}
func TestConvenienceRevert(t *testing.T) {
m := defaultTree.(*mockTree)
m.On("Revert", []string{"foo", "bar"}).Return()
Revert("foo", "bar")
m.AssertExpectations(t)
}
func TestConvenienceGetSections(t *testing.T) {
assert := assert.New(t)
m := defaultTree.(*mockTree)
m.On("GetSections", "foo", "bar").Return("sec1", nil)
list, err := GetSections("foo", "bar")
assert.NoError(err)
assert.EqualValues([]string{"sec1"}, list)
m.AssertExpectations(t)
}
func TestConvenienceGet(t *testing.T) {
assert := assert.New(t)
m := defaultTree.(*mockTree)
m.On("Get", "foo", "bar", "opt").Return("ok", true)
list, ok := Get("foo", "bar", "opt")
assert.True(ok)
assert.EqualValues([]string{"ok"}, list)
m.AssertExpectations(t)
}
func TestConvenienceGetLast(t *testing.T) {
assert := assert.New(t)
m := defaultTree.(*mockTree)
m.On("GetLast", "foo", "bar", "opt").Return("ok", true)
list, ok := GetLast("foo", "bar", "opt")
assert.True(ok)
assert.EqualValues("ok", list)
m.AssertExpectations(t)
}
func TestConvenienceGetBool(t *testing.T) {
assert := assert.New(t)
m := defaultTree.(*mockTree)
m.On("GetBool", "foo", "bar", "opt").Return(true, true)
value, ok := GetBool("foo", "bar", "opt")
assert.True(ok)
assert.True(value)
m.AssertExpectations(t)
}
func TestConvenienceDel(t *testing.T) {
m := defaultTree.(*mockTree)
m.On("Del", "foo", "bar", "opt").Return()
err := Del("foo", "bar", "opt")
assert.NoError(t, err)
m.AssertExpectations(t)
}
func TestConvenienceAddSection(t *testing.T) {
assert := assert.New(t)
m := defaultTree.(*mockTree)
addSectionErr := errors.New("invalid")
m.On("AddSection", "foo", "bar", "system").Return(nil)
m.On("AddSection", "foo", "bar", "interface").Return(addSectionErr) //nolint:goerr113
err := AddSection("foo", "bar", "interface")
assert.Error(err)
assert.EqualError(err, addSectionErr.Error())
assert.NoError(AddSection("foo", "bar", "system"))
m.AssertExpectations(t)
}
func TestConvenienceDelSection(t *testing.T) {
m := defaultTree.(*mockTree)
m.On("DelSection", "foo", "bar").Return()
err := DelSection("foo", "bar")
assert.NoError(t, err)
m.AssertExpectations(t)
}