Skip to content

Commit 59705d5

Browse files
committed
refactor: make all tests more uniform
1 parent a163b07 commit 59705d5

4 files changed

Lines changed: 49 additions & 127 deletions

File tree

pkg/teapot/additional_methods_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ func TestMustURLPanic(t *testing.T) {
179179
r.MustURL("nonexistent.route")
180180
})
181181

182-
if msg == "" {
183-
t.Error("expected MustURL to panic for nonexistent route")
184-
}
182+
assert.NotEmpty(t, msg, "expected MustURL to panic for nonexistent route")
185183
}
186184

187185
// Test: Query and QueryValue chaining

tests/late_propagation_test.go

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"net/http/httptest"
66
"testing"
77

8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
811
"github.com/mallardduck/teapot-router/internal/testutil"
912
"github.com/mallardduck/teapot-router/pkg/teapot"
1013
)
@@ -30,31 +33,21 @@ func TestLatePropagation(t *testing.T) {
3033
break
3134
}
3235
}
33-
if !foundSub {
34-
t.Errorf("Expected to find propagated route /api/hello in main router")
35-
}
36+
assert.True(t, foundSub, "Expected to find propagated route /api/hello in main router")
3637

3738
// 5. Verify URL generation
3839
url, err := r.URL("sub.hello")
39-
if err != nil {
40-
t.Fatalf("Failed to generate URL for propagated route: %v", err)
41-
}
42-
if url != "/api/hello" {
43-
t.Errorf("Expected URL /api/hello, got %s", url)
44-
}
40+
require.NoError(t, err, "Failed to generate URL for propagated route")
41+
assert.Equal(t, "/api/hello", url)
4542

4643
// 6. Verify functional routing
4744
ts := httptest.NewServer(r)
4845
defer ts.Close()
4946

5047
res, err := http.Get(ts.URL + "/api/hello")
51-
if err != nil {
52-
t.Fatal(err)
53-
}
48+
require.NoError(t, err)
5449
defer res.Body.Close()
55-
if res.StatusCode != http.StatusOK {
56-
t.Errorf("Expected status OK, got %d", res.StatusCode)
57-
}
50+
assert.Equal(t, http.StatusOK, res.StatusCode, "Expected status OK, got %d", res.StatusCode)
5851
}
5952

6053
func TestLatePropagation_Homing(t *testing.T) {
@@ -71,12 +64,8 @@ func TestLatePropagation_Homing(t *testing.T) {
7164

7265
// Verify propagation
7366
url, err := r.URL("sub.world")
74-
if err != nil {
75-
t.Fatalf("Failed to generate URL for late-added route: %v", err)
76-
}
77-
if url != "/v1/world" {
78-
t.Errorf("Expected URL /v1/world, got %s", url)
79-
}
67+
require.NoError(t, err, "Failed to generate URL for late-added route")
68+
assert.Equal(t, "/v1/world", url)
8069
}
8170

8271
func TestLatePropagation_DeepHoming(t *testing.T) {
@@ -90,10 +79,6 @@ func TestLatePropagation_DeepHoming(t *testing.T) {
9079
r3.GET("/users", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})).Name("users.index")
9180

9281
url, err := r1.URL("users.index")
93-
if err != nil {
94-
t.Fatalf("Failed to generate URL for deep homing: %v", err)
95-
}
96-
if url != "/api/v1/users" {
97-
t.Errorf("Expected URL /api/v1/users, got %s", url)
98-
}
82+
require.NoError(t, err, "Failed to generate URL for deep homing")
83+
assert.Equal(t, "/api/v1/users", url)
9984
}

