-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
746 lines (550 loc) · 16.9 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
package boardgame
import (
"errors"
"log"
"testing"
"github.com/jkomoros/boardgame/enum"
)
//Place to define testing structs and helpers that are useful throughout
type testAgent struct{}
func (t *testAgent) Name() string {
return "Test"
}
func (t *testAgent) DisplayName() string {
return "Robby the Robot"
}
func (t *testAgent) SetUpForGame(game *Game, player PlayerIndex) (state []byte) {
return nil
}
func (t *testAgent) ProposeMove(game *Game, player PlayerIndex, agentState []byte) (move Move, newAgentState []byte) {
state := game.CurrentState()
gameState, _ := concreteStates(state)
if gameState.CurrentPlayer != player {
return nil, nil
}
move = game.MoveByName("Test")
if move == nil {
log.Println("Couldn't find move Test")
return nil, nil
}
move.(*testMove).TargetPlayerIndex = player
return move, nil
}
//testingComponent is a very basic thing that fufills the Component interface.
type testingComponent struct {
c Component
String string
Integer int
}
func (t *testingComponent) ContainingComponent() Component {
return t.c
}
func (t *testingComponent) SetContainingComponent(c Component) {
t.c = c
}
type testingComponentDynamic struct {
state State
ref StatePropertyRef
c Component
IntVar int
Stack Stack
Enum enum.Val
}
const testGameName = "testgame"
func (t *testingComponent) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testingComponentDynamic) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testingComponentDynamic) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testingComponentDynamic) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testingComponentDynamic) ConnectContainingState(state State, ref StatePropertyRef) {
t.state = state
t.ref = ref
}
func (t *testingComponentDynamic) FinishStateSetUp() {
}
func (t *testingComponentDynamic) State() State {
return t.state
}
func (t *testingComponentDynamic) ImmutableState() ImmutableState {
return t.state
}
func (t *testingComponentDynamic) StatePropertyRef() StatePropertyRef {
return t.ref
}
func (t *testingComponentDynamic) ContainingComponent() Component {
return t.c
}
func (t *testingComponentDynamic) SetContainingComponent(c Component) {
t.c = c
}
//Every game should do such a convenience method. state might be nil.
func concreteStates(state ImmutableState) (*testGameState, []*testPlayerState) {
if state == nil {
return nil, nil
}
players := make([]*testPlayerState, len(state.ImmutablePlayerStates()))
for i, player := range state.ImmutablePlayerStates() {
players[i] = player.(*testPlayerState)
}
game, ok := state.ImmutableGameState().(*testGameState)
if !ok {
return nil, nil
}
return game, players
}
type testGameState struct {
state State
ref StatePropertyRef
Phase enum.TreeVal `enum:"phase"`
CurrentPlayer PlayerIndex
DrawDeck Stack `sanitize:"len"`
Timer Timer
MyIntSlice []int
MyStringSlice []string
MyBoolSlice []bool
MyPlayerIndexSlice []PlayerIndex
MyEnumValue enum.Val
MyEnumConst enum.ImmutableVal
DownSizeStack SizedStack `sizedstack:"test, ConstantStackSize" sanitize:"all:visible"`
OtherStack SizedStack `sizedstack:"test,2"`
MyMergedStack MergedStack `concatenate:"DownSizeStack,OtherStack"`
MyBoard Board `stack:"test" board:"3"`
}
func (t *testGameState) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testGameState) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testGameState) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testGameState) ConnectContainingState(state State, ref StatePropertyRef) {
t.state = state
t.ref = ref
}
func (t *testGameState) FinishStateSetUp() {
}
func (t *testGameState) State() State {
return t.state
}
func (t *testGameState) ImmutableState() ImmutableState {
return t.state
}
func (t *testGameState) StatePropertyRef() StatePropertyRef {
return t.ref
}
type testPlayerState struct {
state State
ref StatePropertyRef
//Note: PlayerIndex is stored ehre, but not a normal property or
//serialized, because it's really just a convenience method because it's
//implied by its position in the State.Users array.
playerIndex PlayerIndex
Score int
//by omitting the groupName, it should effectively be 'other'
MovesLeftThisTurn int `sanitize:"hidden"`
Hand SizedStack `sizedstack:"test,2"`
IsFoo bool
EnumVal enum.Val `enum:"color"`
}
func (t *testPlayerState) GameScore() int {
return t.Score
}
func (t *testPlayerState) PlayerIndex() PlayerIndex {
return t.playerIndex
}
func (t *testPlayerState) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testPlayerState) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testPlayerState) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testPlayerState) ConnectContainingState(state State, ref StatePropertyRef) {
t.state = state
t.ref = ref
}
func (t *testPlayerState) FinishStateSetUp() {
}
func (t *testPlayerState) State() State {
return t.state
}
func (t *testPlayerState) ImmutableState() ImmutableState {
return t.state
}
func (t *testPlayerState) StatePropertyRef() StatePropertyRef {
return t.ref
}
type testMoveInvalidPlayerIndex struct {
baseFixUpMove
//This move is a dangerous one and also a fix-up. So make it so by default
//it doesn't apply.
CurrentlyLegal bool
}
var testMoveInvalidPlayerIndexConfig = NewMoveConfig(
"Invalid PlayerIndex",
func() Move {
return new(testMoveInvalidPlayerIndex)
},
nil)
func (t *testMoveInvalidPlayerIndex) HelpText() string {
return "Set one of the PlayerIndex properties to an invalid number, so we can verify that ApplyMove catches it."
}
func (t *testMoveInvalidPlayerIndex) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testMoveInvalidPlayerIndex) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testMoveInvalidPlayerIndex) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testMoveInvalidPlayerIndex) Legal(state ImmutableState, propopser PlayerIndex) error {
if !t.CurrentlyLegal {
return errors.New("Move not currently legal")
}
return nil
}
func (t *testMoveInvalidPlayerIndex) Apply(state State) error {
game, players := concreteStates(state)
game.CurrentPlayer = PlayerIndex(len(players))
return nil
}
type testMoveMakeIllegalPhase struct {
baseMove
}
var testMoveMakeIllegalPhaseConfig = NewMoveConfig(
"Make Illegal Phase",
func() Move {
return new(testMoveMakeIllegalPhase)
},
nil)
func (t *testMoveMakeIllegalPhase) HelpText() string {
return "Sets to illegal phase which should fail to apply"
}
func (t *testMoveMakeIllegalPhase) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testMoveMakeIllegalPhase) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testMoveMakeIllegalPhase) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testMoveMakeIllegalPhase) Legal(state ImmutableState, player PlayerIndex) error {
return nil
}
func (t *testMoveMakeIllegalPhase) Apply(state State) error {
g := state.GameState().(*testGameState)
//phaseNormal is illegal because it is a non-leaf enum val move
g.Phase.SetValue(phaseNormal)
return nil
}
type testMoveIncrementCardInHand struct {
baseMove
TargetPlayerIndex PlayerIndex
}
var testMoveIncrementCardInHandConfig = NewMoveConfig(
"Increment IntValue of Card in Hand",
func() Move {
return new(testMoveIncrementCardInHand)
},
nil)
func (t *testMoveIncrementCardInHand) HelpText() string {
return "Increments the IntValue of the card in the hand"
}
func (t *testMoveIncrementCardInHand) DefaultsForState(state ImmutableState) {
t.TargetPlayerIndex = state.ImmutableCurrentPlayer().StatePropertyRef().PlayerIndex
}
func (t *testMoveIncrementCardInHand) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testMoveIncrementCardInHand) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testMoveIncrementCardInHand) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testMoveIncrementCardInHand) Legal(state ImmutableState, proposer PlayerIndex) error {
game, players := concreteStates(state)
player := players[game.CurrentPlayer]
if !t.TargetPlayerIndex.Equivalent(proposer) {
return errors.New("The proposer is not the target Player index")
}
if !t.TargetPlayerIndex.Equivalent(game.CurrentPlayer) {
return errors.New("The target player index is not the current player")
}
if player.Hand.NumComponents() == 0 {
return errors.New("The current player does not have any components in their hand")
}
if game.DrawDeck.NumComponents() < 1 {
return errors.New("There aren't enough cards left over in the draw deck")
}
return nil
}
func (t *testMoveIncrementCardInHand) Apply(state State) error {
game, players := concreteStates(state)
player := players[game.CurrentPlayer]
for _, component := range player.Hand.Components() {
if component == nil {
continue
}
values := component.DynamicValues()
if values == nil {
return errors.New("DynamicValues was nil")
}
easyValues := values.(*testingComponentDynamic)
easyValues.IntVar += 3
game.DrawDeck.ComponentAt(game.DrawDeck.Len() - 1).MoveToFirstSlot(easyValues.Stack)
return nil
}
return errors.New("Didn't find a component in hand")
}
type testMoveDrawCard struct {
baseMove
TargetPlayerIndex PlayerIndex
}
var testMoveDrawCardConfig = NewMoveConfig(
"Draw Card",
func() Move {
return new(testMoveDrawCard)
},
nil)
func (t *testMoveDrawCard) HelpText() string {
return "Draws one card from draw deck into player's hand"
}
func (t *testMoveDrawCard) DefaultsForState(state ImmutableState) {
t.TargetPlayerIndex = state.ImmutableCurrentPlayer().StatePropertyRef().PlayerIndex
}
func (t *testMoveDrawCard) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testMoveDrawCard) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testMoveDrawCard) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testMoveDrawCard) Legal(state ImmutableState, proposer PlayerIndex) error {
game, players := concreteStates(state)
player := players[game.CurrentPlayer]
if !t.TargetPlayerIndex.Equivalent(proposer) {
return errors.New("The proposer is not equivalent to targetplayerindex")
}
if !t.TargetPlayerIndex.Equivalent(game.CurrentPlayer) {
return errors.New("The target player is not the current player")
}
if player.Hand.SlotsRemaining() == 0 {
return errors.New("The current player does not have enough slots in their hand")
}
if game.DrawDeck.NumComponents() == 0 {
return errors.New("there are no cards to draw")
}
return nil
}
func (t *testMoveDrawCard) Apply(state State) error {
game, players := concreteStates(state)
player := players[game.CurrentPlayer]
if err := game.DrawDeck.ComponentAt(0).MoveToFirstSlot(player.Hand); err != nil {
return errors.New("couldn't move component from draw deck to hand: " + err.Error())
}
return nil
}
type testMoveAdvanceCurentPlayer struct {
baseFixUpMove
}
var testMoveAdvanceCurrentPlayerConfig = NewMoveConfig(
"Advance Current Player",
func() Move {
return new(testMoveAdvanceCurentPlayer)
},
nil)
func (t *testMoveAdvanceCurentPlayer) HelpText() string {
return "Advances to the next player when the current player has no more legal moves they can make this turn."
}
func (t *testMoveAdvanceCurentPlayer) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testMoveAdvanceCurentPlayer) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testMoveAdvanceCurentPlayer) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testMoveAdvanceCurentPlayer) Legal(state ImmutableState, proposer PlayerIndex) error {
game, players := concreteStates(state)
player := players[game.CurrentPlayer]
if player.MovesLeftThisTurn > 0 {
return errors.New("the current player still has moves left this turn")
}
return nil
}
func (t *testMoveAdvanceCurentPlayer) Apply(state State) error {
game, players := concreteStates(state)
//Make sure we're leaving it at 0
players[game.CurrentPlayer].MovesLeftThisTurn = 0
game.CurrentPlayer++
if !game.CurrentPlayer.Valid(state) {
//Must have looped back around to 0
game.CurrentPlayer = 0
}
players[game.CurrentPlayer].MovesLeftThisTurn = 1
return nil
}
type testMove struct {
baseMove
AString string
ScoreIncrement int
TargetPlayerIndex PlayerIndex
ABool bool
}
var testMoveConfig = NewMoveConfig(
"Test",
func() Move {
return new(testMove)
},
nil)
func (t *testMove) HelpText() string {
return "Advances the score of the current player by the specified amount."
}
func (t *testMove) DefaultsForState(state ImmutableState) {
game, _ := concreteStates(state)
if game == nil {
blob, _ := DefaultMarshalJSON(state)
log.Println(string(blob))
return
}
t.TargetPlayerIndex = game.CurrentPlayer
t.ScoreIncrement = 3
}
func (t *testMove) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testMove) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testMove) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testMove) Legal(state ImmutableState, proposer PlayerIndex) error {
game, _ := concreteStates(state)
if !t.TargetPlayerIndex.Equivalent(proposer) {
return errors.New("The target player index is not equivalent to the proposer")
}
if !t.TargetPlayerIndex.Equivalent(game.CurrentPlayer) {
return errors.New("The target player is not hte current player")
}
if game.CurrentPlayer != t.TargetPlayerIndex {
return errors.New("The current player is not the same as the target player")
}
return nil
}
func (t *testMove) Apply(state State) error {
game, players := concreteStates(state)
players[game.CurrentPlayer].Score += t.ScoreIncrement
players[game.CurrentPlayer].MovesLeftThisTurn--
return nil
}
type testAlwaysLegalMove struct {
baseFixUpMove
}
var testAlwaysLegalMoveConfig = NewMoveConfig(
"Test Always Legal Move",
func() Move {
return new(testAlwaysLegalMove)
},
nil)
func (t *testAlwaysLegalMove) HelpText() string {
return "A move that is always legal"
}
func (t *testAlwaysLegalMove) Reader() PropertyReader {
return getDefaultReader(t)
}
func (t *testAlwaysLegalMove) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(t)
}
func (t *testAlwaysLegalMove) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(t)
}
func (t *testAlwaysLegalMove) Legal(state ImmutableState, proposer PlayerIndex) error {
return nil
}
func (t *testAlwaysLegalMove) Apply(state State) error {
//This move doesn't do anything
return nil
}
type illegalMove struct {
baseMove
Stack Stack
}
func (i *illegalMove) Reader() PropertyReader {
return getDefaultReader(i)
}
func (i *illegalMove) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(i)
}
func (i *illegalMove) ReadSetConfigurer() PropertyReadSetConfigurer {
return getDefaultReadSetConfigurer(i)
}
func (i *illegalMove) Legal(state ImmutableState, proposer PlayerIndex) error {
return nil
}
func (i *illegalMove) Apply(state State) error {
return nil
}
var testIllegalMoveConfig = NewMoveConfig(
"Illegal Move",
func() Move {
return new(illegalMove)
},
nil)
func (i *illegalMove) HelpText() string {
return "Move that is illegal because it has an illegal property type on it"
}
//testingComponentValues is designed to be run on a stack.ComponentValues() of
//a stack of testingComponents, in order to convert them all to the specified
//underlying struct.
func testingComponentValues(in []Reader) []*testingComponent {
result := make([]*testingComponent, len(in))
for i := 0; i < len(in); i++ {
if in[i] == nil {
result[i] = nil
continue
}
result[i] = in[i].(*testingComponent)
}
return result
}
func testDefaultGame(t *testing.T, stableIds bool) *Game {
return testGame(t, stableIds, 0, nil, nil)
}
//testGame returns a Game that has not yet had SetUp() called.
func testGame(t *testing.T, stableIds bool, numPlayers int, variant Variant, agentNames []string) *Game {
manager := newTestGameManger(t)
id := ""
secretSalt := ""
if stableIds {
//having the same fixed salt helps make the test predictable regarding
//component ids.
id = "FAKEIDFORTESTING"
secretSalt = "FAKESALTFORTESTING"
}
game, err := manager.newGameImpl(id, secretSalt)
if err != nil {
t.Error("Couldn't create game: " + err.Error())
}
if err := game.setUp(numPlayers, variant, agentNames); err != nil {
t.Error("Couldn't set up game: " + err.Error())
}
return game
}