-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame_Test.cs
122 lines (105 loc) · 3.7 KB
/
Game_Test.cs
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
using Xunit;
using ConnectFour;
using System;
namespace Tests {
public class GameTests {
public class MockPlayerThrowsErrorMove: IPlayer {
public int MoveCount {get; set;}
public string Token {get;}
public MockPlayerThrowsErrorMove() {
Token = "*";
MoveCount = 0;
}
public int Move() {
if (this.MoveCount >= 4) {
return 0;
}
this.MoveCount++;
throw new System.ArgumentOutOfRangeException("Invalid");
}
}
[Fact]
public void CallsPlayerMoveAgainWhenInitialMoveThrowsError() {
var board = new ConnectFourBoard();
var mockPlayer = new MockPlayerThrowsErrorMove();
var mockPlayer2 = new MockPlayerThrowsErrorMove();
var game = new ConnectFourGame(board, mockPlayer, mockPlayer2);
game.Play();
Assert.Equal(4, mockPlayer.MoveCount);
Assert.Equal(4, mockPlayer2.MoveCount);
}
public class MockPlayerReturnsZero: IPlayer {
public int MoveCount {get; set;}
public string Token {get;}
public MockPlayerReturnsZero(string token = "*") {
MoveCount = 0;
this.Token = token;
}
public int Move() {
this.MoveCount++;
return 0;
}
}
public class MockBoardThrowsErrorOnPut: IBoard {
public int called;
public int Width {get;}
public int Height {get;}
public string Render () { return "Test Render"; }
public MockBoardThrowsErrorOnPut() {
called = 0;
}
public bool CheckGame(int column, int row)
{
return true;
}
public int PutCounter(int index, string counter)
{
this.called++;
if (this.called >= 2) {
return 0;
}
throw new ArgumentException("No More Room");
}
}
[Fact]
public void CallsPlayerWhenPositionOnBoardIsOutOfBounds() {
var board = new MockBoardThrowsErrorOnPut();
var mockPlayer = new MockPlayerReturnsZero();
var player2 = new MockPlayerReturnsZero();
var game = new ConnectFourGame(board, mockPlayer, player2);
game.Play();
Assert.Equal(2, board.called);
}
public class MockBoardReturnsCheckTrue: IBoard {
public string WinnersToken;
public int Width {get;}
public int Height {get;}
public string Render () { return "Test Render"; }
public int Called;
public MockBoardReturnsCheckTrue() {
this.WinnersToken = string.Empty;
this.Called = 0;
}
public bool CheckGame(int column, int row)
{
return true;
}
public int PutCounter(int index, string counter)
{
this.Called++;
this.WinnersToken = counter;
return 0;
}
}
[Fact]
public void ReturnsFromGameWhenAPlayerWins() {
var board = new MockBoardReturnsCheckTrue();
var mockWinner = new MockPlayerReturnsZero("!");
var mockLoser = new MockPlayerReturnsZero("0");
var game = new ConnectFourGame(board, mockWinner, mockLoser);
game.Play();
Assert.Equal(1, board.Called);
Assert.Equal("!", board.WinnersToken);
}
}
}