-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvar_test.go
More file actions
91 lines (83 loc) · 1.84 KB
/
var_test.go
File metadata and controls
91 lines (83 loc) · 1.84 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
86
87
88
89
90
91
package rig
import (
"errors"
"flag"
"reflect"
"testing"
"github.com/Pimmr/rig/validators"
)
func newStringValue(s string) *stringValue {
v := new(stringValue)
*v = stringValue(s)
return v
}
func newIntValue(i int) *intValue {
v := new(intValue)
*v = intValue(i)
return v
}
func TestVar(t *testing.T) {
testValidator := func(shouldFail bool) validators.Var {
return func(flag.Value) error {
if shouldFail {
return errors.New("failing validator")
}
return nil
}
}
for _, test := range []struct {
val flag.Value
validators []validators.Var
input string
expected flag.Value
expectError bool
}{
{
val: newStringValue(""),
input: "foo",
expected: newStringValue("foo"),
expectError: false,
},
{
val: newIntValue(0),
input: "42",
expected: newIntValue(42),
expectError: false,
},
{
val: newIntValue(0),
input: "notanint",
expectError: true,
},
{
val: newIntValue(0),
validators: []validators.Var{testValidator(false)},
input: "42",
expected: newIntValue(42),
expectError: false,
},
{
val: newIntValue(0),
validators: []validators.Var{testValidator(true)},
input: "42",
expectError: true,
},
} {
v := Var(test.val, "flag", "ENV", "testing Var", test.validators...)
err := v.Set(test.input)
if test.expectError && err == nil {
t.Errorf("Var(%T).Set(%q): expected error, got nil instead", test.val, test.input)
continue
}
if !test.expectError && err != nil {
t.Errorf("Var(%T).Set(%q): unexpected error: %s", test.val, test.input, err)
continue
}
if err != nil {
continue
}
if !reflect.DeepEqual(test.val, test.expected) {
t.Errorf("Var(%T).Set(%q) = %+v, expected %+v", test.val, test.input, test.val, test.expected)
}
}
}