-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtesting.go
More file actions
225 lines (179 loc) · 5.82 KB
/
testing.go
File metadata and controls
225 lines (179 loc) · 5.82 KB
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
package gonertia
import (
"bytes"
"encoding/json"
"io"
"reflect"
"regexp"
"testing"
)
type t interface {
Helper()
Fatal(args ...any)
Fatalf(format string, args ...any)
}
var _ t = (*testing.T)(nil)
// AssertableInertia is an Inertia response struct with assert methods.
type AssertableInertia struct {
t t
*page
Body *bytes.Buffer
}
// AssertComponent verifies that component from Inertia
// response and the passed component are the same.
func (i AssertableInertia) AssertComponent(want string) {
i.t.Helper()
if i.Component != want {
i.t.Fatalf("inertia: Component=%s, want=%s", i.Component, want)
}
}
// AssertVersion verifies that version from Inertia
// response and the passed version are the same.
func (i AssertableInertia) AssertVersion(want string) {
i.t.Helper()
if i.Version != want {
i.t.Fatalf("inertia: Version=%s, want=%s", i.Version, want)
}
}
// AssertURL verifies that url from Inertia
// response and the passed url are the same.
func (i AssertableInertia) AssertURL(want string) {
i.t.Helper()
if i.URL != want {
i.t.Fatalf("inertia: URL=%s, want=%s", i.URL, want)
}
}
// AssertProps verifies that props from Inertia
// response and the passed props are the same.
func (i AssertableInertia) AssertProps(want Props) {
i.t.Helper()
if !reflect.DeepEqual(i.Props, want) {
i.t.Fatalf("inertia: Props=%#v, want=%#v", i.Props, want)
}
}
// AssertFlash verifies that flash from Inertia response and the passed flash are the same.
func (i AssertableInertia) AssertFlash(want Flash) {
i.t.Helper()
if !reflect.DeepEqual(i.Flash, want) {
i.t.Fatalf("inertia: Flash=%#v, want=%#v", i.Flash, want)
}
}
// AssertEncryptHistory verifies that encrypt history
// value from Inertia response and the passed value are the same.
func (i AssertableInertia) AssertEncryptHistory(want bool) {
i.t.Helper()
if i.EncryptHistory != want {
i.t.Fatalf("inertia: EncryptHistory=%t, want=%t", i.EncryptHistory, want)
}
}
// AssertClearHistory verifies that clear history
// value from Inertia response and the passed value are the same.
func (i AssertableInertia) AssertClearHistory(want bool) {
i.t.Helper()
if i.ClearHistory != want {
i.t.Fatalf("inertia: ClearHistory=%t, want=%t", i.ClearHistory, want)
}
}
// AssertDeferredProps verifies that deferred props from Inertia
// response and the passed deferred props are the same.
func (i AssertableInertia) AssertDeferredProps(want map[string][]string) {
i.t.Helper()
if !reflect.DeepEqual(i.DeferredProps, want) {
i.t.Fatalf("inertia: DeferredProps=%#v, want=%#v", i.DeferredProps, want)
}
}
// AssertMergeProps verifies that merge props from Inertia
// response and the passed merge props are the same.
func (i AssertableInertia) AssertMergeProps(want []string) {
i.t.Helper()
if !reflect.DeepEqual(i.MergeProps, want) {
i.t.Fatalf("inertia: MergeProps=%#v, want=%#v", i.MergeProps, want)
}
}
// AssertPrependProps verifies that prepend props from Inertia
// response and the passed prepend props are the same.
func (i AssertableInertia) AssertPrependProps(want []string) {
i.t.Helper()
if !reflect.DeepEqual(i.PrependProps, want) {
i.t.Fatalf("inertia: PrependProps=%#v, want=%#v", i.PrependProps, want)
}
}
// AssertDeepMergeProps verifies that deep merge props from Inertia
// response and the passed deep merge props are the same.
func (i AssertableInertia) AssertDeepMergeProps(want []string) {
i.t.Helper()
if !reflect.DeepEqual(i.DeepMergeProps, want) {
i.t.Fatalf("inertia: DeepMergeProps=%#v, want=%#v", i.DeepMergeProps, want)
}
}
// AssertMatchPropsOn verifies that match props on from Inertia
// response and the passed match props on are the same.
func (i AssertableInertia) AssertMatchPropsOn(want []string) {
i.t.Helper()
if !reflect.DeepEqual(i.MatchPropsOn, want) {
i.t.Fatalf("inertia: MatchPropsOn=%#v, want=%#v", i.MatchPropsOn, want)
}
}
// AssertScrollProps verifies that scroll props from Inertia
// response and the passed scroll props are the same.
func (i AssertableInertia) AssertScrollProps(want map[string]map[string]any) {
i.t.Helper()
// Convert scrollPropMetadata to map[string]any for comparison
got := make(map[string]map[string]any)
for key, val := range i.ScrollProps {
got[key] = map[string]any{
"pageName": val.PageName,
"previousPage": val.PreviousPage,
"nextPage": val.NextPage,
"currentPage": val.CurrentPage,
"reset": val.Reset,
}
}
if !reflect.DeepEqual(got, want) {
i.t.Fatalf("inertia: ScrollProps=%#v, want=%#v", got, want)
}
}
var dataPageScriptRe = regexp.MustCompile(`(?s)<script[^>]* data-page="[^"]*"[^>]*>(.*?)</script>`)
// AssertFromReader creates AssertableInertia from the io.Reader body.
func AssertFromReader(t t, body io.Reader) AssertableInertia {
t.Helper()
bs, err := io.ReadAll(body)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
return AssertFromBytes(t, bs)
}
// AssertFromString creates AssertableInertia from the string body.
func AssertFromString(t t, body string) AssertableInertia {
t.Helper()
return AssertFromBytes(t, []byte(body))
}
// AssertFromBytes creates AssertableInertia from the bytes body.
func AssertFromBytes(t t, body []byte) AssertableInertia {
t.Helper()
assertable := AssertableInertia{t: t}
buf := bytes.NewBuffer(body)
// Might be body is a json? Let's try to unmarshal first.
if err := json.Unmarshal(buf.Bytes(), &assertable.page); err == nil {
assertable.Body = buf
return assertable
}
matched := dataPageScriptRe.FindAllStringSubmatch(buf.String(), -1)
for _, m := range matched {
if len(m) <= 1 {
invalidInertiaResponse(t)
}
pageJSON := []byte(m[1])
if err := json.Unmarshal(pageJSON, &assertable.page); err == nil {
break
}
}
if assertable.page == nil {
invalidInertiaResponse(t)
}
assertable.Body = buf
return assertable
}
func invalidInertiaResponse(t t) {
t.Fatal("invalid inertia response")
}