forked from alecthomas/kingpin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers_test.go
98 lines (85 loc) · 2.03 KB
/
parsers_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
package kingpin
import (
"io/ioutil"
"net"
"net/url"
"os"
"github.com/alecthomas/assert"
"testing"
)
func TestParseStrings(t *testing.T) {
p := parserMixin{}
v := p.Strings()
p.value.Set("a")
p.value.Set("b")
assert.Equal(t, []string{"a", "b"}, *v)
}
func TestStringsStringer(t *testing.T) {
target := []string{}
v := newAccumulator(&target, func(v interface{}) Value { return newStringValue(v.(*string)) })
v.Set("hello")
v.Set("world")
assert.Equal(t, "hello,world", v.String())
}
func TestParseStringMap(t *testing.T) {
p := parserMixin{}
v := p.StringMap()
p.value.Set("a:b")
p.value.Set("b:c")
assert.Equal(t, map[string]string{"a": "b", "b": "c"}, *v)
}
func TestParseIP(t *testing.T) {
p := parserMixin{}
v := p.IP()
p.value.Set("10.1.1.2")
ip := net.ParseIP("10.1.1.2")
assert.Equal(t, ip, *v)
}
func TestParseURL(t *testing.T) {
p := parserMixin{}
v := p.URL()
p.value.Set("http://w3.org")
u, err := url.Parse("http://w3.org")
assert.NoError(t, err)
assert.Equal(t, *u, **v)
}
func TestParseExistingFile(t *testing.T) {
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
p := parserMixin{}
v := p.ExistingFile()
err = p.value.Set(f.Name())
assert.NoError(t, err)
assert.Equal(t, f.Name(), *v)
err = p.value.Set("/etc/hostsDEFINITELYMISSING")
assert.Error(t, err)
}
func TestParseTCPAddr(t *testing.T) {
p := parserMixin{}
v := p.TCP()
err := p.value.Set("127.0.0.1:1234")
assert.NoError(t, err)
expected, err := net.ResolveTCPAddr("tcp", "127.0.0.1:1234")
assert.NoError(t, err)
assert.Equal(t, *expected, **v)
}
func TestParseTCPAddrList(t *testing.T) {
p := parserMixin{}
_ = p.TCPList()
err := p.value.Set("127.0.0.1:1234")
assert.NoError(t, err)
err = p.value.Set("127.0.0.1:1235")
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1:1234,127.0.0.1:1235", p.value.String())
}
func TestFloat32(t *testing.T) {
p := parserMixin{}
v := p.Float32()
err := p.value.Set("123.45")
assert.NoError(t, err)
assert.InEpsilon(t, 123.45, *v, 0.001)
}