Skip to content

Commit

Permalink
*: refactor constants to use camelcase
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Feb 17, 2019
1 parent 7c1c4f7 commit ece0e89
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func newTestFile(block bool) *ini.File {
c, _ := ini.Load([]byte(_CONF_DATA))
c, _ := ini.Load([]byte(confData))
c.BlockMode = block
return c
}
Expand Down
10 changes: 5 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Empty() *File {
func (f *File) NewSection(name string) (*Section, error) {
if len(name) == 0 {
return nil, errors.New("error creating new section: empty section name")
} else if f.options.Insensitive && name != DEFAULT_SECTION {
} else if f.options.Insensitive && name != DefaultSection {
name = strings.ToLower(name)
}

Expand Down Expand Up @@ -111,7 +111,7 @@ func (f *File) NewSections(names ...string) (err error) {
// GetSection returns section by given name.
func (f *File) GetSection(name string) (*Section, error) {
if len(name) == 0 {
name = DEFAULT_SECTION
name = DefaultSection
}
if f.options.Insensitive {
name = strings.ToLower(name)
Expand Down Expand Up @@ -175,7 +175,7 @@ func (f *File) DeleteSection(name string) {
}

if len(name) == 0 {
name = DEFAULT_SECTION
name = DefaultSection
}

for i, s := range f.sectionList {
Expand Down Expand Up @@ -306,7 +306,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
for _, kname := range sec.keyList {
key := sec.Key(kname)
if len(key.Comment) > 0 {
if len(indent) > 0 && sname != DEFAULT_SECTION {
if len(indent) > 0 && sname != DefaultSection {
buf.WriteString(indent)
}

Expand All @@ -325,7 +325,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}
}

if len(indent) > 0 && sname != DEFAULT_SECTION {
if len(indent) > 0 && sname != DefaultSection {
buf.WriteString(indent)
}

Expand Down
32 changes: 16 additions & 16 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func TestFile_NewSection(t *testing.T) {
So(sec, ShouldNotBeNil)
So(sec.Name(), ShouldEqual, "author")

So(f.SectionStrings(), ShouldResemble, []string{ini.DEFAULT_SECTION, "author"})
So(f.SectionStrings(), ShouldResemble, []string{ini.DefaultSection, "author"})

Convey("With duplicated name", func() {
sec, err := f.NewSection("author")
So(err, ShouldBeNil)
So(sec, ShouldNotBeNil)

// Does nothing if section already exists
So(f.SectionStrings(), ShouldResemble, []string{ini.DEFAULT_SECTION, "author"})
So(f.SectionStrings(), ShouldResemble, []string{ini.DefaultSection, "author"})
})

Convey("With empty string", func() {
Expand All @@ -75,15 +75,15 @@ func TestFile_NewRawSection(t *testing.T) {
So(sec, ShouldNotBeNil)
So(sec.Name(), ShouldEqual, "comments")

So(f.SectionStrings(), ShouldResemble, []string{ini.DEFAULT_SECTION, "comments"})
So(f.SectionStrings(), ShouldResemble, []string{ini.DefaultSection, "comments"})
So(f.Section("comments").Body(), ShouldEqual, `1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000`)

Convey("With duplicated name", func() {
sec, err := f.NewRawSection("comments", `1111111111111111111000000000000000001110000`)
So(err, ShouldBeNil)
So(sec, ShouldNotBeNil)
So(f.SectionStrings(), ShouldResemble, []string{ini.DEFAULT_SECTION, "comments"})
So(f.SectionStrings(), ShouldResemble, []string{ini.DefaultSection, "comments"})

// Overwrite previous existed section
So(f.Section("comments").Body(), ShouldEqual, `1111111111111111111000000000000000001110000`)
Expand All @@ -102,13 +102,13 @@ func TestFile_NewSections(t *testing.T) {
So(f, ShouldNotBeNil)

So(f.NewSections("package", "author"), ShouldBeNil)
So(f.SectionStrings(), ShouldResemble, []string{ini.DEFAULT_SECTION, "package", "author"})
So(f.SectionStrings(), ShouldResemble, []string{ini.DefaultSection, "package", "author"})

Convey("With duplicated name", func() {
So(f.NewSections("author", "features"), ShouldBeNil)

// Ignore section already exists
So(f.SectionStrings(), ShouldResemble, []string{ini.DEFAULT_SECTION, "package", "author", "features"})
So(f.SectionStrings(), ShouldResemble, []string{ini.DefaultSection, "package", "author", "features"})
})

Convey("With empty string", func() {
Expand All @@ -119,7 +119,7 @@ func TestFile_NewSections(t *testing.T) {

func TestFile_GetSection(t *testing.T) {
Convey("Get a section", t, func() {
f, err := ini.Load(_FULL_CONF)
f, err := ini.Load(fullConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand All @@ -137,7 +137,7 @@ func TestFile_GetSection(t *testing.T) {

func TestFile_Section(t *testing.T) {
Convey("Get a section", t, func() {
f, err := ini.Load(_FULL_CONF)
f, err := ini.Load(fullConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand Down Expand Up @@ -167,12 +167,12 @@ VERSION = v1`))

func TestFile_Sections(t *testing.T) {
Convey("Get all sections", t, func() {
f, err := ini.Load(_FULL_CONF)
f, err := ini.Load(fullConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

secs := f.Sections()
names := []string{ini.DEFAULT_SECTION, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "string escapes", "advance"}
names := []string{ini.DefaultSection, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "string escapes", "advance"}
So(len(secs), ShouldEqual, len(names))
for i, name := range names {
So(secs[i].Name(), ShouldEqual, name)
Expand Down Expand Up @@ -203,11 +203,11 @@ func TestFile_ChildSections(t *testing.T) {

func TestFile_SectionStrings(t *testing.T) {
Convey("Get all section names", t, func() {
f, err := ini.Load(_FULL_CONF)
f, err := ini.Load(fullConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

So(f.SectionStrings(), ShouldResemble, []string{ini.DEFAULT_SECTION, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "string escapes", "advance"})
So(f.SectionStrings(), ShouldResemble, []string{ini.DefaultSection, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "string escapes", "advance"})
})
}

Expand All @@ -228,20 +228,20 @@ func TestFile_Append(t *testing.T) {
f := ini.Empty()
So(f, ShouldNotBeNil)

So(f.Append(_MINIMAL_CONF, []byte(`
So(f.Append(minimalConf, []byte(`
[author]
NAME = Unknwon`)), ShouldBeNil)

Convey("With bad input", func() {
So(f.Append(123), ShouldNotBeNil)
So(f.Append(_MINIMAL_CONF, 123), ShouldNotBeNil)
So(f.Append(minimalConf, 123), ShouldNotBeNil)
})
})
}

func TestFile_WriteTo(t *testing.T) {
Convey("Write content to somewhere", t, func() {
f, err := ini.Load(_FULL_CONF)
f, err := ini.Load(fullConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand Down Expand Up @@ -297,7 +297,7 @@ test =

func TestFile_SaveTo(t *testing.T) {
Convey("Write content to somewhere", t, func() {
f, err := ini.Load(_FULL_CONF)
f, err := ini.Load(fullConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand Down
10 changes: 6 additions & 4 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ import (
const (
// Name for default section. You can use this constant or the string literal.
// In most of cases, an empty string is all you need to access the section.
DEFAULT_SECTION = "DEFAULT"
DefaultSection = "DEFAULT"
// Deprecated: Use "DefaultSection" instead.
DEFAULT_SECTION = DefaultSection

// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 99
_VERSION = "1.42.0"
depthValues = 99
version = "1.42.1"
)

// Version returns current package version literal.
func Version() string {
return _VERSION
return version
}

var (
Expand Down
2 changes: 1 addition & 1 deletion ini_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func Test_Version(t *testing.T) {
Convey("Get version", t, func() {
So(Version(), ShouldEqual, _VERSION)
So(Version(), ShouldEqual, version)
})
}

Expand Down
34 changes: 17 additions & 17 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

const (
_CONF_DATA = `
confData = `
; Package name
NAME = ini
; Package version
Expand All @@ -43,9 +43,9 @@ const (
Coding addict.
Good man.
""" # Succeeding comment`
_MINIMAL_CONF = "testdata/minimal.ini"
_FULL_CONF = "testdata/full.ini"
_NOT_FOUND_CONF = "testdata/404.ini"
minimalConf = "testdata/minimal.ini"
fullConf = "testdata/full.ini"
notFoundConf = "testdata/404.ini"
)

var update = flag.Bool("update", false, "Update .golden files")
Expand Down Expand Up @@ -78,7 +78,7 @@ NAME = Unknwon

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

Expand Down Expand Up @@ -229,20 +229,20 @@ long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----

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)
f, err := ini.LoadSources(ini.LoadOptions{Loose: true}, notFoundConf, minimalConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Convey("Inverse case", func() {
_, err = ini.Load(_NOT_FOUND_CONF)
_, 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(_MINIMAL_CONF)
f, err := ini.InsensitiveLoad(minimalConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand All @@ -259,7 +259,7 @@ e-mail = [email protected]
})

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

Expand All @@ -272,18 +272,18 @@ 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)
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}, _NOT_FOUND_CONF)
_, 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}, _MINIMAL_CONF)
f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true, Insensitive: true}, minimalConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand All @@ -300,7 +300,7 @@ e-mail = [email protected]
})

Convey("Inverse case", func() {
f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, _MINIMAL_CONF)
f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: true}, minimalConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand Down Expand Up @@ -817,18 +817,18 @@ GITHUB = 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)
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}, _NOT_FOUND_CONF)
_, 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}, _MINIMAL_CONF)
f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false, Insensitive: true}, minimalConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand All @@ -845,7 +845,7 @@ e-mail = [email protected]
})

Convey("Inverse case", func() {
f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, _MINIMAL_CONF)
f, err := ini.LoadSources(ini.LoadOptions{AllowPythonMultilineValues: false}, minimalConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand Down
2 changes: 1 addition & 1 deletion key.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (k *Key) transformValue(val string) string {
if !strings.Contains(val, "%") {
return val
}
for i := 0; i < _DEPTH_VALUES; i++ {
for i := 0; i < depthValues; i++ {
vr := varPattern.FindString(val)
if len(vr) == 0 {
break
Expand Down
4 changes: 2 additions & 2 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func timesEqual(values []time.Time, expected ...time.Time) {

func TestKey_Helpers(t *testing.T) {
Convey("Getting and setting values", t, func() {
f, err := ini.Load(_FULL_CONF)
f, err := ini.Load(fullConf)
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

Expand Down Expand Up @@ -166,7 +166,7 @@ func TestKey_Helpers(t *testing.T) {

Convey("Get sections", func() {
sections := f.Sections()
for i, name := range []string{ini.DEFAULT_SECTION, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "string escapes", "advance"} {
for i, name := range []string{ini.DefaultSection, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "string escapes", "advance"} {
So(sections[i].Name(), ShouldEqual, name)
}
})
Expand Down

0 comments on commit ece0e89

Please sign in to comment.