Merge pull request #65 from zimmski/fix-default-values
Fix map default values and IniIncludeDefaults
diff --git a/group_private.go b/group_private.go
index 87c0f15..5242f5d 100644
--- a/group_private.go
+++ b/group_private.go
@@ -61,7 +61,15 @@
continue
}
- option.defaultValue = reflect.ValueOf(option.value.Interface())
+ if option.value.Kind() == reflect.Map {
+ option.defaultValue = reflect.MakeMap(option.value.Type())
+
+ for _, k := range option.value.MapKeys() {
+ option.defaultValue.SetMapIndex(k, option.value.MapIndex(k))
+ }
+ } else {
+ option.defaultValue = reflect.ValueOf(option.value.Interface())
+ }
}
}
diff --git a/ini_private.go b/ini_private.go
index 9f56657..941ffd3 100644
--- a/ini_private.go
+++ b/ini_private.go
@@ -80,8 +80,7 @@
val := option.value
- if (options&IniIncludeDefaults) == IniNone &&
- reflect.DeepEqual(val, option.defaultValue) {
+ if (options&IniIncludeDefaults) == IniNone && reflect.DeepEqual(val.Interface(), option.defaultValue.Interface()) {
continue
}
diff --git a/ini_test.go b/ini_test.go
index 7f78907..d4e2d5b 100644
--- a/ini_test.go
+++ b/ini_test.go
@@ -7,24 +7,30 @@
)
func TestWriteIni(t *testing.T) {
- var opts helpOptions
+ var tests = []struct {
+ args []string
+ options IniOptions
+ expected string
+ }{
+ {
+ []string{"-vv", "--intmap=a:2", "--intmap", "b:3", "command"},
+ IniDefault,
+ `[Application Options]
+; Show verbose debug information
+verbose = true
+verbose = true
- p := NewNamedParser("TestIni", Default)
- p.AddGroup("Application Options", "The application options", &opts)
+[Other Options]
+; A map from string to int
+int-map = a:2
+int-map = b:3
- _, err := p.ParseArgs([]string{"-vv", "--intmap=a:2", "--intmap", "b:3", "command"})
-
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
-
- inip := NewIniParser(p)
-
- var b bytes.Buffer
- inip.Write(&b, IniDefault|IniIncludeDefaults)
-
- got := b.String()
- expected := `[Application Options]
+`,
+ },
+ {
+ []string{"-vv", "--intmap=a:2", "--intmap", "b:3", "command"},
+ IniDefault | IniIncludeDefaults,
+ `[Application Options]
; Show verbose debug information
verbose = true
verbose = true
@@ -53,38 +59,12 @@
; Use for extra verbosity
; ExtraVerbose =
-`
-
- if got != expected {
- ret, err := helpDiff(got, expected)
-
- if err != nil {
- t.Errorf("Unexpected ini, expected:\n\n%s\n\nbut got\n\n%s", expected, got)
- } else {
- t.Errorf("Unexpected ini:\n\n%s", ret)
- }
- }
-}
-
-func TestWriteIniCommentDefaults(t *testing.T) {
- var opts helpOptions
-
- p := NewNamedParser("TestIni", Default)
- p.AddGroup("Application Options", "The application options", &opts)
-
- _, err := p.ParseArgs([]string{"command"})
-
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
-
- inip := NewIniParser(p)
-
- var b bytes.Buffer
- inip.Write(&b, IniDefault|IniIncludeDefaults|IniCommentDefaults)
-
- got := b.String()
- expected := `[Application Options]
+`,
+ },
+ {
+ []string{"command"},
+ IniDefault | IniIncludeDefaults | IniCommentDefaults,
+ `[Application Options]
; Show verbose debug information
; verbose =
@@ -111,15 +91,38 @@
; Use for extra verbosity
; ExtraVerbose =
-`
+`,
+ },
+ }
- if got != expected {
- ret, err := helpDiff(got, expected)
+ for _, test := range tests {
+ var opts helpOptions
+
+ p := NewNamedParser("TestIni", Default)
+ p.AddGroup("Application Options", "The application options", &opts)
+
+ _, err := p.ParseArgs(test.args)
if err != nil {
- t.Errorf("Unexpected ini, expected:\n\n%s\n\nbut got\n\n%s", expected, got)
- } else {
- t.Errorf("Unexpected ini:\n\n%s", ret)
+ t.Fatalf("Unexpected error: %v", err)
+ }
+
+ inip := NewIniParser(p)
+
+ var b bytes.Buffer
+ inip.Write(&b, test.options)
+
+ got := b.String()
+ expected := test.expected
+
+ if got != expected {
+ ret, err := helpDiff(got, expected)
+
+ if err != nil {
+ t.Errorf("Unexpected ini with arguments %+v and ini options %b, expected:\n\n%s\n\nbut got\n\n%s", test.args, test.options, expected, got)
+ } else {
+ t.Errorf("Unexpected ini with arguments %+v and ini options %b:\n\n%s", test.args, test.options, ret)
+ }
}
}
}