forked from sgreben/flagvar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalternative_test.go
59 lines (51 loc) · 1016 Bytes
/
alternative_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
package flagvar_test
import (
"flag"
"net/url"
"reflect"
"testing"
"time"
"github.com/sgreben/flagvar"
)
func TestAlternativeEither(t *testing.T) {
uv := &flagvar.URL{}
tv := &flagvar.Time{Layout: time.Kitchen}
fv := flagvar.Alternative{
Either: uv,
Or: tv,
}
fs := flag.FlagSet{}
fs.Var(&fv, "url-or-time", "")
u, _ := url.Parse("https://github.com/sgreben/flagvar")
err := fs.Parse([]string{"-url-or-time", u.String()})
if err != nil {
t.Fail()
}
if !fv.EitherOk {
t.Fail()
}
if !reflect.DeepEqual(uv.Value, u) {
t.Fail()
}
}
func TestAlternativeOr(t *testing.T) {
uv := &flagvar.URL{}
tv := &flagvar.Time{Layout: time.Kitchen}
fv := flagvar.Alternative{
Either: uv,
Or: tv,
}
fs := flag.FlagSet{}
fs.Var(&fv, "url-or-time", "")
u, _ := time.Parse(time.Kitchen, "10:30AM")
err := fs.Parse([]string{"-url-or-time", u.Format(time.Kitchen)})
if err != nil {
t.Fail()
}
if fv.EitherOk {
t.Fail()
}
if !reflect.DeepEqual(tv.Value, u) {
t.Fail()
}
}