tests/mount_naming_test.go

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package tests
33
import (
44
"testing"
55

6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
69
"github.com/mallardduck/teapot-router/internal/testutil"
710
"github.com/mallardduck/teapot-router/pkg/teapot"
811
)
@@ -45,23 +48,15 @@ func TestMountWithNamePrefix(t *testing.T) {
4548
r.MountNamed("/v1", "api.v1", sub)
4649

4750
_, err = r.URL("api.v1.list")
48-
if err != nil {
49-
t.Logf("Expected api.v1.list but got error: %v", err)
50-
t.Fail()
51-
} else {
52-
t.Log("Successfully found api.v1.list")
53-
}
51+
require.NoError(t, err, "Expected api.v1.list to be found")
52+
t.Log("Successfully found api.v1.list")
5453

5554
// Test Homing with MountNamed
5655
sub.GET("/profile", handler).Name("profile")
5756

5857
_, err = r.URL("api.v1.profile")
59-
if err != nil {
60-
t.Logf("Expected api.v1.profile but got error: %v", err)
61-
t.Fail()
62-
} else {
63-
t.Log("Successfully found api.v1.profile")
64-
}
58+
require.NoError(t, err, "Expected api.v1.profile to be found")
59+
t.Log("Successfully found api.v1.profile")
6560

6661
// Test Mount with .Name() builder
6762
sub2 := teapot.New()
@@ -70,22 +65,14 @@ func TestMountWithNamePrefix(t *testing.T) {
7065
r.Mount("/v2", sub2).Name("api.v2")
7166

7267
_, err = r.URL("api.v2.list")
73-
if err != nil {
74-
t.Logf("Expected api.v2.list but got error: %v", err)
75-
t.Fail()
76-
} else {
77-
t.Log("Successfully found api.v2.list")
78-
}
68+
require.NoError(t, err, "Expected api.v2.list to be found")
69+
t.Log("Successfully found api.v2.list")
7970

8071
// Test Homing with Mount builder
8172
sub2.GET("/detail", handler).Name("detail")
8273
_, err = r.URL("api.v2.detail")
83-
if err != nil {
84-
t.Logf("Expected api.v2.detail but got error: %v", err)
85-
t.Fail()
86-
} else {
87-
t.Log("Successfully found api.v2.detail")
88-
}
74+
require.NoError(t, err, "Expected api.v2.detail to be found")
75+
t.Log("Successfully found api.v2.detail")
8976

9077
// Test Hierarchical Mount builder
9178
r0 := teapot.New()
@@ -98,12 +85,8 @@ func TestMountWithNamePrefix(t *testing.T) {
9885

9986
// Current name should be p0.p1.list
10087
_, err = r0.URL("p0.p1.list")
101-
if err != nil {
102-
t.Logf("Expected p0.p1.list but got error: %v", err)
103-
t.Fail()
104-
} else {
105-
t.Log("Successfully found p0.p1.list")
106-
}
88+
require.NoError(t, err, "Expected p0.p1.list to be found")
89+
t.Log("Successfully found p0.p1.list")
10790

10891
// Now RENAME the mount in r1
10992
// Find the mount builder? Wait, Mount returns a new one each time.
@@ -112,27 +95,16 @@ func TestMountWithNamePrefix(t *testing.T) {
11295

11396
// Check r0 again. Should have p0.p1_new.list
11497
_, err = r0.URL("p0.p1_new.list")
115-
if err != nil {
116-
t.Logf("Expected p0.p1_new.list but got error: %v", err)
117-
t.Fail()
118-
} else {
119-
t.Log("Successfully found p0.p1_new.list")
120-
}
98+
require.NoError(t, err, "Expected p0.p1_new.list to be found")
99+
t.Log("Successfully found p0.p1_new.list")
121100

122101
// What happens to the old name? It should be gone.
123102
_, err = r0.URL("p0.p1.list")
124-
if err == nil {
125-
t.Log("Expected p0.p1.list to be gone, but it's still there")
126-
t.Fail()
127-
}
103+
assert.Error(t, err, "Expected p0.p1.list to be gone, but it's still there")
128104

129105
// Test deep homing with prefix change
130106
r2.GET("/r4", handler).Name("new_route")
131107
_, err = r0.URL("p0.p1_new.new_route")
132-
if err != nil {
133-
t.Logf("Expected p0.p1_new.new_route but got error: %v", err)
134-
t.Fail()
135-
} else {
136-
t.Log("Successfully found p0.p1_new.new_route")
137-
}
108+
require.NoError(t, err, "Expected p0.p1_new.new_route to be found")
109+
t.Log("Successfully found p0.p1_new.new_route")
138110
}

tests/router_improvements_test.go

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910

1011
"github.com/mallardduck/teapot-router/pkg/teapot"
1112
)
@@ -22,22 +23,14 @@ func TestRouterImprovements(t *testing.T) {
2223
defer ts.Close()
2324

2425
res, err := http.Get(ts.URL + "/test")
25-
if err != nil {
26-
t.Fatal(err)
27-
}
26+
require.NoError(t, err)
2827
defer res.Body.Close()
29-
if res.StatusCode != http.StatusOK {
30-
t.Errorf("expected OK, got %v", res.Status)
31-
}
28+
assert.Equal(t, http.StatusOK, res.StatusCode, "expected OK, got %v", res.Status)
3229

3330
// Verify name registration
3431
url, err := r.URL("test")
35-
if err != nil {
36-
t.Fatal(err)
37-
}
38-
if url != "/test" {
39-
t.Errorf("expected /test, got %q", url)
40-
}
32+
require.NoError(t, err)
33+
assert.Equal(t, "/test", url)
4134
})
4235

4336
t.Run("RegisterExternal (phantom routes)", func(t *testing.T) {
@@ -52,28 +45,20 @@ func TestRouterImprovements(t *testing.T) {
5245
break
5346
}
5447
}
55-
if !found {
56-
t.Error("RegisterExternal route not found in Routes()")
57-
}
48+
assert.True(t, found, "RegisterExternal route not found in Routes()")
5849

5950
// Verify URL generation works for phantom routes
6051
url, err := r.URL("ext")
61-
if err != nil {
62-
t.Fatal(err)
63-
}
64-
if url != "/external" {
65-
t.Errorf("expected /external, got %q", url)
66-
}
52+
require.NoError(t, err)
53+
assert.Equal(t, "/external", url)
6754

6855
// Verify it doesn't actually dispatch
6956
ts := httptest.NewServer(r)
7057
defer ts.Close()
7158
res, err := http.Get(ts.URL + "/external")
7259
assert.NoError(t, err)
7360
defer res.Body.Close()
74-
if res.StatusCode != http.StatusNotFound {
75-
t.Errorf("expected 404 for phantom route, got %v", res.Status)
76-
}
61+
assert.Equal(t, http.StatusNotFound, res.StatusCode, "expected 404 for phantom route, got %v", res.Status)
7762
})
7863

