forked from dynport/gocli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
args_test.go
140 lines (120 loc) · 3.89 KB
/
args_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gocli
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestParse(t *testing.T) {
args := &Args{}
args.String("-h")
args.Parse([]string{"droplets", "create", "-h=some.host"})
assert.Equal(t, args.Args, []string{"droplets", "create"})
assert.Equal(t, args.Get("-h"), []string{"some.host"})
args.Parse([]string{"droplets", "create", "-h", "some.host"})
assert.Equal(t, args.Args, []string{"droplets", "create"})
assert.Equal(t, args.Get("-h"), []string{"some.host"})
}
func TestParseBool(t *testing.T) {
args := &Args{}
args.Bool("--rack")
e := args.Parse([]string{"droplets", "create", "--rack"})
assert.Nil(t, e)
assert.Equal(t, args.Args, []string{"droplets", "create"})
assert.Equal(t, args.GetBool("--rack"), true)
}
func TestNotRegistered(t *testing.T) {
args := &Args{}
assert.Nil(t, args.Parse([]string{"droplets", "create"}))
assert.NotNil(t, args.Parse([]string{"droplets", "create", "--rack"}))
}
func TestStringWithoutDefault(t *testing.T) {
args := &Args{}
args.RegisterString("--host", "host", true, "", "Docker Host to be used")
args.Parse([]string{"a", "b"})
_, e := args.GetString("--host")
assert.NotNil(t, e)
}
func TestRegister(t *testing.T) {
args := NewArgs(map[string]*Flag{
"-h": {Type: STRING, Required: false, DefaultValue: "some.host", Description: "Some Description"},
},
)
assert.NotNil(t, args)
ty, _ := args.TypeOf("-h")
assert.Equal(t, ty, "string")
}
func TestStringWithDefault(t *testing.T) {
args := &Args{}
args.RegisterString("--host", "host", false, "default.host", "Docker Host to be used")
args.RegisterBool("--rack", "rack", false, false, "Use as rack application")
args.Parse([]string{"a", "b"})
v, e := args.GetString("--host")
assert.Nil(t, e)
assert.Equal(t, v, "default.host")
}
func TestUsage(t *testing.T) {
args := &Args{}
args.RegisterString("--host", "host", false, "default.host", "Docker Host to be used")
args.RegisterBool("--rack", "rack", true, false, "Use as rack application")
s := args.Usage()
assert.NotNil(t, s)
assert.Contains(t, s, "--rack")
}
func TestRegisterFlag(t *testing.T) {
args := &Args{}
args.RegisterFlag(&Flag{CliFlag: "--host", Type: STRING})
args.RegisterFlag(&Flag{CliFlag: "--help", Type: STRING})
args.RegisterFlag(&Flag{CliFlag: "--enabled", Type: BOOL})
assert.Equal(t, len(args.lookup("--h")), 2)
assert.Equal(t, len(args.lookup("--h")), 2)
assert.Equal(t, len(args.lookup("--ho")), 1)
assert.Equal(t, len(args.lookup("--host")), 1)
}
func TestRegisterInt(t *testing.T) {
args := &Args{}
args.RegisterInt("-i", "image", false, 10, "I id")
args.RegisterInt("-a", "a", false, 30, "A id")
args.Parse([]string{"-i", "20"})
v, _ := args.GetInt("-i")
assert.Equal(t, v, 20)
v, _ = args.GetInt("-a")
assert.Equal(t, v, 30)
}
func TestAttributesMap(t *testing.T) {
args := NewArgs(FlagMap{
"-v": {Type: STRING, DefaultValue: "1.2.3", Key: "version"},
"--host": {Type: STRING},
"--user": {Type: STRING},
"--enabled": {Type: BOOL, DefaultValue: ""},
"--flush": {Type: BOOL, DefaultValue: "true"},
})
args.Parse([]string{"-v", "1.2.1", "--host", "localhost"})
m := args.AttributesMap()
assert.Equal(t, len(m), 3)
assert.Equal(t, m, map[string]string{
"version": "1.2.1",
"host": "localhost",
"flush": "true",
})
}
func TestAttributesMapWithMultipleArgs(t *testing.T) {
args := NewArgs(FlagMap{
"-H": {Type: STRING, Key: "hosts"},
})
args.Parse([]string{"-H", "host1", "-H", "host2"})
m := args.AttributesMap()
assert.Equal(t, m["hosts"], "host1,host2")
}
func TestRegisterBool(t *testing.T) {
args := &Args{}
args.RegisterBool("--disabled", "disabled", false, false, "Disabled")
tp, _ := args.TypeOf("--disabled")
assert.Equal(t, tp, "bool")
e := args.Parse([]string{"--disabled"})
assert.Nil(t, e)
res := args.GetBool("--disabled")
assert.Equal(t, res, true)
}
func TestRegisterArgs(t *testing.T) {
args := &Args{}
args.RegisterArgs("command host")
}