-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils.go
60 lines (47 loc) · 1.21 KB
/
testutils.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
package oapi
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hackathon-22-winter-01/hackathon-22-winter-01_backend/pkg/assert"
"github.com/shiguredo/websocket"
"github.com/stretchr/testify/require"
)
// WsResponse for tests
type WsResponseWrapper[T any] struct {
T *testing.T
Res *WsResponse
Body T
}
func (w *WsResponseWrapper[T]) Equal(expectedType WsResponseType, expectedBody T, opts ...cmp.Option) {
w.T.Helper()
assert.NoError(w.T, json.Unmarshal(w.Res.Body.union, &w.Body))
assert.Equal(w.T, expectedType, w.Res.Type)
assert.Equal(w.T, expectedBody, w.Body, opts...)
}
// WsRequest for tests
type WsRequestWrapper struct {
T *testing.T
Req *WsRequest
}
func NewWsRequestForTest(t *testing.T, typ WsRequestType, body any) *WsRequestWrapper {
t.Helper()
b, err := json.Marshal(body)
assert.NoError(t, err)
return &WsRequestWrapper{
T: t,
Req: &WsRequest{
Type: typ,
Body: WsRequest_Body{union: b},
},
}
}
func WriteWsRequest(t *testing.T, c *websocket.Conn, typ WsRequestType, body any) {
t.Helper()
b, err := json.Marshal(body)
assert.NoError(t, err)
require.NoError(t, c.WriteJSON(WsRequest{
Type: typ,
Body: WsRequest_Body{union: b},
}))
}