-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
88 lines (75 loc) · 1.8 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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)
}