diff --git a/pkg/iniparser.go b/pkg/iniparser.go index 495a22c..37f8aec 100644 --- a/pkg/iniparser.go +++ b/pkg/iniparser.go @@ -162,9 +162,11 @@ func (i iniParser) Set(sectionName string, key string, value string) error { return nil } + // ToString is a method that returns the parsed ini map of the caller object as one string // The returned string won't include the comments -func (i iniParser) ToString() string { +// Also,it tells fmt pkg how to print the object +func (i iniParser) String() string { var result string sectionNames := make([]string, 0) for sectionName := range i.sections { @@ -183,7 +185,7 @@ func (i iniParser) ToString() string { result += key + " = " + i.sections[sectionName].map_[key] + "\n" } } - return result + return fmt.Sprint(result) } // SaveToFile is a method that takes a path to an output file and returns an error @@ -200,7 +202,7 @@ func (i iniParser) SaveToFile(path string) error { } defer file.Close() - stringFile := i.ToString() + stringFile := i.String() _, err = file.WriteString(stringFile) if err != nil { return fmt.Errorf("error in writing to the file: %v", err) diff --git a/pkg/iniparser_test.go b/pkg/iniparser_test.go index 5ad3b7c..9316c65 100644 --- a/pkg/iniparser_test.go +++ b/pkg/iniparser_test.go @@ -1,6 +1,7 @@ package iniparser import ( + "fmt" "os" "reflect" "testing" @@ -301,16 +302,17 @@ func TestSet(t *testing.T) { } -func TestToString(t *testing.T) { +func TestString(t *testing.T) { parser1 := InitParser() parser2 := InitParser() parser1.LoadFromString(stringINI) - got := parser1.ToString() + got := parser1.String() parser2.LoadFromString(got) assertEquality(t, parser1.sections, parser2.sections) + assertEquality(t, fmt.Sprint(parser1), fmt.Sprint(parser2)) } func TestSaveToFile(t *testing.T) { @@ -326,6 +328,6 @@ func TestSaveToFile(t *testing.T) { t.Errorf("Error! %v", err) } - stringFile := parser.ToString() + stringFile := parser.String() assertFile(t, outPath, stringFile) }