summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/go-ini/ini/ini_test.go
blob: df5fbe384d9f8afe61916a1c09c0c947a532a525 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2014 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package ini_test

import (
	"bytes"
	"flag"
	"io/ioutil"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"gopkg.in/ini.v1"
)

const (
	_CONF_DATA = `
	; Package name
	NAME        = ini
	; Package version
	VERSION     = v1
	; Package import path
	IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
	
	# Information about package author
	# Bio can be written in multiple lines.
	[author]
	NAME   = Unknwon  ; Succeeding comment
	E-MAIL = fake@localhost
	GITHUB = https://github.com/%(NAME)s
	BIO    = """Gopher.
	Coding addict.
	Good man.
	"""  # Succeeding comment`
	_MINIMAL_CONF   = "testdata/minimal.ini"
	_FULL_CONF      = "testdata/full.ini"
	_NOT_FOUND_CONF = "testdata/404.ini"
)

var update = flag.Bool("update", false, "Update .golden files")

func TestLoad(t *testing.T) {
	Convey("Load from good data sources", t, func() {
		f, err := ini.Load([]byte(`
NAME = ini
VERSION = v1
IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s`),
			"testdata/minimal.ini",
			ioutil.NopCloser(bytes.NewReader([]byte(`
[author]
NAME = Unknwon
`))),
		)
		So(err, ShouldBeNil)
		So(f, ShouldNotBeNil)

		// Vaildate values make sure all sources are loaded correctly
		sec := f.Section("")
		So(sec.Key("NAME").String(), ShouldEqual, "ini")
		So(sec.Key("VERSION").String(), ShouldEqual, "v1")
		So(sec.Key("IMPORT_PATH").String(), ShouldEqual, "gopkg.in/ini.v1")

		sec = f.Section("author")
		So(sec.Key("NAME").String(), ShouldEqual, "Unknwon")
		So(sec.Key("E-MAIL").String(), ShouldEqual, "u@gogs.io")
	})

	Convey("Load from bad data sources", t, func() {
		Convey("Invalid input", func() {
			_, err := ini.Load(_NOT_FOUND_CONF)
			So(err, ShouldNotBeNil)
		})

		Convey("Unsupported type", func() {
			_, err := ini.Load(123)
			So(err, ShouldNotBeNil)
		})
	})
}

