File: struct_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 (163 lines) | stat: -rw-r--r-- 3,805 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package kingpin

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestFlagsInvalidType(t *testing.T) {
	type MyFlags struct {
		Debug bool `help:"foo"`
	}

	a := newTestApp()
	err := a.Struct(MyFlags{})
	assert.Error(t, err)
	err = a.Struct(&MyFlags{})
	assert.NoError(t, err)
}

func TestFlagsStruct(t *testing.T) {
	type MyFlags struct {
		Debug bool     `help:"Enable debug mode."`
		URL   string   `help:"URL to connect to." default:"localhost:80"`
		Names []string `help:"Names of things."`
	}
	a := newTestApp()
	actual := &MyFlags{}
	err := a.Struct(actual)
	assert.NoError(t, err)
	assert.NotNil(t, a.flagGroup.long["debug"])
	assert.NotNil(t, a.flagGroup.long["url"])

	*actual = MyFlags{}
	a.Parse([]string{})
	assert.Equal(t, &MyFlags{URL: "localhost:80"}, actual)

	*actual = MyFlags{}
	a.Parse([]string{"--debug"})
	assert.Equal(t, &MyFlags{Debug: true, URL: "localhost:80"}, actual)

	*actual = MyFlags{}
	a.Parse([]string{"--url=w3.org"})
	assert.Equal(t, &MyFlags{URL: "w3.org"}, actual)

	*actual = MyFlags{}
	a.Parse([]string{"--names=alec", "--names=bob"})
	assert.Equal(t, &MyFlags{URL: "localhost:80", Names: []string{"alec", "bob"}}, actual)

	type RequiredFlag struct {
		Flag bool `help:"A flag." required:"true"`
	}

	a = newTestApp()
	rflags := &RequiredFlag{}
	err = a.Struct(rflags)
	assert.NoError(t, err)
	_, err = a.Parse([]string{})
	assert.Error(t, err)
	_, err = a.Parse([]string{"--flag"})
	assert.NoError(t, err)
	assert.Equal(t, &RequiredFlag{Flag: true}, rflags)

	type DurationFlag struct {
		Elapsed time.Duration `help:"Elapsed time."`
	}

	a = newTestApp()
	dflag := &DurationFlag{}
	err = a.Struct(dflag)
	assert.NoError(t, err)
	_, err = a.Parse([]string{"--elapsed=5s"})
	assert.NoError(t, err)
	assert.Equal(t, 5*time.Second, dflag.Elapsed)
}

func TestNestedStruct(t *testing.T) {
	type NestedFlags struct {
		URL string `help:"URL to connect to." default:"localhost:80"`
	}

	type MyFlags struct {
		NestedFlags
		Debug bool `help:"Enable debug mode"`
	}

	a := newTestApp()
	actual := &MyFlags{}
	err := a.Struct(actual)
	assert.NoError(t, err)

	assert.NotNil(t, a.GetFlag("debug"))
	assert.NotNil(t, a.GetFlag("url"))

	_, err = a.Parse([]string{"--debug", "--url=foobar"})
	assert.NoError(t, err)
	assert.True(t, actual.Debug)
	assert.Equal(t, "foobar", actual.URL)
}

func TestStructHierarchy(t *testing.T) {
	type App struct {
		Login struct {
			Username string `arg:"true" required:"true"`
		}
		Debug bool
	}
	a := newTestApp()
	actual := &App{}
	err := a.Struct(actual)
	assert.NoError(t, err)
	expected := &App{
		Login: struct {
			Username string `arg:"true" required:"true"`
		}{
			Username: "alec",
		},
		Debug: true,
	}
	_, err = a.Parse([]string{"--debug", "login", "alec"})
	assert.NoError(t, err)
	assert.Equal(t, expected, actual)
}

func TestStructEnum(t *testing.T) {
	type App struct {
		Enum  string   `enum:"one,two"`
		Enums []string `enum:"one,two"`
	}
	actual := &App{}
	a := newTestApp()
	a.Struct(actual)
	_, err := a.Parse([]string{"--enum=three"})
	assert.Error(t, err)
	_, err = a.Parse([]string{"--enums=three"})
	assert.Error(t, err)
	_, err = a.Parse([]string{"--enum=one", "--enums=one", "--enums=two"})
	assert.NoError(t, err)
	assert.Equal(t, &App{Enum: "one", Enums: []string{"one", "two"}}, actual)
}

type onActionStructTest struct {
	Debug bool

	called int
}

func (o *onActionStructTest) OnDebug(element *ParseElement, context *ParseContext) error {
	o.called++
	return nil
}

func TestStructOnAction(t *testing.T) {
	actual := &onActionStructTest{}
	var _ Action = actual.OnDebug
	a := newTestApp()
	err := a.Struct(actual)
	assert.NoError(t, err)
	_, err = a.Parse([]string{"--debug"})
	assert.NoError(t, err)
	assert.Equal(t, &onActionStructTest{Debug: true, called: 1}, actual)
}