summaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2022-12-03 11:58:01 +0000
committerGrégoire Duchêne <gduchene@awhk.org>2022-12-03 11:58:01 +0000
commit552c8aaa156f39725329c6d00ea715e0312c6fb8 (patch)
tree53cb46ed59c6f129ca5702d256511b3ea9f77787 /flag_test.go
parent5b88af5109405a51b0c6f8237016707604109ca7 (diff)
Add InitFlagSet
Diffstat (limited to 'flag_test.go')
-rw-r--r--flag_test.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/flag_test.go b/flag_test.go
index ccbfadb..584a2c9 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -52,3 +52,62 @@ func TestFlagTSliceVar(s *testing.T) {
t.AssertErrorIs(nil, fs.Parse([]string{"-test=1", "-test=2", "-test=42,84"}))
t.AssertEqual([]int{1, 2, 42, 84}, fl)
}
+
+func TestInitFlagSet(s *testing.T) {
+ t := core.T{T: s}
+
+ for _, tc := range []struct {
+ name string
+ env []string
+ cfg map[string]string
+ args []string
+
+ expInt int
+ expIntSlice []int
+ expStr string
+ expErr error
+ }{
+ {
+ name: "ArgsOnly",
+ args: []string{"-int=42", "-int-slice=42,84"},
+
+ expInt: 42,
+ expIntSlice: []int{42, 84},
+ },
+ {
+ name: "EnvOnly",
+ env: []string{"INT=42", "INT_SLICE=42,84"},
+
+ expInt: 42,
+ expIntSlice: []int{42, 84},
+ },
+ {
+ name: "CfgOnly",
+ cfg: map[string]string{"int": "42", "int-slice": "42,84"},
+
+ expInt: 42,
+ expIntSlice: []int{42, 84},
+ },
+ {
+ name: "InOrder",
+ env: []string{"STRING=Hello World!"},
+ cfg: map[string]string{"string": "Hello Universe!", "int-slice": "42,84"},
+ args: []string{"-int=42", "-int-slice=21,42"},
+
+ expInt: 42,
+ expIntSlice: []int{21, 42},
+ expStr: "Hello Universe!",
+ },
+ } {
+ t.Run(tc.name, func(t *core.T) {
+ fs := flag.NewFlagSet("", flag.PanicOnError)
+ fi := fs.Int("int", 0, "")
+ fl := core.FlagTSlice(fs, "int-slice", nil, "", strconv.Atoi, ",")
+ fm := fs.String("string", "", "")
+ t.AssertErrorIs(tc.expErr, core.InitFlagSet(fs, tc.env, tc.cfg, tc.args))
+ t.AssertEqual(tc.expInt, *fi)
+ t.AssertEqual(tc.expIntSlice, *fl)
+ t.AssertEqual(tc.expStr, *fm)
+ })
+ }
+}