12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310 |
- // 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 (
- confData = `
- ; 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`
- minimalConf = "testdata/minimal.ini"
- fullConf = "testdata/full.ini"
- notFoundConf = "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)
- // Validate 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(notFoundConf)
- So(err, ShouldNotBeNil)
- })
- Convey("Unsupported type", func() {
- _, err := ini.Load(123)
- So(err, ShouldNotBeNil)
- })
- })
- Convey("Can't properly parse INI files containing `#` or `;` in value", t, func() {
- f, err := ini.Load([]byte(`
- [author]
- NAME = U#n#k#n#w#o#n
- GITHUB = U;n;k;n;w;o;n
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- sec := f.Section("author")
- nameValue := sec.Key("NAME").String()
- githubValue := sec.Key("GITHUB").String()
- So(nameValue, ShouldEqual, "U")
- So(githubValue, ShouldEqual, "U")
- })
- Convey("Can't parse small python-compatible INI files", t, func() {
- f, err := ini.Load([]byte(`
- [long]
- long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----
- foo
- bar
- foobar
- barfoo
- -----END RSA PRIVATE KEY-----
- `))
- So(err, ShouldNotBeNil)
- So(f, ShouldBeNil)
- So(err.Error(), ShouldEqual, "key-value delimiter not found: foo\n")
- })
- Convey("Can't parse big python-compatible INI files", t, func() {
- f, err := ini.Load([]byte(`
- [long]
- long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----
- 1foo
- 2bar
- 3foobar
- 4barfoo
- 5foo
- 6bar
- 7foobar
- 8barfoo
- 9foo
- 10bar
- 11foobar
- 12barfoo
- 13foo
- 14bar
- 15foobar
- 16barfoo
- 17foo
- 18bar
- 19foobar
- 20barfoo
- 21foo
- 22bar
- 23foobar
- 24barfoo
- 25foo
- 26bar
- 27foobar
- 28barfoo
- 29foo
- 30bar
- 31foobar
- 32barfoo
- 33foo
- 34bar
- 35foobar
- 36barfoo
- 37foo
- 38bar
- 39foobar
- 40barfoo
- 41foo
- 42bar
- 43foobar
- 44barfoo
- 45foo
- 46bar
- 47foobar
- 48barfoo
- 49foo
- 50bar
- 51foobar
- 52barfoo
- 53foo
- 54bar
- 55foobar
- 56barfoo
- 57foo
- 58bar
- 59foobar
- 60barfoo
- 61foo
- 62bar
- 63foobar
- 64barfoo
- 65foo
- 66bar
- 67foobar
- 68barfoo
- 69foo
- 70bar
- 71foobar
- 72barfoo
- 73foo
- 74bar
- 75foobar
- 76barfoo
- 77foo
- 78bar
- 79foobar
- 80barfoo
- 81foo
- 82bar
- 83foobar
- 84barfoo
- 85foo
- 86bar
- 87foobar
- 88barfoo
- 89foo
- 90bar
- 91foobar
- 92barfoo
- 93foo
- 94bar
- 95foobar
- 96barfoo
- -----END RSA PRIVATE KEY-----
- `))
- So(err, ShouldNotBeNil)
- So(f, ShouldBeNil)
- So(err.Error(), ShouldEqual, "key-value delimiter not found: 1foo\n")
- })
- }
- func TestLooseLoad(t *testing.T) {
- Convey("Load from data sources with option `Loose` true", t, func() {
- f, err := ini.LoadSources(ini.LoadOptions{Loose: true}, notFoundConf, minimalConf)
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- Convey("Inverse case", func() {
- _, err = ini.Load(notFoundConf)
- So(err, ShouldNotBeNil)
- })
- })
- }
- func TestInsensitiveLoad(t *testing.T) {
- Convey("Insensitive to section and key names", t, func() {
- f, err := ini.InsensitiveLoad(minimalConf)
- 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(minimalConf)
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("Author").Key("e-mail").String(), ShouldBeEmpty)
- })
- })
- // Ref: https://github.com/go-ini/ini/issues/198
- Convey("Insensitive load with default section", t, func() {
- f, err := ini.InsensitiveLoad([]byte(`
- user = unknwon
- [profile]
- email = unknwon@local
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section(ini.DefaultSection).Key("user").String(), ShouldEqual, "unknwon")
- })
- }
- func TestLoadSources(t *testing.T) {
- Convey("Load from data sources with options", t, func() {
- Convey("with true `AllowPythonMultilineValues`", func() {
- Convey("Ignore nonexistent files", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true, Loose: true}, notFoundConf, minimalConf)
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- Convey("Inverse case", func() {
- _, err = ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, notFoundConf)
- So(err, ShouldNotBeNil)
- })
- })
- Convey("Insensitive to section and key names", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true, Insensitive: true}, minimalConf)
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, minimalConf)
- 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{
- AllowPythonMultilineValues: true,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, []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{
- AllowPythonMultilineValues: true,
- IgnoreInlineComment: true,
- }, []byte(`
- key1=value ;comment
- key2=value2 #comment2
- key3=val#ue #comment3`))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("").Key("key1").String(), ShouldEqual, `value ;comment`)
- So(f.Section("").Key("key2").String(), ShouldEqual, `value2 #comment2`)
- So(f.Section("").Key("key3").String(), ShouldEqual, `val#ue #comment3`)
- Convey("Inverse case", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, []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("Skip unrecognizable lines", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- SkipUnrecognizableLines: true,
- }, []byte(`
- GenerationDepth: 13
- BiomeRarityScale: 100
- ################
- # Biome Groups #
- ################
- BiomeGroup(NormalBiomes, 3, 99, RoofedForestEnchanted, ForestSakura, FloatingJungle
- BiomeGroup(IceBiomes, 4, 85, Ice Plains)
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("").Key("GenerationDepth").String(), ShouldEqual, "13")
- So(f.Section("").Key("BiomeRarityScale").String(), ShouldEqual, "100")
- So(f.Section("").HasKey("BiomeGroup"), ShouldBeFalse)
- })
- Convey("Allow boolean type keys", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: true,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, []byte(`
- key1=hello
- #key2
- key3`))
- So(err, ShouldNotBeNil)
- })
- })
- Convey("Allow shadow keys", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowShadows: true, AllowPythonMultilineValues: true}, []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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, []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{
- AllowPythonMultilineValues: true,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: 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("Unescape comment symbols inside value", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: true,
- 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("Can parse small python-compatible INI files", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: true,
- Insensitive: true,
- UnparseableSections: []string{"core_lesson", "comments"},
- }, []byte(`
- [long]
- long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----
- foo
- bar
- foobar
- barfoo
- -----END RSA PRIVATE KEY-----
- multiline_list =
- first
- second
- third
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("long").Key("long_rsa_private_key").String(), ShouldEqual, "-----BEGIN RSA PRIVATE KEY-----\nfoo\nbar\nfoobar\nbarfoo\n-----END RSA PRIVATE KEY-----")
- So(f.Section("long").Key("multiline_list").String(), ShouldEqual, "\nfirst\nsecond\nthird")
- })
- Convey("Can parse big python-compatible INI files", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: true,
- Insensitive: true,
- UnparseableSections: []string{"core_lesson", "comments"},
- }, []byte(`
- [long]
- long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----
- 1foo
- 2bar
- 3foobar
- 4barfoo
- 5foo
- 6bar
- 7foobar
- 8barfoo
- 9foo
- 10bar
- 11foobar
- 12barfoo
- 13foo
- 14bar
- 15foobar
- 16barfoo
- 17foo
- 18bar
- 19foobar
- 20barfoo
- 21foo
- 22bar
- 23foobar
- 24barfoo
- 25foo
- 26bar
- 27foobar
- 28barfoo
- 29foo
- 30bar
- 31foobar
- 32barfoo
- 33foo
- 34bar
- 35foobar
- 36barfoo
- 37foo
- 38bar
- 39foobar
- 40barfoo
- 41foo
- 42bar
- 43foobar
- 44barfoo
- 45foo
- 46bar
- 47foobar
- 48barfoo
- 49foo
- 50bar
- 51foobar
- 52barfoo
- 53foo
- 54bar
- 55foobar
- 56barfoo
- 57foo
- 58bar
- 59foobar
- 60barfoo
- 61foo
- 62bar
- 63foobar
- 64barfoo
- 65foo
- 66bar
- 67foobar
- 68barfoo
- 69foo
- 70bar
- 71foobar
- 72barfoo
- 73foo
- 74bar
- 75foobar
- 76barfoo
- 77foo
- 78bar
- 79foobar
- 80barfoo
- 81foo
- 82bar
- 83foobar
- 84barfoo
- 85foo
- 86bar
- 87foobar
- 88barfoo
- 89foo
- 90bar
- 91foobar
- 92barfoo
- 93foo
- 94bar
- 95foobar
- 96barfoo
- -----END RSA PRIVATE KEY-----
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("long").Key("long_rsa_private_key").String(), ShouldEqual, `-----BEGIN RSA PRIVATE KEY-----
- 1foo
- 2bar
- 3foobar
- 4barfoo
- 5foo
- 6bar
- 7foobar
- 8barfoo
- 9foo
- 10bar
- 11foobar
- 12barfoo
- 13foo
- 14bar
- 15foobar
- 16barfoo
- 17foo
- 18bar
- 19foobar
- 20barfoo
- 21foo
- 22bar
- 23foobar
- 24barfoo
- 25foo
- 26bar
- 27foobar
- 28barfoo
- 29foo
- 30bar
- 31foobar
- 32barfoo
- 33foo
- 34bar
- 35foobar
- 36barfoo
- 37foo
- 38bar
- 39foobar
- 40barfoo
- 41foo
- 42bar
- 43foobar
- 44barfoo
- 45foo
- 46bar
- 47foobar
- 48barfoo
- 49foo
- 50bar
- 51foobar
- 52barfoo
- 53foo
- 54bar
- 55foobar
- 56barfoo
- 57foo
- 58bar
- 59foobar
- 60barfoo
- 61foo
- 62bar
- 63foobar
- 64barfoo
- 65foo
- 66bar
- 67foobar
- 68barfoo
- 69foo
- 70bar
- 71foobar
- 72barfoo
- 73foo
- 74bar
- 75foobar
- 76barfoo
- 77foo
- 78bar
- 79foobar
- 80barfoo
- 81foo
- 82bar
- 83foobar
- 84barfoo
- 85foo
- 86bar
- 87foobar
- 88barfoo
- 89foo
- 90bar
- 91foobar
- 92barfoo
- 93foo
- 94bar
- 95foobar
- 96barfoo
- -----END RSA PRIVATE KEY-----`)
- })
- Convey("Allow unparsable sections", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: true,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, []byte(`
- [CORE_LESSON]
- my lesson state data – 1111111111111111111000000000000000001110000
- 111111111111111111100000000000111000000000 – end my lesson state data`))
- So(err, ShouldNotBeNil)
- })
- })
- Convey("And false `SpaceBeforeInlineComment`", func() {
- Convey("Can't parse INI files containing `#` or `;` in value", func() {
- f, err := ini.LoadSources(
- ini.LoadOptions{AllowPythonMultilineValues: false, SpaceBeforeInlineComment: false},
- []byte(`
- [author]
- NAME = U#n#k#n#w#o#n
- GITHUB = U;n;k;n;w;o;n
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- sec := f.Section("author")
- nameValue := sec.Key("NAME").String()
- githubValue := sec.Key("GITHUB").String()
- So(nameValue, ShouldEqual, "U")
- So(githubValue, ShouldEqual, "U")
- })
- })
- Convey("And true `SpaceBeforeInlineComment`", func() {
- Convey("Can parse INI files containing `#` or `;` in value", func() {
- f, err := ini.LoadSources(
- ini.LoadOptions{AllowPythonMultilineValues: false, SpaceBeforeInlineComment: true},
- []byte(`
- [author]
- NAME = U#n#k#n#w#o#n
- GITHUB = U;n;k;n;w;o;n
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- sec := f.Section("author")
- nameValue := sec.Key("NAME").String()
- githubValue := sec.Key("GITHUB").String()
- So(nameValue, ShouldEqual, "U#n#k#n#w#o#n")
- So(githubValue, ShouldEqual, "U;n;k;n;w;o;n")
- })
- })
- })
- Convey("with false `AllowPythonMultilineValues`", func() {
- Convey("Ignore nonexistent files", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: false,
- Loose: true,
- }, notFoundConf, minimalConf)
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- Convey("Inverse case", func() {
- _, err = ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: false,
- }, notFoundConf)
- So(err, ShouldNotBeNil)
- })
- })
- Convey("Insensitive to section and key names", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: false,
- Insensitive: true,
- }, minimalConf)
- 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.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: false,
- }, minimalConf)
- 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{
- AllowPythonMultilineValues: false,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []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{
- AllowPythonMultilineValues: false,
- IgnoreInlineComment: true,
- }, []byte(`
- key1=value ;comment
- key2=value2 #comment2
- key3=val#ue #comment3`))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("").Key("key1").String(), ShouldEqual, `value ;comment`)
- So(f.Section("").Key("key2").String(), ShouldEqual, `value2 #comment2`)
- So(f.Section("").Key("key3").String(), ShouldEqual, `val#ue #comment3`)
- Convey("Inverse case", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []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{
- AllowPythonMultilineValues: false,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []byte(`
- key1=hello
- #key2
- key3`))
- So(err, ShouldNotBeNil)
- })
- })
- Convey("Allow shadow keys", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false, AllowShadows: true}, []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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []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{
- AllowPythonMultilineValues: false,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []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{
- AllowPythonMultilineValues: false,
- 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("Can't parse small python-compatible INI files", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []byte(`
- [long]
- long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----
- foo
- bar
- foobar
- barfoo
- -----END RSA PRIVATE KEY-----
- `))
- So(err, ShouldNotBeNil)
- So(f, ShouldBeNil)
- So(err.Error(), ShouldEqual, "key-value delimiter not found: foo\n")
- })
- Convey("Can't parse big python-compatible INI files", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []byte(`
- [long]
- long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----
- 1foo
- 2bar
- 3foobar
- 4barfoo
- 5foo
- 6bar
- 7foobar
- 8barfoo
- 9foo
- 10bar
- 11foobar
- 12barfoo
- 13foo
- 14bar
- 15foobar
- 16barfoo
- 17foo
- 18bar
- 19foobar
- 20barfoo
- 21foo
- 22bar
- 23foobar
- 24barfoo
- 25foo
- 26bar
- 27foobar
- 28barfoo
- 29foo
- 30bar
- 31foobar
- 32barfoo
- 33foo
- 34bar
- 35foobar
- 36barfoo
- 37foo
- 38bar
- 39foobar
- 40barfoo
- 41foo
- 42bar
- 43foobar
- 44barfoo
- 45foo
- 46bar
- 47foobar
- 48barfoo
- 49foo
- 50bar
- 51foobar
- 52barfoo
- 53foo
- 54bar
- 55foobar
- 56barfoo
- 57foo
- 58bar
- 59foobar
- 60barfoo
- 61foo
- 62bar
- 63foobar
- 64barfoo
- 65foo
- 66bar
- 67foobar
- 68barfoo
- 69foo
- 70bar
- 71foobar
- 72barfoo
- 73foo
- 74bar
- 75foobar
- 76barfoo
- 77foo
- 78bar
- 79foobar
- 80barfoo
- 81foo
- 82bar
- 83foobar
- 84barfoo
- 85foo
- 86bar
- 87foobar
- 88barfoo
- 89foo
- 90bar
- 91foobar
- 92barfoo
- 93foo
- 94bar
- 95foobar
- 96barfoo
- -----END RSA PRIVATE KEY-----
- `))
- So(err, ShouldNotBeNil)
- So(f, ShouldBeNil)
- So(err.Error(), ShouldEqual, "key-value delimiter not found: 1foo\n")
- })
- Convey("Allow unparsable sections", func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- AllowPythonMultilineValues: false,
- 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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, []byte(`
- [CORE_LESSON]
- my lesson state data – 1111111111111111111000000000000000001110000
- 111111111111111111100000000000111000000000 – end my lesson state data`))
- So(err, ShouldNotBeNil)
- })
- })
- Convey("And false `SpaceBeforeInlineComment`", func() {
- Convey("Can't parse INI files containing `#` or `;` in value", func() {
- f, err := ini.LoadSources(
- ini.LoadOptions{AllowPythonMultilineValues: true, SpaceBeforeInlineComment: false},
- []byte(`
- [author]
- NAME = U#n#k#n#w#o#n
- GITHUB = U;n;k;n;w;o;n
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- sec := f.Section("author")
- nameValue := sec.Key("NAME").String()
- githubValue := sec.Key("GITHUB").String()
- So(nameValue, ShouldEqual, "U")
- So(githubValue, ShouldEqual, "U")
- })
- })
- Convey("And true `SpaceBeforeInlineComment`", func() {
- Convey("Can parse INI files containing `#` or `;` in value", func() {
- f, err := ini.LoadSources(
- ini.LoadOptions{AllowPythonMultilineValues: true, SpaceBeforeInlineComment: true},
- []byte(`
- [author]
- NAME = U#n#k#n#w#o#n
- GITHUB = U;n;k;n;w;o;n
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- sec := f.Section("author")
- nameValue := sec.Key("NAME").String()
- githubValue := sec.Key("GITHUB").String()
- So(nameValue, ShouldEqual, "U#n#k#n#w#o#n")
- So(githubValue, ShouldEqual, "U;n;k;n;w;o;n")
- })
- })
- })
- })
- }
- func Test_KeyValueDelimiters(t *testing.T) {
- Convey("Custom key-value delimiters", t, func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- KeyValueDelimiters: "?!",
- }, []byte(`
- [section]
- key1?value1
- key2!value2
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("section").Key("key1").String(), ShouldEqual, "value1")
- So(f.Section("section").Key("key2").String(), ShouldEqual, "value2")
- })
- }
- func Test_PreserveSurroundedQuote(t *testing.T) {
- Convey("Preserve surrounded quote test", t, func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- PreserveSurroundedQuote: true,
- }, []byte(`
- [section]
- key1 = "value1"
- key2 = value2
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("section").Key("key1").String(), ShouldEqual, "\"value1\"")
- So(f.Section("section").Key("key2").String(), ShouldEqual, "value2")
- })
- Convey("Preserve surrounded quote test inverse test", t, func() {
- f, err := ini.LoadSources(ini.LoadOptions{
- PreserveSurroundedQuote: false,
- }, []byte(`
- [section]
- key1 = "value1"
- key2 = value2
- `))
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- So(f.Section("section").Key("key1").String(), ShouldEqual, "value1")
- So(f.Section("section").Key("key2").String(), ShouldEqual, "value2")
- })
- }
|