Skip to content

Commit

Permalink
test: cleanup existing tests; add TestMenu_Output
Browse files Browse the repository at this point in the history
  • Loading branch information
Broderick-Westrope committed Dec 25, 2024
1 parent 9d17bc7 commit d2cc85c
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 76 deletions.
33 changes: 14 additions & 19 deletions internal/tui/views/leaderboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package views
import (
"database/sql"
"fmt"
"io"
"testing"
"time"

Expand All @@ -14,7 +13,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestLeaderboardCapacity(t *testing.T) {
func TestLeaderboard_TableEntries(t *testing.T) {
db := setupInMemoryDB(t)
repo := data.NewLeaderboardRepository(db)

Expand All @@ -37,7 +36,7 @@ func TestLeaderboardCapacity(t *testing.T) {
for i := range tc.count {
_, err := repo.Save(&data.Score{
GameMode: t.Name(),
Name: fmt.Sprintf("testuser-%d", i),
Name: fmt.Sprintf("user-%d", i),
Time: time.Second * time.Duration(i*2),
Score: i * 100,
Lines: i,
Expand All @@ -54,22 +53,20 @@ func TestLeaderboardCapacity(t *testing.T) {
tm := teatest.NewTestModel(t, m)
tm.Send(tea.Quit())

outBytes, err := io.ReadAll(tm.FinalOutput(t))
require.NoError(t, err)

outBytes := []byte(tm.FinalModel(t).View())
teatest.RequireEqualOutput(t, outBytes)
})
}
}

func TestLeaderboardNewEntry(t *testing.T) {
func TestLeaderboard_NewEntryInEmptyTable(t *testing.T) {
db := setupInMemoryDB(t)

m, err := NewLeaderboardModel(&tui.LeaderboardInput{
GameMode: t.Name(),
NewEntry: &data.Score{
GameMode: t.Name(),
Name: "testuser-new",
Name: "user-new",
Time: time.Minute,
Score: 1000,
Lines: 2,
Expand All @@ -81,20 +78,18 @@ func TestLeaderboardNewEntry(t *testing.T) {
tm := teatest.NewTestModel(t, m)
tm.Send(tea.Quit())

outBytes, err := io.ReadAll(tm.FinalOutput(t))
require.NoError(t, err)

outBytes := []byte(tm.FinalModel(t).View())
teatest.RequireEqualOutput(t, outBytes)
}

func TestLeaderboardNavigation(t *testing.T) {
func TestLeaderboard_KeyboardNavigation(t *testing.T) {
db := setupInMemoryDB(t)
repo := data.NewLeaderboardRepository(db)

for i := range 50 {
_, err := repo.Save(&data.Score{
GameMode: t.Name(),
Name: fmt.Sprintf("testuser-%d", i),
Name: fmt.Sprintf("user-%d", i),
Time: time.Second * time.Duration(i*2),
Score: i * 100,
Lines: i,
Expand All @@ -107,7 +102,7 @@ func TestLeaderboardNavigation(t *testing.T) {
GameMode: t.Name(),
NewEntry: &data.Score{
GameMode: t.Name(),
Name: "testuser-new",
Name: "user-new",
Time: time.Minute,
Score: 2001,
Lines: 2,
Expand All @@ -117,13 +112,13 @@ func TestLeaderboardNavigation(t *testing.T) {
require.NoError(t, err)

tm := teatest.NewTestModel(t, m)
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
time.Sleep(10 * time.Millisecond)
for range 3 {
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
time.Sleep(10 * time.Millisecond)
}
tm.Send(tea.Quit())

outBytes, err := io.ReadAll(tm.FinalOutput(t))
require.NoError(t, err)

outBytes := []byte(tm.FinalModel(t).View())
teatest.RequireEqualOutput(t, outBytes)
}

Expand Down
28 changes: 27 additions & 1 deletion internal/tui/views/menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,33 @@ import (
"github.com/stretchr/testify/require"
)

func TestMenuFormCompletion(t *testing.T) {
func TestMenu_Output(t *testing.T) {
m := NewMenuModel(&tui.MenuInput{})
tm := teatest.NewTestModel(t, m)

// Input username
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("testuser")})
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
time.Sleep(10 * time.Millisecond)

// Select game mode
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
time.Sleep(10 * time.Millisecond)
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
time.Sleep(10 * time.Millisecond)

// Select level
for range 3 {
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
time.Sleep(10 * time.Millisecond)
}

tm.Send(tea.Quit())
outBytes := []byte(tm.FinalModel(t).View())
teatest.RequireEqualOutput(t, outBytes)
}

func TestMenu_SwitchModeMsg(t *testing.T) {
modeToMoveDownCount := func(mode tui.Mode) int {
switch mode {
case tui.ModeMarathon:
Expand Down

This file was deleted.

23 changes: 0 additions & 23 deletions internal/tui/views/testdata/TestLeaderboardNavigation.golden

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Rank Name Time Score Lines Level
────────────────────────────────────────────────────────
14 user-36 1m12s 3600 36 38
15 user-35 1m10s 3500 35 37
16 user-34 1m8s 3400 34 36
17 user-33 1m6s 3300 33 35
18 user-32 1m4s 3200 32 34
19 user-31 1m2s 3100 31 33
20 user-30 1m0s 3000 30 32
21 user-29 58s 2900 29 31
22 user-28 56s 2800 28 30
23 user-27 54s 2700 27 29
24 user-26 52s 2600 26 28
25 user-25 50s 2500 25 27
26 user-24 48s 2400 24 26
27 user-23 46s 2300 23 25
28 user-22 44s 2200 22 24
29 user-21 42s 2100 21 23
30 user-new 1m0s 2001 2 3
31 user-20 40s 2000 20 22
32 user-19 38s 1900 19 21
33 user-18 36s 1800 18 20
escape exit • ? help
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[?25l[?2004h Rank Name Time Score Lines Level
Rank Name Time Score Lines Level
────────────────────────────────────────────────────────
1 testuser-… 1m0s 1000 2 3
1 user-new 1m0s 1000 2 3



Expand All @@ -20,4 +20,4 @@



escape exit • ? help[?2004l[?25h[?1002l[?1003l[?1006l
escape exit • ? help
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[?25l[?2004h Rank Name Time Score Lines Level
Rank Name Time Score Lines Level
────────────────────────────────────────────────────────


Expand All @@ -20,4 +20,4 @@



escape exit • ? help[?2004l[?25h[?1002l[?1003l[?1006l
escape exit • ? help
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[?25l[?2004h Rank Name Time Score Lines Level
Rank Name Time Score Lines Level
────────────────────────────────────────────────────────
1 testuser-2 4s 200 2 4
2 testuser-1 2s 100 1 3
3 testuser-0 0s 0 0 2
1 user-2 4s 200 2 4
2 user-1 2s 100 1 3
3 user-0 0s 0 0 2



Expand All @@ -20,4 +20,4 @@



escape exit • ? help[?2004l[?25h[?1002l[?1003l[?1006l
escape exit • ? help
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Rank Name Time Score Lines Level
────────────────────────────────────────────────────────
1 user-49 1m38s 4900 49 51
2 user-48 1m36s 4800 48 50
3 user-47 1m34s 4700 47 49
4 user-46 1m32s 4600 46 48
5 user-45 1m30s 4500 45 47
6 user-44 1m28s 4400 44 46
7 user-43 1m26s 4300 43 45
8 user-42 1m24s 4200 42 44
9 user-41 1m22s 4100 41 43
10 user-40 1m20s 4000 40 42
11 user-39 1m18s 3900 39 41
12 user-38 1m16s 3800 38 40
13 user-37 1m14s 3700 37 39
14 user-36 1m12s 3600 36 38
15 user-35 1m10s 3500 35 37
16 user-34 1m8s 3400 34 36
17 user-33 1m6s 3300 33 35
18 user-32 1m4s 3200 32 34
19 user-31 1m2s 3100 31 33
20 user-30 1m0s 3000 30 32
escape exit • ? help
34 changes: 34 additions & 0 deletions internal/tui/views/testdata/TestMenu_Output.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

______________________ ______________
/_ __/ ____/_ __/ __ \/ _/ ____/ __ \
/ / / __/ / / / /_/ // // / __/ / / /
/ / / /___ / / / _, _// // /_/ / /_/ /
/_/ /_____/ /_/ /_/ |_/___/\____/\____/


Username:
> testuser

Game Mode:
Marathon
> Sprint (40 Lines)
Ultra (Time Trial)

┃ Starting Level:
┃ 1
┃ 2
┃ 3
┃ > 4
┃ 5
┃ 6
┃ 7
┃ 8
┃ 9
┃ 10
┃ 11
┃ 12
┃ 13
┃ 14
┃ 15

↑ up • ↓ down • / filter • shift+tab back • enter submit

0 comments on commit d2cc85c

Please sign in to comment.