Skip to content

Commit 1d3303a

Browse files
inada-sclaude
andcommitted
Improve test cleanup, error coverage, and assertion messages
- Add cleanTables helper to replace direct SQL DELETE in db_test.go - Add error case tests for convertGamePatch and toIPPort - Replace reflect.DeepEqual in shareddata_test.go with assertEq for better failure output Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2eee1db commit 1d3303a

5 files changed

Lines changed: 54 additions & 21 deletions

File tree

gdxsv/db_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ func Test203CalculateUserBattleCount(t *testing.T) {
207207
}
208208

209209
func Test300Ranking(t *testing.T) {
210-
// ugly...
211-
_, err := getDB().(SQLiteDB).Exec("DELETE FROM user")
212-
must(t, err)
210+
cleanTables(t, "user")
213211

214212
var users []*DBUser
215213
for i := 0; i < 3; i++ {
@@ -338,10 +336,7 @@ func Test300Ranking(t *testing.T) {
338336
}
339337

340338
func Test400Replay(t *testing.T) {
341-
_, err := getDB().(SQLiteDB).Exec("DELETE FROM user")
342-
must(t, err)
343-
_, err = getDB().(SQLiteDB).Exec("DELETE FROM battle_record")
344-
must(t, err)
339+
cleanTables(t, "user", "battle_record")
345340

346341
var users []*DBUser
347342
for i := 0; i < 4; i++ {

gdxsv/lbs_battle_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func Test_toIPPort(t *testing.T) {
1818
}{
1919
{"tcp4 addr", args{"192.168.1.10:1234"}, net.IPv4(192, 168, 1, 10), 1234, false},
2020
{"localhost", args{"localhost:1234"}, net.IPv4(127, 0, 0, 1), 1234, false},
21+
{"missing port", args{"192.168.1.10"}, nil, 0, true},
22+
{"bad port", args{"192.168.1.10:badport"}, nil, 0, true},
23+
{"empty string", args{""}, nil, 0, true},
24+
{"port out of range", args{"192.168.1.10:99999"}, nil, 0, true},
2125
}
2226
for _, tt := range tests {
2327
t.Run(tt.name, func(t *testing.T) {

gdxsv/main_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func assertEq(tb testing.TB, expected, actual interface{}) {
2222
}
2323
}
2424

25+
func cleanTables(tb testing.TB, tables ...string) {
26+
tb.Helper()
27+
db := getDB().(SQLiteDB)
28+
for _, table := range tables {
29+
_, err := db.Exec("DELETE FROM " + table)
30+
must(tb, err)
31+
}
32+
}
33+
2534
func waitFor(tb testing.TB, timeout time.Duration, fn func() bool) {
2635
tb.Helper()
2736
deadline := time.Now().Add(timeout)

gdxsv/patch_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@ func Test_convertGamePatch(t *testing.T) {
5050
},
5151
},
5252
},
53+
{
54+
name: "invalid size",
55+
args: args{patch: &MPatch{Disk: GameDiskDC2, Name: "bad-size", Codes: "7,0,0,0"}},
56+
wantErr: true,
57+
},
58+
{
59+
name: "non-numeric size",
60+
args: args{patch: &MPatch{Disk: GameDiskDC2, Name: "bad-size", Codes: "abc,0,0,0"}},
61+
wantErr: true,
62+
},
63+
{
64+
name: "invalid address",
65+
args: args{patch: &MPatch{Disk: GameDiskDC2, Name: "bad-addr", Codes: "8,zzzz,0,0"}},
66+
wantErr: true,
67+
},
68+
{
69+
name: "invalid original value",
70+
args: args{patch: &MPatch{Disk: GameDiskDC2, Name: "bad-orig", Codes: "8,0,not_a_number,0"}},
71+
wantErr: true,
72+
},
73+
{
74+
name: "invalid changed value",
75+
args: args{patch: &MPatch{Disk: GameDiskDC2, Name: "bad-changed", Codes: "8,0,0,not_a_number"}},
76+
wantErr: true,
77+
},
78+
{
79+
name: "empty codes",
80+
args: args{patch: &MPatch{Disk: GameDiskDC2, Name: "empty", Codes: ""}},
81+
want: &proto.GamePatch{GameDisk: GameDiskDC2, Name: "empty"},
82+
},
83+
{
84+
name: "comment only",
85+
args: args{patch: &MPatch{Disk: GameDiskDC2, Name: "comment-only", Codes: "# just a comment\n# another comment"}},
86+
want: &proto.GamePatch{GameDisk: GameDiskDC2, Name: "comment-only"},
87+
},
5388
}
5489
for _, tt := range tests {
5590
t.Run(tt.name, func(t *testing.T) {

gdxsv/shareddata_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"gdxsv/gdxsv/proto"
5-
"reflect"
65
"testing"
76
"time"
87
)
@@ -64,12 +63,8 @@ func TestSharedData_Sync(t *testing.T) {
6463
McsGames: sd1.GetMcsGames(),
6564
})
6665

67-
if !reflect.DeepEqual(sd1.GetMcsUsers(), sd2.GetMcsUsers()) {
68-
t.Error("GetMcsUsers is different")
69-
}
70-
if !reflect.DeepEqual(sd1.GetMcsGames(), sd2.GetMcsGames()) {
71-
t.Error("GetMcsGames is different")
72-
}
66+
assertEq(t, sd1.GetMcsUsers(), sd2.GetMcsUsers())
67+
assertEq(t, sd1.GetMcsGames(), sd2.GetMcsGames())
7368

7469
sd2.UpdateMcsGameState(battleCode, McsGameStateOpened)
7570
sd2.UpdateMcsUserState(sessionID, McsUserStateJoined)
@@ -82,13 +77,8 @@ func TestSharedData_Sync(t *testing.T) {
8277
UpdatedAt: time.Unix(1, 0),
8378
})
8479

85-
if !reflect.DeepEqual(sd1.GetMcsUsers(), sd2.GetMcsUsers()) {
86-
t.Error("GetMcsUsers is different")
87-
}
88-
89-
if !reflect.DeepEqual(sd1.GetMcsGames(), sd2.GetMcsGames()) {
90-
t.Error("GetMcsGames is different")
91-
}
80+
assertEq(t, sd1.GetMcsUsers(), sd2.GetMcsUsers())
81+
assertEq(t, sd1.GetMcsGames(), sd2.GetMcsGames())
9282

9383
sd2.SetMcsUserCloseReason(sessionID, "timeout")
9484
sd2.UpdateMcsUserState(sessionID, McsUserStateLeft)

0 commit comments

Comments
 (0)