-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags_test.go
More file actions
74 lines (64 loc) · 1.55 KB
/
flags_test.go
File metadata and controls
74 lines (64 loc) · 1.55 KB
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
package main
import (
"strings"
"testing"
)
func TestFlagsSet(t *testing.T) {
var bf boolFlag
var ui uintFlag
var cs commaStringFlag
testCases := []struct {
fv flagValue
input string
error string
expect string // Only checked if len(expect) > 0
}{
{&bf, "true", "", "true"},
{&bf, "F", "", "false"},
{&bf, "junk", "invalid", ""},
{&ui, "23", "", "23"},
{&ui, "-23", "invalid", ""},
{&ui, "xx23", "invalid", ""},
// These test cases rely on &cs state being retained across calls
{&cs, "a,b", "", "a,b"},
{&cs, "+c,d", "", "a,b,c,d"},
{&cs, "e,f", "", "e,f"},
{&cs, "", "", ""}, // Clear
{&cs, "+c,d", "", "c,d"}, // Append to empty
}
for ix, tc := range testCases {
err := tc.fv.Set(tc.input)
if err != nil && len(tc.error) == 0 {
t.Error(ix, "Unexpected error", err)
continue
}
got := ""
if err != nil {
got = err.Error()
}
if !strings.Contains(got, tc.error) {
t.Errorf("%d Error mismatch. Expected '%s', got '%s'\n", ix, tc.error, got)
continue
}
if len(tc.expect) > 0 {
got := tc.fv.String()
if got != tc.expect {
t.Errorf("%d String() mismatch. Expected '%s', got '%s'\n",
ix, tc.expect, got)
}
}
}
}
func TestFlagMin(t *testing.T) {
var f uintFlag
f.min = 1 // Set minimum to 1
err := f.Set("0") // and try and set to zero
if err == nil {
t.Fatal("Expected an error trying to set a min value to zero")
}
got := err.Error()
exp := "Less than minimum"
if !strings.Contains(got, exp) {
t.Error("Expected 'Less than minimum', but got", got)
}
}