-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs_test.go
120 lines (101 loc) · 4.21 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
package command
import (
"bytes"
"context"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
type TestCommandArgsDef struct {
ArgsDef
Int0 int `arg:"int0"`
Str1 string `arg:"str1"`
Bool2 bool `arg:"bool2"`
}
var passedArgsCollector *TestCommandArgsDef
func CommandFunc(int0 int, str1 string, bool2 bool) error {
// fmt.Println(int0, str1, bool2)
passedArgsCollector = &TestCommandArgsDef{Int0: int0, Str1: str1, Bool2: bool2}
return nil
}
func CommandFuncErrResult(int0 int, str1 string, bool2 bool) error {
passedArgsCollector = &TestCommandArgsDef{Int0: int0, Str1: str1, Bool2: bool2}
return assert.AnError
}
// func Test_ArgsDef(t *testing.T) {
// var commandArgsDef TestCommandArgsDef
// stringCommandFunc, err := commandArgsDef.StringArgsFunc(CommandFunc, reflect.TypeOf(commandArgsDef), nil)
// assert.NoError(t, err, "Args.StringArgsFunc")
// passedArgsCollector = nil
// err = stringCommandFunc("123", "Hello World!", "true")
// assert.NoError(t, err, "command should return nil")
// assert.Equal(t, 123, passedArgsCollector.Int0, "int0")
// assert.Equal(t, "Hello World!", passedArgsCollector.Str1, "str1")
// assert.Equal(t, true, passedArgsCollector.Bool2, "bool2")
// stringCommandFunc, err = commandArgsDef.StringArgsFunc(CommandFunc, reflect.TypeOf(commandArgsDef), nil)
// assert.NoError(t, err, "Args.StringArgsFunc")
// passedArgsCollector = nil
// err = stringCommandFunc("123")
// assert.NoError(t, err, "command should return nil")
// assert.Equal(t, 123, passedArgsCollector.Int0, "int0")
// assert.Equal(t, "", passedArgsCollector.Str1, "str1")
// assert.Equal(t, false, passedArgsCollector.Bool2, "bool2")
// stringCommandFunc, err = commandArgsDef.StringArgsFunc(CommandFuncErrResult, reflect.TypeOf(commandArgsDef), nil)
// assert.NoError(t, err, "Args.StringArgsFunc")
// passedArgsCollector = nil
// err = stringCommandFunc("123", "Hello World!", "true")
// assert.Error(t, err, "command should return an error")
// }
func Test_GetStringArgsFunc(t *testing.T) {
var commandArgsDef TestCommandArgsDef
stringCommandFunc, err := GetStringArgsFunc(CommandFunc, &commandArgsDef)
assert.NoError(t, err, "GetStringArgsFunc")
passedArgsCollector = nil
err = stringCommandFunc(context.Background(), "123", "Hello World!", "true")
assert.NoError(t, err, "command should return nil")
assert.Equal(t, 123, passedArgsCollector.Int0, "int0")
assert.Equal(t, "Hello World!", passedArgsCollector.Str1, "str1")
assert.Equal(t, true, passedArgsCollector.Bool2, "bool2")
}
func Test_GetStringMapArgsFunc(t *testing.T) {
var commandArgsDef TestCommandArgsDef
stringCommandFunc, err := GetStringMapArgsFunc(CommandFunc, &commandArgsDef)
assert.NoError(t, err, "GetStringMapArgsFunc")
argsMap := map[string]string{
"int0": "123",
"str1": "Hello World!",
"bool2": "true",
}
passedArgsCollector = nil
err = stringCommandFunc(context.Background(), argsMap)
assert.NoError(t, err, "command should return nil")
assert.Equal(t, 123, passedArgsCollector.Int0, "int0")
assert.Equal(t, "Hello World!", passedArgsCollector.Str1, "str1")
assert.Equal(t, true, passedArgsCollector.Bool2, "bool2")
}
type ResultStruct struct {
ResultCode int
ResultMessage string
}
var (
defaultResultStruct = ResultStruct{404, "not found"}
defaultResultStructJSON, _ = json.MarshalIndent(&defaultResultStruct, "", " ")
)
func CommandFuncStructResult(int0 int, str1 string, bool2 bool) (*ResultStruct, error) {
passedArgsCollector = &TestCommandArgsDef{Int0: int0, Str1: str1, Bool2: bool2}
return &defaultResultStruct, nil
}
func Test_WithResultHandler(t *testing.T) {
var commandArgsDef TestCommandArgsDef
var resultBuf bytes.Buffer
stringCommandFunc, err := GetStringArgsFunc(CommandFuncStructResult, &commandArgsDef, PrintTo(&resultBuf))
assert.NoError(t, err, "GetStringArgsFunc")
passedArgsCollector = nil
err = stringCommandFunc(context.Background(), "123", "Hello World!", "true")
// Check passed args
assert.Equal(t, 123, passedArgsCollector.Int0, "int0")
assert.Equal(t, "Hello World!", passedArgsCollector.Str1, "str1")
assert.Equal(t, true, passedArgsCollector.Bool2, "bool2")
// Check result struct
assert.Equal(t, resultBuf.String(), string(defaultResultStructJSON), "equal result JSON")
}