-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands_test.go
61 lines (46 loc) · 1.65 KB
/
commands_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
package configstruct
import (
"github.com/stretchr/testify/assert"
"testing"
)
type rootCmdConfig struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"`
Debug bool `env:"CONFIGSTRUCT_DEBUG" cli:"debug" usage:"debug mode"`
FloatValue float64 `env:"CONFIGSTRUCT_FLOAT" cli:"floatValue" usage:"float value"`
}
type testStruct struct {
testValue string
}
type subCmdConfig struct {
Number int `cli:"number" usage:"number to count"`
}
func TestCommand_ParseAndRun(t *testing.T) {
args := []string{"cliName", "-hostname", "localhost", "math", "count", "-number", "2"}
var rootConfig rootCmdConfig
var countConfig subCmdConfig
countCmd := NewCommand("count", "Count numbers", &countConfig, func(cmd *Command, cfg interface{}) error {
cfgValues := cfg.(*subCmdConfig)
t.Log("count command", cfgValues.Number)
return nil
})
mathCmd := NewCommand("math", "Mathematical functions", nil, nil, countCmd)
cmd := NewCommand("", "Test CLI", &rootConfig, func(cmd *Command, cfg interface{}) error {
cfgValues := cfg.(*rootCmdConfig)
t.Log("root command", cfgValues.Hostname)
return nil
}, mathCmd)
err := cmd.ParseAndRun(args)
if err != nil {
t.Errorf("error should be nil but is %v", err)
}
}
func TestCommand_Dependencies(t *testing.T) {
c := NewCommand("testCmd", "Test CLI", nil, nil)
test := &testStruct{testValue: "test"}
c.SetDependency("test", test)
testReturn, err := c.GetDependency("test")
assert.NoError(t, err)
assert.IsType(t, &testStruct{}, testReturn)
assert.Equal(t, "test", testReturn.(*testStruct).testValue)
}