-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbool_test.go
More file actions
85 lines (73 loc) · 1.88 KB
/
bool_test.go
File metadata and controls
85 lines (73 loc) · 1.88 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
75
76
77
78
79
80
81
82
83
84
85
package rig
import "testing"
func TestBoolValue(t *testing.T) {
for _, test := range []struct {
value bool
expectedString string
}{
{value: true, expectedString: "true"},
{value: false, expectedString: "false"},
} {
b := boolValue(test.value)
if b.String() != test.expectedString {
t.Errorf("Bool(&%v).String() = %q, expected %q", test.value, b, test.expectedString)
}
s := "true"
err := b.Set(s)
if err != nil {
t.Errorf("Bool(&%v).Set(%q): unexpected error: %s", test.value, s, err)
}
if !b {
t.Errorf("Bool(&%v).Set(%q): expected value to be true, got false instead", test.value, s)
}
if !b.IsBoolFlag() {
t.Error("Bool().IsBoolFlag() = false, expected true")
}
}
}
func TestBool(t *testing.T) {
v := true
flag := "flag"
env := "ENV"
usage := "usage"
b := Bool(&v, flag, env, usage)
if b.TypeHint == "" {
t.Error("Bool().TypeHint = \"\": expected .TypeHint to be set")
}
if b.Name != flag {
t.Errorf("Bool(...).Name = %q, expected %q", b.Name, flag)
}
if b.Env != env {
t.Errorf("Bool(...).Env = %q, expected %q", b.Env, env)
}
if b.Usage != usage {
t.Errorf("Bool(...).Usage = %q, expected %q", b.Usage, usage)
}
expectedString := "true"
if b.String() != expectedString {
t.Errorf("Bool(&true)).String() = %q, expected %q", b.String(), expectedString)
}
s := "false"
err := b.Set(s)
if err != nil {
t.Errorf("Bool().Set(%q): unexpected error: %s", s, err)
}
if v {
t.Errorf("Bool(&v).Set(%q): expected v to be false, got %v instead", s, v)
}
s = "notabool"
err = b.Set(s)
if err == nil {
t.Errorf("Bool().Set(%q): expected error, got nil", s)
}
if !b.IsBoolFlag() {
t.Error("Bool().IsBoolFlag() = false, expected true")
}
}
func TestBoolGenerator(t *testing.T) {
g := BoolGenerator()
b := g()
if _, ok := b.(*boolValue); !ok {
t.Errorf("BoolGenerator(): expected type *boolValue, got %T instead", b)
}
}