diff --git a/pkg/iniparser.go b/pkg/iniparser.go index 1896fb8..3da5ed5 100644 --- a/pkg/iniparser.go +++ b/pkg/iniparser.go @@ -7,11 +7,14 @@ import ( "errors" "fmt" "os" - "reflect" "sort" "strings" ) +var errEmptyParser = errors.New("this parser has no sections") +var errNoKey = errors.New("key not found") +var errNoSection = errors.New("section not found") + // The section acts as a type representing the value of the iniparser map type section struct { map_ map[string]string @@ -99,19 +102,26 @@ func (i *iniParser) LoadFromFile(path string) error { } // GetSectionNames is a method that returns an array of strings having the names -// of the sections of the caller iniParser object -func (i iniParser) GetSectionNames() []string { +// of the sections of the caller iniParser object and an error in case of empty sections +func (i iniParser) GetSectionNames() ([]string, error) { + if len(i.sections) == 0 { + return make([]string, 0), errEmptyParser + } names := make([]string, 0) for key := range i.sections { names = append(names, key) } - return names + sort.Strings(names) + return names, nil } // GetSections is a method that returns a map[string]section representing -// the data structure of the caller iniParser object -func (i iniParser) GetSections() map[string]section { - return i.sections +// the data structure of the caller iniParser object and an error in case of empty sections +func (i iniParser) GetSections() (map[string]section, error) { + if len(i.sections) == 0 { + return make(map[string]section, 0), errEmptyParser + } + return i.sections, nil } // Get is a method that takes a string for the sectionName and a string for the key @@ -123,11 +133,11 @@ func (i iniParser) GetSections() map[string]section { // else --> nil func (i iniParser) Get(sectionName string, key string) (string, error) { - if reflect.DeepEqual(i.sections[sectionName], section{}) { - return "", errors.New("section not found") + if _, ok := i.sections[sectionName]; !ok { + return "", errNoSection } - if i.sections[sectionName].map_[key] == "" { - return "", errors.New("key not found") + if _, ok := i.sections[sectionName].map_[key]; !ok { + return "", errNoKey } return i.sections[sectionName].map_[key], nil @@ -141,11 +151,11 @@ func (i iniParser) Get(sectionName string, key string) (string, error) { // If the key passed isn't found in the passed section --> "key not found" // else --> nil func (i *iniParser) Set(sectionName string, key string, value string) error { - if reflect.DeepEqual(i.sections[sectionName], section{}) { - return errors.New("section not found") + if _, ok := i.sections[sectionName]; !ok { + return errNoSection } - if i.sections[sectionName].map_[key] == "" { - return errors.New("key not found") + if _, ok := i.sections[sectionName].map_[key]; !ok { + return errNoKey } i.sections[sectionName].map_[key] = value return nil diff --git a/pkg/iniparser_test.go b/pkg/iniparser_test.go index 354c548..5ad3b7c 100644 --- a/pkg/iniparser_test.go +++ b/pkg/iniparser_test.go @@ -154,29 +154,54 @@ func TestLoadFromFile(t *testing.T) { } func TestGetSectionNames(t *testing.T) { - parser := InitParser() - err := parser.LoadFromFile(path) - if err != nil { - t.Errorf("Error! %v", err) - } - names := parser.GetSectionNames() + t.Run("Normal case: sections are not empty", func(t *testing.T) { + + parser := InitParser() + err := parser.LoadFromFile(path) + if err != nil { + t.Errorf("Error! %v", err) + } + names, err := parser.GetSectionNames() - expected := []string{"owner", "database", "section"} + expected := []string{"database", "owner", "section"} - assertEquality(t, expected, names) + assertEquality(t, expected, names) + assertEquality(t, nil, err) + }) + + t.Run("Corner case: sections are empty", func(t *testing.T) { + parser := InitParser() + + _, err := parser.GetSectionNames() + + assertEquality(t, "this parser has no sections", err.Error()) + }) } func TestGetSections(t *testing.T) { - parser := InitParser() - err := parser.LoadFromFile(path) - if err != nil { - t.Errorf("Error! %v", err) - } + t.Run("Normal case: sections are not empty", func(t *testing.T) { + + parser := InitParser() + err := parser.LoadFromFile(path) + if err != nil { + t.Errorf("Error! %v", err) + } + + got, err := parser.GetSections() - got := parser.GetSections() + expected := populateExpectedNormal(t) + assertEquality(t, expected, got) + assertEquality(t, nil, err) + }) + + t.Run("Corner case: sections are empty", func(t *testing.T) { + + parser := InitParser() + + _, err := parser.GetSections() + assertEquality(t, "this parser has no sections", err.Error()) + }) - expected := populateExpectedNormal(t) - assertEquality(t, expected, got) } func TestGet(t *testing.T) {