package game import ( "strings" "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")} g := NewGame(Config{Seed: 99, Term: tt}) err := g.Run() if err != nil { t.Fatalf("Run: %v", err) } if g.Playing { t.Error("still playing after quit") } // After quitting, the scoreboard is the last thing shown (in C it went // to stdout after endwin; here it is drawn on the screen). found := false for y := range NumLines { if strings.Contains(g.scr.Std.Line(y), "Top Ten") { found = true } } 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 := NewGame(Config{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 := NewGame(Config{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 := NewGame(Config{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, Config{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) } }