forked from itzg/go-flagsfiller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddtional_test.go
119 lines (87 loc) · 2.49 KB
/
addtional_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package flagsfiller_test
import (
"flag"
"net"
"net/netip"
"testing"
"time"
"github.com/nextmv-io/go-flagsfiller"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTime(t *testing.T) {
type Config struct {
T time.Time `default:"2010-Oct-01==10:02:03" layout:"2006-Jan-02==15:04:05"`
}
var config Config
filler := flagsfiller.New()
var flagset flag.FlagSet
err := filler.Fill(&flagset, &config)
require.NoError(t, err)
//test default tag
err = flagset.Parse([]string{})
require.NoError(t, err)
expeted, _ := time.Parse("2006-Jan-02==15:04:05", "2010-Oct-01==10:02:03")
assert.Equal(t, expeted, config.T)
err = flagset.Parse([]string{"-t", "2016-Dec-13==16:03:02"})
require.NoError(t, err)
expeted, _ = time.Parse("2006-01-02 15:04:05", "2016-12-13 16:03:02")
assert.Equal(t, expeted, config.T)
}
func TestNetIP(t *testing.T) {
type Config struct {
Addr net.IP
}
var config Config
filler := flagsfiller.New()
var flagset flag.FlagSet
err := filler.Fill(&flagset, &config)
require.NoError(t, err)
err = flagset.Parse([]string{"-addr", "1.2.3.4"})
require.NoError(t, err)
assert.Equal(t, net.ParseIP("1.2.3.4"), config.Addr)
}
func TestMACAddr(t *testing.T) {
type Config struct {
Addr net.HardwareAddr
}
var config Config
filler := flagsfiller.New()
var flagset flag.FlagSet
err := filler.Fill(&flagset, &config)
require.NoError(t, err)
err = flagset.Parse([]string{"-addr", "1c:2a:11:ce:23:45"})
require.NoError(t, err)
assert.Equal(t, net.HardwareAddr{0x1c, 0x2a, 0x11, 0xce, 0x23, 0x45}, config.Addr)
}
func TestIPNet(t *testing.T) {
type Config struct {
Prefix net.IPNet
}
var config Config
filler := flagsfiller.New()
var flagset flag.FlagSet
err := filler.Fill(&flagset, &config)
require.NoError(t, err)
err = flagset.Parse([]string{"-prefix", "192.168.1.0/24"})
require.NoError(t, err)
_, expected, _ := net.ParseCIDR("192.168.1.0/24")
assert.Equal(t, *expected, config.Prefix)
}
func TestTextUnmarshalerType(t *testing.T) {
type Config struct {
Addr netip.Addr `default:"9.9.9.9"`
}
var config Config
filler := flagsfiller.New()
var flagset flag.FlagSet
err := filler.Fill(&flagset, &config)
require.NoError(t, err)
//test default tag
err = flagset.Parse([]string{})
require.NoError(t, err)
assert.Equal(t, netip.AddrFrom4([4]byte{9, 9, 9, 9}), config.Addr)
err = flagset.Parse([]string{"-addr", "1.2.3.4"})
require.NoError(t, err)
assert.Equal(t, netip.AddrFrom4([4]byte{1, 2, 3, 4}), config.Addr)
}