Skip to content

Commit

Permalink
refactor : error variables added
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMostafa08 committed Sep 11, 2024
1 parent b1f5ab4 commit d2763d6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 31 deletions.
40 changes: 25 additions & 15 deletions pkg/iniparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
57 changes: 41 additions & 16 deletions pkg/iniparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d2763d6

Please sign in to comment.