forked from crgimenes/grupo-estudos-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing_test.go
337 lines (308 loc) · 5.91 KB
/
testing_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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package testing
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestMain(m *testing.M) {
log.Println("Start tests")
code := m.Run()
log.Println("Stop tests")
os.Exit(code)
}
func Test_sum(t *testing.T) {
type args struct {
a int
b int
}
type expected struct {
ret int
}
tests := []struct {
name string
args args
want expected
setup func()
tearDown func()
}{
{
name: "test 1+1",
args: args{
a: 1,
b: 1,
},
want: expected{
ret: 2,
},
setup: func() {
fmt.Println("setup do teste 1+1")
},
tearDown: func() {
fmt.Println("tearDown do teste 1+1")
},
},
{
name: "test 2+2",
args: args{
a: 2,
b: 2,
},
want: expected{
ret: 4,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setup != nil {
tt.setup()
}
ret := sum(tt.args.a, tt.args.b)
if ret != tt.want.ret {
t.Errorf("sum() = %v, want %v", ret, tt.want.ret)
}
if tt.tearDown != nil {
tt.tearDown()
}
})
}
}
func Test_divideInteiros(t *testing.T) {
type args struct {
dividendo int
divisor int
}
type expected struct {
quociente int
resto int
err error
}
tests := []struct {
name string
args args
want expected
}{
// Casos de teste para a função
{
name: "divide por zero",
want: expected{
err: errDivisaoInvalida,
},
args: args{
dividendo: 10,
divisor: 0,
},
},
{
name: "divisão sem resto",
want: expected{
err: nil,
resto: 0,
quociente: 5,
},
args: args{
dividendo: 10,
divisor: 2,
},
},
{
name: "divisão com resto",
want: expected{
err: nil,
resto: 1,
quociente: 3,
},
args: args{
dividendo: 7,
divisor: 2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotQuociente, gotResto, err := divideInteiros(tt.args.dividendo, tt.args.divisor)
if err != tt.want.err {
t.Errorf("divideInteiros() error = %v, wantErr %v", err, tt.want.err)
return
}
if gotQuociente != tt.want.quociente {
t.Errorf("divideInteiros() gotQuociente = %v, want %v", gotQuociente, tt.want.quociente)
}
if gotResto != tt.want.resto {
t.Errorf("divideInteiros() gotResto = %v, want %v", gotResto, tt.want.resto)
}
})
}
}
func Test_leitor(t *testing.T) {
expected := "hello world"
r := bytes.NewReader([]byte(expected))
got := leitor(r)
if got != expected {
t.Errorf("leitor() = %v, want %v", got, expected)
}
}
func Test_leEFecha(t *testing.T) {
expected := "hello world"
r := ioutil.NopCloser(bytes.NewReader([]byte(expected)))
got := leEFecha(r)
if got != expected {
t.Errorf("leEFecha() = %v, want %v", got, expected)
}
}
func Test_responde(t *testing.T) {
w := httptest.NewRecorder()
expected := "{\"message\":\"algo muito importante\"}\n"
responde(w)
b := w.Body.Bytes()
if string(b) != expected {
t.Errorf("responde(): expected %q, but got %q", expected, string(b))
}
if w.Header().Get("Content-Type") != "application/json" {
t.Errorf("responde(): expected Content-Type application/json, but got %q", w.Header().Get("Content-Type"))
}
}
func Test_clienteHttp(t *testing.T) {
serverOk := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
}))
defer serverOk.Close()
serverErr := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "error", http.StatusInternalServerError)
fmt.Fprintln(w, "error")
}))
defer serverErr.Close()
type args struct {
token string
method string
url string
body io.Reader
}
tests := []struct {
name string
args args
wantStatus int
wantErr bool
}{
{
name: "StatusOK",
wantErr: false,
wantStatus: http.StatusOK,
args: args{
token: "test",
method: http.MethodGet,
url: serverOk.URL,
},
},
{
name: "StatusInternalServerError",
wantErr: false,
wantStatus: http.StatusInternalServerError,
args: args{
token: "test",
method: http.MethodGet,
url: serverErr.URL,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResp, err := clienteHttp(tt.args.token, tt.args.method, tt.args.url, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("clienteHttp() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotResp.StatusCode != tt.wantStatus {
t.Errorf("clienteHttp() = %v, want %v", gotResp.StatusCode, tt.wantStatus)
}
})
}
}
/*
Testando a função closer() usando interfaces
*/
type closerSuccess struct {
}
func (c closerSuccess) Close() (err error) {
return
}
type closerError struct {
}
func (c closerError) Close() (err error) {
err = errors.New("closer error")
return
}
func Test_closer(t *testing.T) {
getStdout := func(obj io.Closer) (out []byte, err error) {
rescueStdout := os.Stdout
defer func() { os.Stdout = rescueStdout }()
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
os.Stdout = w
closer(obj)
err = w.Close()
if err != nil {
return
}
out, err = ioutil.ReadAll(r)
return
}
cs := closerSuccess{}
ce := closerError{}
type args struct {
body io.Closer
}
type expected struct {
err bool
}
tests := []struct {
name string
args args
want expected
}{
{
name: "success",
args: args{
body: cs,
},
want: expected{
err: false,
},
},
{
name: "error",
args: args{
body: ce,
},
want: expected{
err: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := getStdout(tt.args.body)
if err != nil {
t.Error(err)
return
}
if (len(out) > 0) != tt.want.err {
fmt.Printf("out: %q\n", string(out))
t.Errorf("closer() unexpected log %q", string(out))
}
})
}
}