1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282 |
- // 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)
- // 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(_NOT_FOUND_CONF)
- 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}, _NOT_FOUND_CONF, _MINIMAL_CONF)
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- Convey("Inverse case", func() {
- _, err = ini.Load(_NOT_FOUND_CONF)
- So(err, ShouldNotBeNil)
- })
- })
- }
- func TestInsensitiveLoad(t *testing.T) {
- Convey("Insensitive to section and key names", t, 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)
- })
- })
- }
- 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}, _NOT_FOUND_CONF, _MINIMAL_CONF)
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- Convey("Inverse case", func() {
- _, err = ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, _NOT_FOUND_CONF)
- So(err, ShouldNotBeNil)
- })
- })
- Convey("Insensitive to section and key names", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true, Insensitive: true}, _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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, _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{
- 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-----
- `))
- 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-----")
- })
- 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}, _NOT_FOUND_CONF, _MINIMAL_CONF)
- So(err, ShouldBeNil)
- So(f, ShouldNotBeNil)
- Convey("Inverse case", func() {
- _, err = ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, _NOT_FOUND_CONF)
- So(err, ShouldNotBeNil)
- })
- })
- Convey("Insensitive to section and key names", func() {
- f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false, Insensitive: true}, _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.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, _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{
- 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")
- })
- }
|