func TestLoadSources(t *testing.T) {
	Convey("Load from data sources with options", t, func() {
		Convey("Ignore nonexistent files", func() {
			f, err := ini.LooseLoad(_NOT_FOUND_CONF, _MINIMAL_CONF)
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			Convey("Inverse case", func() {
				_, err = ini.Load(_NOT_FOUND_CONF)
				So(err, ShouldNotBeNil)
			})
		})

		Convey("Insensitive to section and key names", func() {
			f, err := ini.InsensitiveLoad(_MINIMAL_CONF)
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section("Author").Key("e-mail").String(), ShouldEqual, "u@gogs.io")

			Convey("Write out", func() {
				var buf bytes.Buffer
				_, err := f.WriteTo(&buf)
				So(err, ShouldBeNil)
				So(buf.String(), ShouldEqual, `[author]
e-mail = u@gogs.io

`)
			})

			Convey("Inverse case", func() {
				f, err := ini.Load(_MINIMAL_CONF)
				So(err, ShouldBeNil)
				So(f, ShouldNotBeNil)

				So(f.Section("Author").Key("e-mail").String(), ShouldBeEmpty)
			})
		})

		Convey("Ignore continuation lines", func() {
			f, err := ini.LoadSources(ini.LoadOptions{
				IgnoreContinuation: true,
			}, []byte(`
key1=a\b\
key2=c\d\
key3=value`))
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section("").Key("key1").String(), ShouldEqual, `a\b\`)
			So(f.Section("").Key("key2").String(), ShouldEqual, `c\d\`)
			So(f.Section("").Key("key3").String(), ShouldEqual, "value")

			Convey("Inverse case", func() {
				f, err := ini.Load([]byte(`
key1=a\b\
key2=c\d\`))
				So(err, ShouldBeNil)
				So(f, ShouldNotBeNil)

				So(f.Section("").Key("key1").String(), ShouldEqual, `a\bkey2=c\d`)
			})
		})

		Convey("Ignore inline comments", func() {
			f, err := ini.LoadSources(ini.LoadOptions{
				IgnoreInlineComment: true,
			}, []byte(`
key1=value ;comment
key2=value2 #comment2`))
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section("").Key("key1").String(), ShouldEqual, `value ;comment`)
			So(f.Section("").Key("key2").String(), ShouldEqual, `value2 #comment2`)

			Convey("Inverse case", func() {
				f, err := ini.Load([]byte(`
key1=value ;comment
key2=value2 #comment2`))
				So(err, ShouldBeNil)
				So(f, ShouldNotBeNil)

				So(f.Section("").Key("key1").String(), ShouldEqual, `value`)
				So(f.Section("").Key("key1").Comment, ShouldEqual, `;comment`)
				So(f.Section("").Key("key2").String(), ShouldEqual, `value2`)
				So(f.Section("").Key("key2").Comment, ShouldEqual, `#comment2`)
			})
		})

		Convey("Allow boolean type keys", func() {
			f, err := ini.LoadSources(ini.LoadOptions{
				AllowBooleanKeys: true,
			}, []byte(`
key1=hello
#key2
key3`))
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section("").KeyStrings(), ShouldResemble, []string{"key1", "key3"})
			So(f.Section("").Key("key3").MustBool(false), ShouldBeTrue)

			Convey("Write out", func() {
				var buf bytes.Buffer
				_, err := f.WriteTo(&buf)
				So(err, ShouldBeNil)
				So(buf.String(), ShouldEqual, `key1 = hello
# key2
key3
`)
			})

			Convey("Inverse case", func() {
				_, err := ini.Load([]byte(`
key1=hello
#key2
key3`))
				So(err, ShouldNotBeNil)
			})
		})

		Convey("Allow shadow keys", func() {
			f, err := ini.ShadowLoad([]byte(`
[remote "origin"]
url = https://github.com/Antergone/test1.git
url = https://github.com/Antergone/test2.git
fetch = +refs/heads/*:refs/remotes/origin/*`))
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section(`remote "origin"`).Key("url").String(), ShouldEqual, "https://github.com/Antergone/test1.git")
			So(f.Section(`remote "origin"`).Key("url").ValueWithShadows(), ShouldResemble, []string{
				"https://github.com/Antergone/test1.git",
				"https://github.com/Antergone/test2.git",
			})
			So(f.Section(`remote "origin"`).Key("fetch").String(), ShouldEqual, "+refs/heads/*:refs/remotes/origin/*")

			Convey("Write out", func() {
				var buf bytes.Buffer
				_, err := f.WriteTo(&buf)
				So(err, ShouldBeNil)
				So(buf.String(), ShouldEqual, `[remote "origin"]
url   = https://github.com/Antergone/test1.git
url   = https://github.com/Antergone/test2.git
fetch = +refs/heads/*:refs/remotes/origin/*

`)
			})

			Convey("Inverse case", func() {
				f, err := ini.Load([]byte(`
[remote "origin"]
url = https://github.com/Antergone/test1.git
url = https://github.com/Antergone/test2.git`))
				So(err, ShouldBeNil)
				So(f, ShouldNotBeNil)

				So(f.Section(`remote "origin"`).Key("url").String(), ShouldEqual, "https://github.com/Antergone/test2.git")
			})
		})

		Convey("Unescape double quotes inside value", func() {
			f, err := ini.LoadSources(ini.LoadOptions{
				UnescapeValueDoubleQuotes: true,
			}, []byte(`
create_repo="创建了仓库 <a href=\"%s\">%s</a>"`))
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section("").Key("create_repo").String(), ShouldEqual, `创建了仓库 <a href="%s">%s</a>`)

			Convey("Inverse case", func() {
				f, err := ini.Load([]byte(`
create_repo="创建了仓库 <a href=\"%s\">%s</a>"`))
				So(err, ShouldBeNil)
				So(f, ShouldNotBeNil)

				So(f.Section("").Key("create_repo").String(), ShouldEqual, `"创建了仓库 <a href=\"%s\">%s</a>"`)
			})
		})

		Convey("Unescape comment symbols inside value", func() {
			f, err := ini.LoadSources(ini.LoadOptions{
				IgnoreInlineComment:         true,
				UnescapeValueCommentSymbols: true,
			}, []byte(`
key = test value <span style="color: %s\; background: %s">more text</span>
`))
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section("").Key("key").String(), ShouldEqual, `test value <span style="color: %s; background: %s">more text</span>`)
		})

		Convey("Allow unparseable sections", func() {
			f, err := ini.LoadSources(ini.LoadOptions{
				Insensitive:         true,
				UnparseableSections: []string{"core_lesson", "comments"},
			}, []byte(`
Lesson_Location = 87
Lesson_Status = C
Score = 3
Time = 00:02:30

[CORE_LESSON]
my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data

[COMMENTS]
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
			So(err, ShouldBeNil)
			So(f, ShouldNotBeNil)

			So(f.Section("").Key("score").String(), ShouldEqual, "3")
			So(f.Section("").Body(), ShouldBeEmpty)
			So(f.Section("core_lesson").Body(), ShouldEqual, `my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data`)
			So(f.Section("comments").Body(), ShouldEqual, `<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`)

			Convey("Write out", func() {
				var buf bytes.Buffer
				_, err := f.WriteTo(&buf)
				So(err, ShouldBeNil)
				So(buf.String(), ShouldEqual, `lesson_location = 87
lesson_status   = C
score           = 3
time            = 00:02:30

[core_lesson]
my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data

[comments]
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>
`)
			})

			Convey("Inverse case", func() {
				_, err := ini.Load([]byte(`
[CORE_LESSON]
my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data`))
				So(err, ShouldNotBeNil)
			})
		})
	})
}