Exit the process on game-over instead of unwinding a panic
One game run is one process, so game-over ends the process directly, as the C game did with exit(). myExit now restores the terminal (via the new Terminal.Fini) and calls os.Exit(0); the gameEnd sentinel, the recover in Run, and the recover in DeathDemo are gone. Run() no longer returns an error (it does not return — the game exits from within), and playit's pre-loop setup is split into startLevel/prePlay so tests can drive a bounded number of turns. Because death (combat, and starvation over a long session) now exits the process, the four Run()-to-completion tests can no longer run through the exit path: TestDeathUnwindsWithGameEnd is removed (it tested the deleted unwind), the crash-sweep and quit/save session tests are dropped, and TestRunDownStairs is reworked to drive the turn loop for a single descend. Score rendering, previously checked after a scripted quit, is now covered directly by TestScoreRendersList. Save/restore integrity remains covered by TestSaveRestoreRoundTrip.
This commit is contained in:
149
game/run_test.go
149
game/run_test.go
@@ -5,23 +5,56 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRunScriptedSession drives a complete game through Run(): a few
|
||||
// moves, a rest, an inventory, then Q-quit answered yes.
|
||||
func TestRunScriptedSession(t *testing.T) {
|
||||
tt := &testTerm{input: []byte("hjkl.i Qy")}
|
||||
// driveTurns runs the game's per-turn loop up to n times, doing the same
|
||||
// first-level and pre-play setup Run() does. Run() itself no longer
|
||||
// returns — game-over exits the process — so tests drive command()
|
||||
// directly, with short scripts that avoid quitting, saving, or playing
|
||||
// long enough to starve, any of which would exit the test binary.
|
||||
func driveTurns(t *testing.T, g *RogueGame, n int) {
|
||||
t.Helper()
|
||||
|
||||
g := New(Params{Seed: 99, Term: tt})
|
||||
g.startLevel()
|
||||
g.prePlay()
|
||||
|
||||
err := g.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
for range n {
|
||||
g.command()
|
||||
}
|
||||
}
|
||||
|
||||
if g.Playing {
|
||||
t.Error("still playing after quit")
|
||||
// TestRunDownStairs stands the hero on the staircase and descends via the
|
||||
// '>' command through the real turn loop, then checks the level changed.
|
||||
func TestRunDownStairs(t *testing.T) {
|
||||
// '>' is a free action (After=false), so it is followed by a paying
|
||||
// rest ('.') to end the command() call; without a paying action the
|
||||
// turn loop would spin forever on the auto-fed prompt input.
|
||||
tt := &testTerm{input: []byte(">.")}
|
||||
g := New(Params{Seed: 7, Term: tt})
|
||||
g.NewLevel()
|
||||
g.Player.Pos = g.Level.Stairs // stand on the stairs
|
||||
g.restored = true // keep startLevel from regenerating
|
||||
g.Daemons = DaemonList{} // and give it a fresh daemon table
|
||||
g.StartDaemon(DRunners, 0, After)
|
||||
g.StartDaemon(DDoctor, 0, After)
|
||||
g.Fuse(DSwander, 0, wanderTime(g), After)
|
||||
g.StartDaemon(DStomach, 0, After)
|
||||
|
||||
driveTurns(t, g, 1)
|
||||
|
||||
if g.Depth != 2 {
|
||||
t.Errorf("depth = %d after descending, want 2", g.Depth)
|
||||
}
|
||||
// After quitting, the scoreboard is the last thing shown (in C it went
|
||||
// to stdout after endwin; here it is drawn on the screen).
|
||||
}
|
||||
|
||||
// TestScoreRendersList checks that the scoreboard is drawn on the screen
|
||||
// (in C it went to stdout after endwin; here it stays on the screen). The
|
||||
// quit and death paths that normally show it now exit the process, so the
|
||||
// display is exercised through score() directly.
|
||||
func TestScoreRendersList(t *testing.T) {
|
||||
g := New(Params{Seed: 1, Term: &testTerm{}})
|
||||
g.Player.Purse = 100
|
||||
|
||||
g.score(g.Player.Purse, 1, 0) // flags 1 = quit; posts the top-ten list
|
||||
|
||||
found := false
|
||||
|
||||
for y := range NumLines {
|
||||
@@ -31,96 +64,6 @@ func TestRunScriptedSession(t *testing.T) {
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("score list not on screen after quit")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunManyTurns mashes movement keys for a while as a crash sweep of
|
||||
// the whole turn loop (daemons, hunger, monsters, combat), ending with a
|
||||
// quit. The input alternates directions so the hero bumps around rooms.
|
||||
func TestRunManyTurns(t *testing.T) {
|
||||
// Spaces between commands double as answers to any --More-- prompts;
|
||||
// without them a single prompt would swallow the rest of the script
|
||||
// (wait_for eats everything that isn't a space).
|
||||
moves := []byte("h j k l y u b n s .")
|
||||
script := make([]byte, 0, len(moves)*200+7)
|
||||
|
||||
for range 200 {
|
||||
script = append(script, moves...)
|
||||
}
|
||||
|
||||
script = append(script, " Q y Qy"...)
|
||||
tt := &testTerm{input: script}
|
||||
|
||||
g := New(Params{Seed: 31337, Term: tt})
|
||||
|
||||
err := g.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
}
|
||||
|
||||
if g.Playing {
|
||||
t.Error("session did not end")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunDownStairs walks the hero onto the stairs by teleporting there in
|
||||
// wizard style, then descends and keeps playing.
|
||||
func TestRunDownStairs(t *testing.T) {
|
||||
tt := &testTerm{input: []byte(">..Qy")}
|
||||
g := New(Params{Seed: 7, Term: tt})
|
||||
g.NewLevel()
|
||||
g.Player.Pos = g.Level.Stairs // stand on the stairs
|
||||
g.restored = true // keep Run from regenerating the level
|
||||
g.Daemons = DaemonList{} // and give it a fresh daemon table
|
||||
g.StartDaemon(DRunners, 0, After)
|
||||
g.StartDaemon(DDoctor, 0, After)
|
||||
g.Fuse(DSwander, 0, wanderTime(g), After)
|
||||
g.StartDaemon(DStomach, 0, After)
|
||||
|
||||
err := g.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
}
|
||||
|
||||
if g.Depth != 2 {
|
||||
t.Errorf("depth = %d after descending, want 2", g.Depth)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSaveCommandRoundTrip saves via the 'S' command (as a player would)
|
||||
// and restores the game.
|
||||
func TestSaveCommandRoundTrip(t *testing.T) {
|
||||
// The C get_str caps input at MAXINP=50 characters, so the save path
|
||||
// must be short: work from the temp directory.
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
path := "cmd.save"
|
||||
// 'S' with no default file name goes straight to the name prompt.
|
||||
script := "S" + path + "\n"
|
||||
tt := &testTerm{input: []byte(script)}
|
||||
g := New(Params{Seed: 55, Term: tt})
|
||||
|
||||
g.FileName = "" // force the name prompt
|
||||
|
||||
runErr := g.Run()
|
||||
if runErr != nil {
|
||||
t.Fatalf("Run: %v", runErr)
|
||||
}
|
||||
|
||||
h, err := Restore(path, Params{Term: &testTerm{}})
|
||||
if err != nil {
|
||||
t.Fatalf("Restore: %v", err)
|
||||
}
|
||||
|
||||
if h.Depth != g.Depth || h.Player.Purse != g.Player.Purse {
|
||||
t.Error("restored game does not match saved game")
|
||||
}
|
||||
// The restored game must be playable.
|
||||
setInput(t, h, []byte("..Qy")...)
|
||||
|
||||
restoredErr := h.Run()
|
||||
if restoredErr != nil {
|
||||
t.Fatalf("restored Run: %v", restoredErr)
|
||||
t.Error("score list not on screen")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user