-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag_reflect_bool_test.go
77 lines (67 loc) · 2.04 KB
/
flag_reflect_bool_test.go
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
package clif
import (
"context"
"errors"
"reflect"
"strconv"
"testing"
)
func TestNewValueFromBoolean_success(t *testing.T) {
t.Parallel()
type testCase struct {
input FlagValue
expected bool
}
testCases := map[string]testCase{
"true": {input: FlagValue{Set: true, Raw: "true"}, expected: true},
"TRUE": {input: FlagValue{Set: true, Raw: "TRUE"}, expected: true},
"t": {input: FlagValue{Set: true, Raw: "t"}, expected: true},
"1": {input: FlagValue{Set: true, Raw: "1"}, expected: true},
"false": {input: FlagValue{Set: true, Raw: "false"}, expected: false},
"FALSE": {input: FlagValue{Set: true, Raw: "FALSE"}, expected: false},
"f": {input: FlagValue{Set: true, Raw: "f"}, expected: false},
"0": {input: FlagValue{Set: true, Raw: "0"}, expected: false},
"toggle": {input: FlagValue{Set: false, Raw: ""}, expected: true},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var target bool
res, err := newValueFromBoolean(ctx, test.input, reflect.ValueOf(target))
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
got := res.Bool()
if got != test.expected {
t.Fatalf("Expected %v, got %v", test.expected, got)
}
})
}
}
func TestNewValueFromBoolean_error(t *testing.T) {
t.Parallel()
testCases := map[string]string{
"empty": "",
"mixedCase": "trUE",
"invalidValue": "yes",
}
for name, input := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var target bool
_, err := newValueFromBoolean(ctx, FlagValue{Set: true, Raw: input}, reflect.ValueOf(target))
if err == nil {
t.Fatal("Expected error, got none")
}
numError := &strconv.NumError{}
if !errors.As(err, &numError) {
t.Fatalf("Expected strconv.NumError, got %T: %v", err, err)
}
if !errors.Is(numError.Err, strconv.ErrSyntax) {
t.Fatalf("Expected strconv.NumError to be reporting a strconv.ErrSyntax, got %v instead", numError.Err)
}
})
}
}