aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
blob: 1cc9ba8ea769d1e33eac16bfbcbe53e4b09ab9b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-FileCopyrightText: © 2022 Grégoire Duchêne <gduchene@awhk.org>
// SPDX-License-Identifier: ISC

package core_test

import (
	"flag"
	"strconv"
	"testing"

	"go.awhk.org/core"
)

func TestFeature_Disable(t *testing.T) {
	f := core.Feature{}
	f.Disable()
	(&core.T{T: t}).AssertEqual(false, f.Enabled())
}

func TestFeature_Enable(t *testing.T) {
	f := core.Feature{}
	f.Enable()
	(&core.T{T: t}).AssertEqual(true, f.Enabled())
}

func TestFlag(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	fl := core.Flag(fs, "test", 42, "", strconv.Atoi)
	t.AssertEqual(42, *fl)
	t.AssertErrorIs(nil, fs.Parse([]string{"-test=84"}))
	t.AssertEqual(84, *fl)
}

func TestFlagFeature(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	ff := core.FlagFeature(fs, "some-feature", false, "")
	t.AssertErrorIs(nil, fs.Parse([]string{"-some-feature"}))
	t.AssertEqual(true, ff.Enabled())
}

func TestFlagVar(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	fl := 42
	core.FlagVar(fs, &fl, "test", "", strconv.Atoi)
	t.AssertEqual(42, fl)
	t.AssertErrorIs(nil, fs.Parse([]string{"-test=84"}))
	t.AssertEqual(84, fl)
}

func TestFlagSlice(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	fl := core.FlagSlice(fs, "test", []int{42}, "", strconv.Atoi, ",")
	t.AssertEqual([]int{42}, *fl)
	t.AssertErrorIs(nil, fs.Parse([]string{"-test=1", "-test=2", "-test=42,84"}))
	t.AssertEqual([]int{1, 2, 42, 84}, *fl)
}

func TestFlagSliceVar(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	fl := []int{42}
	core.FlagSliceVar(fs, &fl, "test", "", strconv.Atoi, ",")
	t.AssertEqual([]int{42}, fl)
	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.FlagSlice(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)
		})
	}

	t.Run("NoMutableFlagValue", func(t *core.T) {
		fs := flag.NewFlagSet("", flag.PanicOnError)
		fi := fs.Int("int", 0, "")
		t.AssertErrorIs(nil, core.InitFlagSet(fs, nil, nil, []string{"-int=42"}))
		t.AssertEqual(42, *fi)
		t.AssertErrorIs(nil, core.InitFlagSet(fs, nil, nil, []string{"-int=21"}))
		t.AssertEqual(42, *fi)
	})
}