1
// Copyright 2014 Unknwon
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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
22
. "github.com/smartystreets/goconvey/convey"
25
func Test_Version(t *testing.T) {
26
Convey("Get version", t, func() {
27
So(Version(), ShouldEqual, _VERSION)
37
IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
39
# Information about package author
40
# Bio can be written in multiple lines.
42
NAME = Unknwon ; Succeeding comment
43
E-MAIL = fake@localhost
44
GITHUB = https://github.com/%(NAME)s
48
""" # Succeeding comment
51
CLONE_URL = https://%(IMPORT_PATH)s
54
UNUSED_KEY = should be deleted
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
67
TIME = 2015-01-01T20:17:05Z
73
FLOAT64S = 1.1, 2.2, 3.3
76
TIMES = 2015-01-01T20:17:05Z,2015-01-01T20:17:05Z,2015-01-01T20:17:05Z
79
empty_lines = next line is empty\
81
; Comment before the section
82
[comments] ; This is a comment for the section too
85
key2 = "value2" ; This is a comment for key2
86
key3 = "one", "two", "three"
89
value with quotes = "some value"
90
value quote2 again = 'some value'
94
"""` + "`" + `5+5` + "`" + `""" = 10
95
` + "`" + `"6+6"` + "`" + ` = 12
96
` + "`" + `7-2=4` + "`" + ` = false
97
ADDRESS = ` + "`" + `404 road,
98
NotFound, State, 50000` + "`" + `
100
two_lines = how about \
108
func Test_Load(t *testing.T) {
109
Convey("Load from data sources", t, func() {
111
Convey("Load with empty data", func() {
112
So(Empty(), ShouldNotBeNil)
115
Convey("Load with multiple data sources", func() {
116
cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini")
118
So(cfg, ShouldNotBeNil)
120
f, err := Load([]byte(_CONF_DATA), "testdata/404.ini")
121
So(err, ShouldNotBeNil)
126
Convey("Bad load process", t, func() {
128
Convey("Load from invalid data sources", func() {
129
_, err := Load(_CONF_DATA)
130
So(err, ShouldNotBeNil)
132
f, err := Load("testdata/404.ini")
133
So(err, ShouldNotBeNil)
137
So(err, ShouldNotBeNil)
139
_, err = Load([]byte(""), 1)
140
So(err, ShouldNotBeNil)
143
Convey("Load with bad section name", func() {
144
_, err := Load([]byte("[]"))
145
So(err, ShouldNotBeNil)
147
_, err = Load([]byte("["))
148
So(err, ShouldNotBeNil)
151
Convey("Load with bad keys", func() {
152
_, err := Load([]byte(`"""name`))
153
So(err, ShouldNotBeNil)
155
_, err = Load([]byte(`"""name"""`))
156
So(err, ShouldNotBeNil)
158
_, err = Load([]byte(`""=1`))
159
So(err, ShouldNotBeNil)
161
_, err = Load([]byte(`=`))
162
So(err, ShouldNotBeNil)
164
_, err = Load([]byte(`name`))
165
So(err, ShouldNotBeNil)
168
Convey("Load with bad values", func() {
169
_, err := Load([]byte(`name="""Unknwon`))
170
So(err, ShouldNotBeNil)
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")
180
So(cfg, ShouldNotBeNil)
182
Name string `ini:"name"`
184
So(cfg.MapTo(&fake), ShouldBeNil)
186
cfg, err = LooseLoad([]byte("name=Unknwon"), "testdata/404.ini")
188
So(cfg.Section("").Key("name").String(), ShouldEqual, "Unknwon")
189
So(cfg.MapTo(&fake), ShouldBeNil)
190
So(fake.Name, ShouldEqual, "Unknwon")
196
func Test_File_Append(t *testing.T) {
197
Convey("Append data sources", t, func() {
198
cfg, err := Load([]byte(""))
200
So(cfg, ShouldNotBeNil)
202
So(cfg.Append([]byte(""), []byte("")), ShouldBeNil)
204
Convey("Append bad data sources", func() {
205
So(cfg.Append(1), ShouldNotBeNil)
206
So(cfg.Append([]byte(""), 1), ShouldNotBeNil)
211
func Test_File_WriteTo(t *testing.T) {
212
Convey("Write to somewhere", t, func() {
219
func Test_File_SaveTo(t *testing.T) {
220
Convey("Save file", t, func() {
221
cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini")
223
So(cfg, ShouldNotBeNil)
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)
231
cfg.Section("author").Key("NAME").Comment = "This is author name"
232
So(cfg.SaveToIndent("testdata/conf_out.ini", "\t"), ShouldBeNil)
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)
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)
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)
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)
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)
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())