/
assert.go
72 lines (65 loc) · 1.25 KB
/
assert.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
// Package test has functions to help commons asserts
package test
import (
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"testing"
)
func AssertNotNil(t *testing.T, value interface{}) {
t.Helper()
if value == nil {
t.Error("value is nil")
}
}
func AssertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Error("expected nil error but got this error = %w", err)
}
}
func AssertEqual(t *testing.T, obj1 interface{}, obj2 interface{}) {
t.Helper()
if !reflect.DeepEqual(obj1, obj2) {
t.Errorf("obj {%v} not equal to obj {%v}", obj1, obj2)
}
}
func AssertBodyContains(
t *testing.T,
bodyReader io.Reader,
expectedMsg string,
) {
t.Helper()
body, err := io.ReadAll(bodyReader)
AssertNoError(t, err)
AssertContains(
t,
string(body),
expectedMsg,
)
}
func AssertBodyContainsStruct(
t *testing.T,
bodyReader io.Reader,
expectedMsg interface{},
) {
t.Helper()
body, err := io.ReadAll(bodyReader)
AssertNoError(t, err)
b, err := json.Marshal(expectedMsg)
AssertNoError(t, err)
AssertContains(
t,
string(body),
string(b),
)
}
func AssertContains(t *testing.T, s, substr string) {
t.Helper()
if !strings.Contains(s, substr) {
detail := fmt.Sprintf("{%s} doesn't contains {%s}", s, substr)
t.Error(detail)
}
}