~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/ini.v1/ini_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Unknwon
 
2
//
 
3
// Licensed under the Apache License, Version 2.0 (the "License"): you may
 
4
// not use this file except in compliance with the License. You may obtain
 
5
// a copy of the License at
 
6
//
 
7
//     http://www.apache.org/licenses/LICENSE-2.0
 
8
//
 
9
// Unless required by applicable law or agreed to in writing, software
 
10
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
// License for the specific language governing permissions and limitations
 
13
// under the License.
 
14
 
 
15
package ini
 
16
 
 
17
import (
 
18
        "bytes"
 
19
        "testing"
 
20
        "time"
 
21
 
 
22
        . "github.com/smartystreets/goconvey/convey"
 
23
)
 
24
 
 
25
func Test_Version(t *testing.T) {
 
26
        Convey("Get version", t, func() {
 
27
                So(Version(), ShouldEqual, _VERSION)
 
28
        })
 
29
}
 
30
 
 
31
const _CONF_DATA = `
 
32
; Package name
 
33
NAME        = ini
 
34
; Package version
 
35
VERSION     = v1
 
36
; Package import path
 
37
IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
 
38
 
 
39
# Information about package author
 
40
# Bio can be written in multiple lines.
 
41
[author]
 
42
NAME   = Unknwon  ; Succeeding comment
 
43
E-MAIL = fake@localhost
 
44
GITHUB = https://github.com/%(NAME)s
 
45
BIO    = """Gopher.
 
46
Coding addict.
 
47
Good man.
 
48
"""  # Succeeding comment
 
49
 
 
50
[package]
 
51
CLONE_URL = https://%(IMPORT_PATH)s
 
52
 
 
53
[package.sub]
 
54
UNUSED_KEY = should be deleted
 
55
 
 
56
[features]
 
57
-: Support read/write comments of keys and sections
 
58
-: Support auto-increment of key names
 
59
-: Support load multiple files to overwrite key values
 
60
 
 
61
[types]
 
62
STRING     = str
 
63
BOOL       = true
 
64
BOOL_FALSE = false
 
65
FLOAT64    = 1.25
 
66
INT        = 10
 
67
TIME       = 2015-01-01T20:17:05Z
 
68
DURATION   = 2h45m
 
69
UINT       = 3
 
70
 
 
71
[array]
 
72
STRINGS  = en, zh, de
 
73
FLOAT64S = 1.1, 2.2, 3.3
 
74
INTS     = 1, 2, 3
 
75
UINTS    = 1, 2, 3
 
76
TIMES    = 2015-01-01T20:17:05Z,2015-01-01T20:17:05Z,2015-01-01T20:17:05Z
 
77
 
 
78
[note]
 
79
empty_lines = next line is empty\
 
80
 
 
81
; Comment before the section
 
82
[comments] ; This is a comment for the section too
 
83
; Comment before key
 
84
key  = "value"
 
85
key2 = "value2" ; This is a comment for key2
 
86
key3 = "one", "two", "three"
 
87
 
 
88
[advance]
 
89
value with quotes = "some value"
 
90
value quote2 again = 'some value'
 
91
true = 2+3=5
 
92
"1+1=2" = true
 
93
"""6+1=7""" = true
 
94
"""` + "`" + `5+5` + "`" + `""" = 10
 
95
` + "`" + `"6+6"` + "`" + ` = 12
 
96
` + "`" + `7-2=4` + "`" + ` = false
 
97
ADDRESS = ` + "`" + `404 road,
 
98
NotFound, State, 50000` + "`" + `
 
99
 
 
100
two_lines = how about \
 
101
        continuation lines?
 
102
lots_of_lines = 1 \
 
103
        2 \
 
104
        3 \
 
105
        4 \
 
106
`
 
107
 
 
108
func Test_Load(t *testing.T) {
 
109
        Convey("Load from data sources", t, func() {
 
110
 
 
111
                Convey("Load with empty data", func() {
 
112
                        So(Empty(), ShouldNotBeNil)
 
113
                })
 
114
 
 
115
                Convey("Load with multiple data sources", func() {
 
116
                        cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini")
 
117
                        So(err, ShouldBeNil)
 
118
                        So(cfg, ShouldNotBeNil)
 
119
 
 
120
                        f, err := Load([]byte(_CONF_DATA), "testdata/404.ini")
 
121
                        So(err, ShouldNotBeNil)
 
122
                        So(f, ShouldBeNil)
 
123
                })
 
124
        })
 
125
 
 
126
        Convey("Bad load process", t, func() {
 
127
 
 
128
                Convey("Load from invalid data sources", func() {
 
129
                        _, err := Load(_CONF_DATA)
 
130
                        So(err, ShouldNotBeNil)
 
131
 
 
132
                        f, err := Load("testdata/404.ini")
 
133
                        So(err, ShouldNotBeNil)
 
134
                        So(f, ShouldBeNil)
 
135
 
 
136
                        _, err = Load(1)
 
137
                        So(err, ShouldNotBeNil)
 
138
 
 
139
                        _, err = Load([]byte(""), 1)
 
140
                        So(err, ShouldNotBeNil)
 
141
                })
 
142
 
 
143
                Convey("Load with bad section name", func() {
 
144
                        _, err := Load([]byte("[]"))
 
145
                        So(err, ShouldNotBeNil)
 
146
 
 
147
                        _, err = Load([]byte("["))
 
148
                        So(err, ShouldNotBeNil)
 
149
                })
 
150
 
 
151
                Convey("Load with bad keys", func() {
 
152
                        _, err := Load([]byte(`"""name`))
 
153
                        So(err, ShouldNotBeNil)
 
154
 
 
155
                        _, err = Load([]byte(`"""name"""`))
 
156
                        So(err, ShouldNotBeNil)
 
157
 
 
158
                        _, err = Load([]byte(`""=1`))
 
159
                        So(err, ShouldNotBeNil)
 
160
 
 
161
                        _, err = Load([]byte(`=`))
 
162
                        So(err, ShouldNotBeNil)
 
163
 
 
164
                        _, err = Load([]byte(`name`))
 
165
                        So(err, ShouldNotBeNil)
 
166
                })
 
167
 
 
168
                Convey("Load with bad values", func() {
 
169
                        _, err := Load([]byte(`name="""Unknwon`))
 
170
                        So(err, ShouldNotBeNil)
 
171
                })
 
172
        })
 
173
}
 
