Skip to content

Commit 83b7f56

Browse files
simplify test code
1 parent d105c22 commit 83b7f56

4 files changed

Lines changed: 15 additions & 23 deletions

File tree

checks/http_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"net/http"
77
"net/http/httptest"
8+
"slices"
89
"strings"
910
"testing"
1011

@@ -47,14 +48,9 @@ func TestInterpolateVariables(t *testing.T) {
4748
func TestInterpolationNames(t *testing.T) {
4849
got := InterpolationNames("${baseURL}/users/${id}/${id}")
4950
want := []string{"baseURL", "id", "id"}
50-
if len(got) != len(want) {
51+
if !slices.Equal(got, want) {
5152
t.Fatalf("InterpolationNames() = %#v, want %#v", got, want)
5253
}
53-
for i := range want {
54-
if got[i] != want[i] {
55-
t.Fatalf("InterpolationNames() = %#v, want %#v", got, want)
56-
}
57-
}
5854
}
5955

6056
func TestRunHTTPRequestInterpolatesRequestAndCapturesResponseVariables(t *testing.T) {

checks/local_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
api "github.com/bootdotdev/bootdev/client"
7+
"github.com/goccy/go-json"
78
)
89

910
func TestLocalSubmissionEventPassesCLIAndHTTPResults(t *testing.T) {
@@ -256,7 +257,7 @@ func TestValuesEqualPreservesTypes(t *testing.T) {
256257
{name: "string and bool", got: "true", want: true, ok: false},
257258
{name: "same bools", got: true, want: true, ok: true},
258259
{name: "numeric int and float", got: 1, want: 1.0, ok: true},
259-
{name: "numeric json number and int", got: testJSONNumber("1"), want: 1, ok: true},
260+
{name: "numeric json number and int", got: json.Number("1"), want: 1, ok: true},
260261
{name: "nil and string", got: nil, want: "<nil>", ok: false},
261262
}
262263

@@ -269,12 +270,6 @@ func TestValuesEqualPreservesTypes(t *testing.T) {
269270
}
270271
}
271272

272-
type testJSONNumber string
273-
274-
func (n testJSONNumber) String() string {
275-
return string(n)
276-
}
277-
278273
func intPtr(v int) *int {
279274
return &v
280275
}

checks/runner_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ func TestApplySubmissionResultsMarksAllStepsAndTestsPassedWhenNoFailure(t *testi
146146
messages.ResolveTestMsg{StepIndex: 1, TestIndex: 0, Passed: boolPtr(true)},
147147
}
148148

149-
assertMessages(t, got, want)
149+
if !reflect.DeepEqual(got, want) {
150+
t.Fatalf("messages = %#v, want %#v", got, want)
151+
}
150152
}
151153

152154
func TestApplySubmissionResultsStopsAfterFailedCLITest(t *testing.T) {
@@ -166,7 +168,9 @@ func TestApplySubmissionResultsStopsAfterFailedCLITest(t *testing.T) {
166168
messages.ResolveTestMsg{StepIndex: 1, TestIndex: 1, Passed: boolPtr(false)},
167169
}
168170

169-
assertMessages(t, got, want)
171+
if !reflect.DeepEqual(got, want) {
172+
t.Fatalf("messages = %#v, want %#v", got, want)
173+
}
170174
}
171175

172176
func applySubmissionResultsMessages(cliData api.CLIData, failure *api.StructuredErrCLI) []tea.Msg {
@@ -177,14 +181,6 @@ func applySubmissionResultsMessages(cliData api.CLIData, failure *api.Structured
177181
return msgs
178182
}
179183

180-
func assertMessages(t *testing.T, got []tea.Msg, want []tea.Msg) {
181-
t.Helper()
182-
183-
if !reflect.DeepEqual(got, want) {
184-
t.Fatalf("messages = %#v, want %#v", got, want)
185-
}
186-
}
187-
188184
func boolPtr(v bool) *bool {
189185
return &v
190186
}

cmd/login_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func TestLoginHTTPHandlerRejectsMissingOrigin(t *testing.T) {
5959
if response.Code != http.StatusForbidden {
6060
t.Fatalf("status = %d, want %d", response.Code, http.StatusForbidden)
6161
}
62+
select {
63+
case code := <-inputChan:
64+
t.Fatalf("unexpected login code accepted: %q", code)
65+
default:
66+
}
6267
}
6368

6469
func TestLoginHTTPHandlerLimitsCodeSize(t *testing.T) {

0 commit comments

Comments
 (0)