-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing_utils.go
68 lines (58 loc) · 1.67 KB
/
testing_utils.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
package main
import (
"io"
"io/ioutil"
"log"
"net/http"
"testing"
)
// a few mini utils for easier testing
// MockPoster implements HTTPPoster by saving the request data and returning
// a static response
type MockPoster struct {
Result http.Response
Err error
RequestURL string
RequestBody []byte
RequestBodyType string
}
// Post returns the Result and Err members while reading and saving the request
// data.
func (mock *MockPoster) Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
mock.RequestURL = url
mock.RequestBodyType = bodyType
var readErr error
mock.RequestBody, readErr = ioutil.ReadAll(body)
if readErr != nil {
log.Fatalf("failed to ReadAll in MockPoster: %s", readErr.Error())
}
return &mock.Result, mock.Err
}
// AssertTrue fails a test if its ok argument is false, and logs
// the provided message
func AssertTrue(t *testing.T, msg string, ok bool) {
if !ok {
t.Fatalf(msg)
}
}
// AssertFalse fails a test if its ok argument is true , and logs
// the provided message
func AssertFalse(t *testing.T, msg string, ok bool) {
if ok {
t.Fatalf(msg)
}
}
// AssertNoError will fail a test with the provided message and
// the error message if err is not nil
func AssertNoError(t *testing.T, msg string, err error) {
if err != nil {
t.Fatalf("%s\n error: %v", msg, err.Error())
}
}
// Give a nice error message in the case of non-equality. We still make
// the caller do the comparison for us because that's just a bit easier
func AssertEqual(t *testing.T, equals bool, msg string, expected interface{}, actual interface{}) {
if !equals {
t.Fatalf("%s expected: %v got: %v", msg, expected, actual)
}
}