-
Notifications
You must be signed in to change notification settings - Fork 6
/
fire_test.go
205 lines (178 loc) · 5.33 KB
/
fire_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018 The fuego Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fuego
import (
"os"
"reflect"
"strings"
"testing"
)
// Func1 is a test function for Function.
func Func1(a, b int) (int, int) {
return a + b, a - b
}
func Func2(a, b float64) (float64, float64) {
return a + b, a - b
}
// Func3 is a test function for Function.
func Func3(a, b float32) (float32, float32) {
return a + b, a - b
}
type SampleStruct struct {
Name string
}
// Test Add Method
func (s SampleStruct) Add(a, b int) int {
return a + b
}
// Test Minus Method.
func (s SampleStruct) Minus(a, b int) int {
return a - b
}
func (s SampleStruct) String(str string) string {
return strings.ToUpper(str)
}
func TestFunc1(t *testing.T) {
config := Config{PrintReturnValuesOff: true}
os.Args = []string{"TestFunc1", "3", "5"}
ret, err := Fire(Func1, config)
expectedNumOut := reflect.ValueOf(Func1).Type().NumOut()
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
expectedRet1, expectedRet2 := Func1(3, 5)
gotRet1 := int(ret[0].Int())
gotRet2 := int(ret[1].Int())
if expectedRet1 != gotRet1 || expectedRet2 != gotRet2 {
t.Errorf("(%v, %v) is expected but got (%v, %v)", expectedRet1,
expectedRet2, gotRet1, gotRet2)
}
if err != nil {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestWrongFunc(t *testing.T) {
os.Args = []string{"TestFunc1", "3"}
ret, err := Fire(Func1)
expectedNumOut := 0
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
if err == nil || err.Error() != "Invalid command" {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestFunc2(t *testing.T) {
os.Args = []string{"TestFunc2", "3.5", "5.4"}
ret, err := Fire(Func2)
expectedNumOut := reflect.ValueOf(Func2).Type().NumOut()
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
expectedRet1, expectedRet2 := Func2(3.5, 5.4)
gotRet1 := ret[0].Float()
gotRet2 := ret[1].Float()
if expectedRet1 != gotRet1 || expectedRet2 != gotRet2 {
t.Errorf("(%v, %v) is expected but got (%v, %v)", expectedRet1,
expectedRet2, gotRet1, gotRet2)
}
if err != nil {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestFunc3(t *testing.T) {
os.Args = []string{"TestFunc3", "3.5", "5.4"}
ret, err := Fire(Func3)
expectedNumOut := reflect.ValueOf(Func3).Type().NumOut()
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
expectedRet1, expectedRet2 := Func3(3.5, 5.4)
gotRet1 := float32(ret[0].Float())
gotRet2 := float32(ret[1].Float())
if expectedRet1 != gotRet1 || expectedRet2 != gotRet2 {
t.Errorf("(%v, %v) is expected but got (%v, %v)", expectedRet1,
expectedRet2, gotRet1, gotRet2)
}
if err != nil {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestSampleStruct1(t *testing.T) {
var s SampleStruct
os.Args = []string{"TestSampleStruct", "Add", "3", "5"}
ret, err := Fire(s)
expectedNumOut := reflect.ValueOf(s.Add).Type().NumOut()
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
expectedRet := s.Add(3, 5)
gotRet := int(ret[0].Int())
if expectedRet != gotRet {
t.Errorf("(%v) is expected but got (%v)", expectedRet, gotRet)
}
if err != nil {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestSampleWrongStruct1(t *testing.T) {
var s SampleStruct
os.Args = []string{"TestSampleStruct", "Add", "3"}
ret, err := Fire(s)
expectedNumOut := 0
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
if err == nil || err.Error() != "Invalid command" {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestSampleStruct2(t *testing.T) {
var s SampleStruct
os.Args = []string{"TestSampleStruct", "Minus", "3", "5"}
ret, err := Fire(s)
expectedNumOut := reflect.ValueOf(s.Add).Type().NumOut()
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
expectedRet := s.Minus(3, 5)
gotRet := int(ret[0].Int())
if expectedRet != gotRet {
t.Errorf("(%v) is expected but got (%v)", expectedRet, gotRet)
}
if err != nil {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestSampleStruct3(t *testing.T) {
var s SampleStruct
os.Args = []string{"TestSampleStruct", "String", "hello, world"}
ret, err := Fire(s)
expectedNumOut := reflect.ValueOf(s.String).Type().NumOut()
if len(ret) != expectedNumOut {
t.Errorf("%d return value expected, got %d", expectedNumOut, len(ret))
}
expectedRet := s.String("hello, world")
gotRet := ret[0].String()
if expectedRet != gotRet {
t.Errorf("(%v) is expected but got (%v)", expectedRet, gotRet)
}
if err != nil {
t.Errorf("Error is not expected but got %v", err)
}
}
func TestSampleStructWrongCommand(t *testing.T) {
var s SampleStruct
os.Args = []string{"TestSampleStruct", "Wrong", "hello, world"}
ret, err := Fire(s)
if len(ret) != 0 {
t.Errorf("%d return value expected, got %d", 0, len(ret))
}
if ret != nil {
t.Errorf("(%v) is expected but got (%v)", nil, ret)
}
if err == nil || err.Error() != "Invalid command" {
t.Errorf("Error is expected but got %v", err)
}
}