-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add test setup and game engine * set debug to false during tests * fix nil pointer bug, rename rpc
- Loading branch information
Showing
5 changed files
with
186 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
) | ||
|
||
func TestEngine(t *testing.T) { | ||
configA := Config{ | ||
HostA: "localhost", | ||
PortA: "1234", | ||
HostB: "localhost", | ||
PortB: "4321", | ||
ID: "A", | ||
Debug: false, | ||
} | ||
|
||
configB := configA | ||
configB.ID = "B" | ||
|
||
ready := make(chan GameEngine) | ||
go doSetup(t, configA, ready) | ||
go doSetup(t, configB, ready) | ||
|
||
var engineA *GameEngine | ||
var engineB *GameEngine | ||
|
||
waiting := 2 | ||
for { | ||
engine := <-ready | ||
waiting = waiting - 1 | ||
if engine.ID == "A" { | ||
engineA = &engine | ||
} | ||
engineB = &engine | ||
if waiting == 0 { | ||
break | ||
} | ||
} | ||
|
||
defer engineA.listener.Close() | ||
defer engineB.listener.Close() | ||
|
||
actions := []struct { | ||
engine *GameEngine | ||
key tcell.Key | ||
}{ | ||
{engineA, tcell.KeyEnter}, | ||
{engineA, tcell.KeyUp}, | ||
{engineA, tcell.KeyUp}, | ||
{engineA, tcell.KeyEnter}, | ||
{engineB, tcell.KeyLeft}, | ||
{engineB, tcell.KeyEnter}, | ||
{engineB, tcell.KeyDown}, | ||
{engineB, tcell.KeyDown}, | ||
} | ||
|
||
for _, action := range actions { | ||
ev := generateKeyPress(action.key) | ||
t.Logf("Executing move %s on engine %s", ev.Name(), action.engine.ID) | ||
t.Logf("Got tcell ev %v", ev) | ||
move := action.engine.game.RecieveEvent(ev) | ||
t.Logf("Got move: %v", move) | ||
t.Logf("Game is %v", action.engine.game) | ||
t.Logf("Board is %v", action.engine.game.GetBoard()) | ||
if move.Seq > 0 { | ||
action.engine.game.SendMove(move) | ||
var rpcExitCode int | ||
action.engine.rpc.DoMove(move, &rpcExitCode) | ||
} | ||
} | ||
|
||
} | ||
|
||
func doSetup(t *testing.T, config Config, ready chan<- GameEngine) { | ||
t.Logf("Setting up with config: %v", config) | ||
running, e := setup(config) | ||
if e != nil { | ||
t.Errorf("Error setting up engine. Config: %v, Error: %v", config, e) | ||
} | ||
t.Logf("Setup succeeded for %s", config.ID) | ||
ready <- running | ||
} | ||
|
||
func generateKeyPress(k tcell.Key) *tcell.EventKey { | ||
return tcell.NewEventKey(k, 0, tcell.ModNone) | ||
} |