Skip to content

Commit

Permalink
vtest: Add subtests within each helper assertion
Browse files Browse the repository at this point in the history
This way, when you call multiple Assert* methods on a single response, it prints test output data for each assertion.

Example:
=== RUN   TestNotFound
=== RUN   TestNotFound/body
=== RUN   TestNotFound/status
--- PASS: TestNotFound (0.00s)
    --- PASS: TestNotFound/body (0.00s)
    --- PASS: TestNotFound/status (0.00s)
  • Loading branch information
danielledeleo committed Aug 12, 2021
1 parent 7cb2055 commit 47bccbc
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions vtest/response.go
Expand Up @@ -17,9 +17,11 @@ type Response struct {

// AssertStatus asserts the HTTP status code of this Response
func (r *Response) AssertStatus(status int) *Response {
if r.Status != status {
r.t.Errorf("got status %d, want %d", r.Status, status)
}
r.t.Run("status", func(t *testing.T) {
if r.Status != status {
t.Errorf("got status %d, want %d", r.Status, status)
}
})

return r
}
Expand Down Expand Up @@ -55,15 +57,19 @@ func (r *Response) AssertHeaders(headers http.Header) *Response {

// AssertBody asserts the response body is a byte-for-byte match
func (r *Response) AssertBody(body []byte) *Response {
if len(body) != len(r.Body) {
r.t.Errorf("body length mismatch: got %d, want %d", len(r.Body), len(body))
}
r.t.Run("body", func(t *testing.T) {
if len(body) != len(r.Body) {
t.Errorf("body length mismatch: got %d bytes, want %d bytes", len(r.Body), len(body))
t.FailNow()
}

for i, v := range body {
if v != r.Body[i] {
r.t.Errorf("byte mismatch at byte %d: got %s, want %s", i, string(r.Body[i]), string(v))
for i, v := range body {
if v != r.Body[i] {
t.Errorf("byte mismatch at byte %d: got %s, want %s", i, string(r.Body[i]), string(v))
t.FailNow()
}
}
}
})

return r
}
Expand All @@ -72,14 +78,6 @@ const runeWindow = 25

// AssertBodyString asserts the response body is a rune-for-rune match
func (r *Response) AssertBodyString(body string) *Response {
resRunes := []rune(string(r.Body))
bodyRunes := []rune(body)

min := len(bodyRunes)
if len(resRunes) < min {
min = len(resRunes)
}

// pretty printing helpers for where the mismatch occurred
trimAround := func(i int, str []rune) string {
start := i - runeWindow
Expand Down Expand Up @@ -107,13 +105,27 @@ func (r *Response) AssertBodyString(body string) *Response {
return fmt.Sprintf("\nwant: %s\n got: %s\n %*s", f, s, offset+1, "^")
}

for i := 0; i < min; i++ {
if bodyRunes[i] != resRunes[i] {
r.t.Errorf(`rune mismatch at index %d: %s`, i, context(i, bodyRunes, resRunes))
r.t.Run("body", func(t *testing.T) {
resRunes := []rune(string(r.Body))
bodyRunes := []rune(body)

return r
if len(resRunes) != len(bodyRunes) {
t.Errorf("body length mismatch: got %d runes, want %d runes", len(resRunes), len(bodyRunes))
t.FailNow()
}
}

min := len(bodyRunes)
if len(resRunes) < min {
min = len(resRunes)
}

for i := 0; i < min; i++ {
if bodyRunes[i] != resRunes[i] {
t.Errorf(`rune mismatch at index %d: %s`, i, context(i, bodyRunes, resRunes))
t.FailNow()
}
}
})

return r
}

0 comments on commit 47bccbc

Please sign in to comment.