forked from bakape/shamichan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
67 lines (59 loc) · 1.69 KB
/
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
// Package test contains utility functions used throughout the project in tests
package test
import (
"bytes"
"encoding/base64"
"io/ioutil"
"math/rand"
"path/filepath"
"reflect"
"testing"
)
// LogUnexpected fails the test and prints the values in an
// `expected: X got: Y` format
func LogUnexpected(t *testing.T, expected, got interface{}) {
t.Fatalf("\nexpected: %#v\ngot: %#v", expected, got)
}
// AssertDeepEquals asserts two values are deeply equal or fails the test, if
// not
func AssertDeepEquals(t *testing.T, res, std interface{}) {
if !reflect.DeepEqual(res, std) {
LogUnexpected(t, std, res)
}
}
// UnexpectedError fails the test with an unexpected error message
func UnexpectedError(t *testing.T, err error) {
t.Fatalf("unexpected error: %s", err)
}
// AssertFileEquals reads a file from disk and asserts it equals the standard
// buffer
func AssertFileEquals(t *testing.T, path string, std []byte) {
buf, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
AssertBufferEquals(t, buf, std)
}
// AssertBufferEquals asserts two buffers are equal
func AssertBufferEquals(t *testing.T, buf, std []byte) {
if !bytes.Equal(buf, std) {
t.Fatalf("files not equal: `%s` : `%s`", string(std), string(buf))
}
}
// GenString produces a random base64 string of passed length
func GenString(len int) string {
buf := make([]byte, len)
for i := 0; i < len; i++ {
buf[i] = byte(rand.Intn(256))
}
return base64.RawURLEncoding.EncodeToString(buf)[:len]
}
// ReadSample reads a sample file of passed file name
func ReadSample(t *testing.T, name string) []byte {
path := filepath.Join("testdata", name)
data, err := ioutil.ReadFile(path)
if err != nil {
t.Error(err)
}
return data
}