File: values_test.go

package info (click to toggle)
golang-gopkg-alecthomas-kingpin.v3 3.0~git20180227.b8d601d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 420 kB
  • sloc: makefile: 6
file content (109 lines) | stat: -rw-r--r-- 2,813 bytes parent folder | download
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
package kingpin

import (
	"net"

	"github.com/stretchr/testify/assert"

	"testing"
)

func TestAccumulatorStrings(t *testing.T) {
	target := []string{}
	acc := newAccumulator(&target, nil, func(v interface{}) Value { return newStringValue(v.(*string)) })
	acc.Set("a")
	assert.Equal(t, []string{"a"}, target)
	acc.Set("b")
	assert.Equal(t, []string{"a", "b"}, target)
}

func TestAccumulatorSeparator(t *testing.T) {
	target := []string{}
	acc := newAccumulator(&target, []AccumulatorOption{Separator(",")}, func(v interface{}) Value {
		return newStringValue(v.(*string))
	})
	acc.Set("a,b")
	assert.Equal(t, []string{"a", "b"}, target)
	acc.Set("c,d")
	assert.Equal(t, []string{"a", "b", "c", "d"}, target)
}

func TestStrings(t *testing.T) {
	app := New("", "")
	app.Arg("a", "").Required().String()
	app.Arg("b", "").Required().String()
	c := app.Arg("c", "").Required().Strings()
	app.Parse([]string{"a", "b", "a", "b"})
	assert.Equal(t, []string{"a", "b"}, *c)
}

func TestEnum(t *testing.T) {
	app := New("", "")
	a := app.Arg("a", "").Enum("one", "two", "three")
	_, err := app.Parse([]string{"moo"})
	assert.Error(t, err)
	_, err = app.Parse([]string{"one"})
	assert.NoError(t, err)
	assert.Equal(t, "one", *a)
}

func TestEnumVar(t *testing.T) {
	app := New("", "")
	var a string
	app.Arg("a", "").EnumVar(&a, "one", "two", "three")
	_, err := app.Parse([]string{"moo"})
	assert.Error(t, err)
	_, err = app.Parse([]string{"one"})
	assert.NoError(t, err)
	assert.Equal(t, "one", a)
}

func TestCounter(t *testing.T) {
	app := New("", "")
	c := app.Flag("f", "").Counter()
	_, err := app.Parse([]string{"--f", "--f", "--f"})
	assert.NoError(t, err)
	assert.Equal(t, 3, *c)
}

func TestHexBytes(t *testing.T) {
	app := newTestApp()
	actual := app.Arg("bytes", "").HexBytes()
	_, err := app.Parse([]string{"01020aff"})
	assert.NoError(t, err)
	assert.Equal(t, []byte{0x01, 0x02, 0x0a, 0xff}, *actual)
}

func TestSetValueDoesNotReset(t *testing.T) {
	app := newTestApp()
	mapping := map[string]string{
		"key": "value",
	}
	app.Flag("set", "").StringMapVar(&mapping)
	assert.NotEmpty(t, mapping)
}

func TestIPv4Addr(t *testing.T) {
	app := newTestApp()
	flag := app.Flag("addr", "").IP()
	_, err := app.Parse([]string{"--addr", net.IPv4(1, 2, 3, 4).String()})
	assert.NoError(t, err)
	assert.NotNil(t, *flag)
	assert.Equal(t, net.IPv4(1, 2, 3, 4), *flag)
}

func TestInvalidIPv4Addr(t *testing.T) {
	app := newTestApp()
	app.Flag("addr", "").IP()
	_, err := app.Parse([]string{"--addr", "1.2.3.256"})
	assert.Error(t, err)
}

func TestIPv6Addr(t *testing.T) {
	app := newTestApp()
	flag := app.Flag("addr", "").IP()
	_, err := app.Parse([]string{"--addr", net.IPv6interfacelocalallnodes.String()})
	assert.NoError(t, err)
	assert.NotNil(t, *flag)
	assert.Equal(t, net.IPv6interfacelocalallnodes, *flag)
}