diff options
Diffstat (limited to '')
-rw-r--r-- | flagset_test.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/flagset_test.go b/flagset_test.go new file mode 100644 index 0000000..bef4dd2 --- /dev/null +++ b/flagset_test.go @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2020 Ethel Morgan +// +// SPDX-License-Identifier: MIT + +package flag + +import ( + "testing" + "time" +) + +func TestFlagSetCustom(t *testing.T) { + fs := NewFlagSet("test", ContinueOnError) + + i := fs.Int("i", 0, "i") + d := fs.Custom("d", "1s", "d", func(raw string) (interface{}, error) { + return time.ParseDuration(raw) + }) + e := fs.Custom("e", "", "e", StringEnum("json", "table")) + + if err := fs.Parse([]string{"-i", "12", "-d", "3m", "-e", "json"}); err != nil { + t.Fatalf("got error: %v", err) + } + + if *i != 12 { + t.Errorf("-i == %v, wanted %v", *i, 12) + } + if (*d).(time.Duration) != 3*time.Minute { + t.Errorf("-d == %v, wanted %v", *d, 3*time.Minute) + } + if (*e).(string) != "json" { + t.Errorf("-e == %v, wanted %v", *e, "json") + } +} |