7964
t.Run("Mount propagation", func(t *testing.T) {
@@ -91,18 +76,12 @@ func TestRouterImprovements(t *testing.T) {
9176
break
9277
}
9378
}
94-
if !found {
95-
t.Error("Mounted route not propagated correctly")
96-
}
79+
assert.True(t, found, "Mounted route not propagated correctly")
9780

9881
// Verify URL generation in parent
9982
url, err := parent.URL("hello")
100-
if err != nil {
101-
t.Fatal(err)
102-
}
103-
if url != "/api/hello" {
104-
t.Errorf("expected /api/hello, got %q", url)
105-
}
83+
require.NoError(t, err)
84+
assert.Equal(t, "/api/hello", url)
10685
})
10786

10887
t.Run("SubRouter", func(t *testing.T) {
@@ -118,18 +97,12 @@ func TestRouterImprovements(t *testing.T) {
11897
break
11998
}
12099
}
121-
if !found {
122-
t.Error("SubRouter route not propagated correctly")
123-
}
100+
assert.True(t, found, "SubRouter route not propagated correctly")
124101

125102
// Verify URL generation in parent
126103
url, err := r.URL("dash")
127-
if err != nil {
128-
t.Fatal(err)
129-
}
130-
if url != "/admin/dashboard" {
131-
t.Errorf("expected /admin/dashboard, got %q", url)
132-
}
104+
require.NoError(t, err)
105+
assert.Equal(t, "/admin/dashboard", url)
133106
})
134107

135108
t.Run("SubRouter with wildcards", func(t *testing.T) {
@@ -138,12 +111,8 @@ func TestRouterImprovements(t *testing.T) {
138111
sub.GET("/{bucket}/{key:.*}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})).Name("get_file")
139112

140113
url, err := r.URL("get_file", "bucket", "mybucket", "key", "path/to/file.txt")
141-
if err != nil {
142-
t.Fatal(err)
143-
}
144-
if url != "/files/mybucket/path/to/file.txt" {
145-
t.Errorf("expected /files/mybucket/path/to/file.txt, got %q", url)
146-
}
114+
require.NoError(t, err)
115+
assert.Equal(t, "/files/mybucket/path/to/file.txt", url)
147116
})
148117

149118
t.Run("AggregateRoutes", func(t *testing.T) {
@@ -154,8 +123,6 @@ func TestRouterImprovements(t *testing.T) {
154123
r2.GET("/r2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
155124

156125
combined := teapot.AggregateRoutes(r1, r2)
157-
if len(combined) != 2 {
158-
t.Errorf("expected 2 routes, got %d", len(combined))
159-
}
126+
assert.Len(t, combined, 2)
160127
})
161128
}

0 commit comments

Comments
 (0)