174
 
 
175
func Test_LooseLoad(t *testing.T) {
 
176
        Convey("Loose load from data sources", t, func() {
 
177
                Convey("Loose load mixed with nonexistent file", func() {
 
178
                        cfg, err := LooseLoad("testdata/404.ini")
 
179
                        So(err, ShouldBeNil)
 
180
                        So(cfg, ShouldNotBeNil)
 
181
                        var fake struct {
 
182
                                Name string `ini:"name"`
 
183
                        }
 
184
                        So(cfg.MapTo(&fake), ShouldBeNil)
 
185
 
 
186
                        cfg, err = LooseLoad([]byte("name=Unknwon"), "testdata/404.ini")
 
187
                        So(err, ShouldBeNil)
 
188
                        So(cfg.Section("").Key("name").String(), ShouldEqual, "Unknwon")
 
189
                        So(cfg.MapTo(&fake), ShouldBeNil)
 
190
                        So(fake.Name, ShouldEqual, "Unknwon")
 
191
                })
 
192
        })
 
193
 
 
194
}
 
195
 
 
196
func Test_File_Append(t *testing.T) {
 
197
        Convey("Append data sources", t, func() {
 
198
                cfg, err := Load([]byte(""))
 
199
                So(err, ShouldBeNil)
 
200
                So(cfg, ShouldNotBeNil)
 
201
 
 
202
                So(cfg.Append([]byte(""), []byte("")), ShouldBeNil)
 
203
 
 
204
                Convey("Append bad data sources", func() {
 
205
                        So(cfg.Append(1), ShouldNotBeNil)
 
206
                        So(cfg.Append([]byte(""), 1), ShouldNotBeNil)
 
207
                })
 
208
        })
 
209
}
 
210
 
 
211
func Test_File_WriteTo(t *testing.T) {
 
212
        Convey("Write to somewhere", t, func() {
 
213
                var buf bytes.Buffer
 
214
                cfg := Empty()
 
215
                cfg.WriteTo(&buf)
 
216
        })
 
217
}
 
218
 
 
219
func Test_File_SaveTo(t *testing.T) {
 
220
        Convey("Save file", t, func() {
 
221
                cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini")
 
222
                So(err, ShouldBeNil)
 
223
                So(cfg, ShouldNotBeNil)
 
224
 
 
225
                cfg.Section("").Key("NAME").Comment = "Package name"
 
226
                cfg.Section("author").Comment = `Information about package author
 
227
# Bio can be written in multiple lines.`
 
228
                cfg.Section("advanced").Key("val w/ pound").SetValue("my#password")
 
229
                So(cfg.SaveTo("testdata/conf_out.ini"), ShouldBeNil)
 
230
 
 
231
                cfg.Section("author").Key("NAME").Comment = "This is author name"
 
232
                So(cfg.SaveToIndent("testdata/conf_out.ini", "\t"), ShouldBeNil)
 
233
        })
 
234
}
 
235
 
 
236
// Helpers for slice tests.
 
237
func float64sEqual(values []float64, expected ...float64) {
 
238
        So(values, ShouldHaveLength, len(expected))
 
239
        for i, v := range expected {
 
240
                So(values[i], ShouldEqual, v)
 
241
        }
 
242
}
 
243
 
 
244
func intsEqual(values []int, expected ...int) {
 
245
        So(values, ShouldHaveLength, len(expected))
 
246
        for i, v := range expected {
 
247
                So(values[i], ShouldEqual, v)
 
248
        }
 
249
}
 
250
 
 
251
func int64sEqual(values []int64, expected ...int64) {
 
252
        So(values, ShouldHaveLength, len(expected))
 
253
        for i, v := range expected {
 
254
                So(values[i], ShouldEqual, v)
 
255
        }
 
256
}
 
257
 
 
258
func uintsEqual(values []uint, expected ...uint) {
 
259
        So(values, ShouldHaveLength, len(expected))
 
260
        for i, v := range expected {
 
261
                So(values[i], ShouldEqual, v)
 
262
        }
 
263
}
 
264
 
 
265
func uint64sEqual(values []uint64, expected ...uint64) {
 
266
        So(values, ShouldHaveLength, len(expected))
 
267
        for i, v := range expected {
 
268
                So(values[i], ShouldEqual, v)
 
269
        }
 
270
}
 
271
 
 
272
func timesEqual(values []time.Time, expected ...time.Time) {
 
273
        So(values, ShouldHaveLength, len(expected))
 
274
        for i, v := range expected {
 
275
                So(values[i].String(), ShouldEqual, v.String())
 
276
        }
 
277
}