Skip to content

Commit

Permalink
file: add HasSection method (#308)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Chen <[email protected]>
  • Loading branch information
bryanpedini and unknwon authored Nov 30, 2021
1 parent e641746 commit b505416
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ func (f *File) GetSection(name string) (*Section, error) {
return secs[0], err
}

// HasSection returns true if the file contains a section with given name.
func (f *File) HasSection(name string) bool {
section, _ := f.GetSection(name)
return section != nil
}

// SectionsByName returns all sections with given name.
func (f *File) SectionsByName(name string) ([]*Section, error) {
if len(name) == 0 {
Expand Down
14 changes: 14 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ func TestFile_GetSection(t *testing.T) {
})
}

func TestFile_HasSection(t *testing.T) {
f, err := Load(fullConf)
require.NoError(t, err)
require.NotNil(t, f)

sec := f.HasSection("author")
assert.True(t, sec)

t.Run("section not exists", func(t *testing.T) {
nonexistent := f.HasSection("404")
assert.False(t, nonexistent)
})
}

func TestFile_Section(t *testing.T) {
t.Run("get a section", func(t *testing.T) {
f, err := Load(fullConf)
Expand Down

0 comments on commit b505416

Please sign in to comment.