public void Play_OutcomeWinNonHumanPlayersMove2Wins_ReturnsExpectedGameResult() { //Arrange GameId = 3; var player1Mock = new Mock <IPlayer>(); player1Mock.Setup(m => m.Rules).Returns(GameRules); player1Mock.Setup(m => m.Name).Returns("John Hanley"); PlayerMocks.Add(player1Mock); var player1 = player1Mock.Object; Players.Add(player1); var player1Move = new Move(GameAction.Paper, Players[0]); player1Mock.Setup(m => m.SelectMove()).Returns(player1Move); var player2Mock = new Mock <IPlayer>(); player2Mock.Setup(m => m.Rules).Returns(GameRules); player2Mock.Setup(m => m.Name).Returns("Other Player"); PlayerMocks.Add(player2Mock); var player2 = player2Mock.Object; Players.Add(player2); var player2Move = new Move(GameAction.Scissors, Players[1]); player2Mock.Setup(m => m.SelectMove()).Returns(player2Move); var commencementMessage = $"Game<{GameId}>: Commence..."; UiControllerMock.SetupSequence(m => m.Output(commencementMessage)).Pass(); UserInteractionController = UiControllerMock.Object; Game = new Game(GameId, Players, Moves, UserInteractionController); //Act var result = (GameResult)Game.Play(GameRules); //Assert Assert.IsNotNull(result); Assert.AreEqual(ContestOutcome.Win, result.Outcome); UiControllerMock.Verify(m => m.Output(It.IsAny <string>()), Times.Exactly(3)); PlayerMocks[0].Verify(m => m.SelectMove(), Times.Once); PlayerMocks[1].Verify(m => m.SelectMove(), Times.Once); PlayerMocks[0].Verify(m => m.Name, Times.Once); PlayerMocks[1].Verify(m => m.Name, Times.Once); Assert.IsNotNull(result.WinningMove); Assert.AreEqual(result.WinningMove, player2Move); }
public void DisplayTitleSplash_DisplayTitleSplashToTheUser_ExpectedMethodCalls() { //Arrange var titleSplash = $"{Title}\n\nby {Author}"; UiControllerMock.Setup(m => m.Output(titleSplash)); //Act var theApp = (Application)Application; theApp.DisplayTitleSplash(); //Assert UiControllerMock.Verify(m => m.Output(It.IsAny <string>()), Times.Once); }
public void InitialisePlayer_InitialisesPlayerInstance_ReturnsExpectedPlayerInstance() { //Arrange UiControllerMock.Setup(m => m.RequestInput("Please Enter your name:\n\n{A-Z,a-z, }")).Returns("James Kibirige"); //Act var theApp = (Application)Application; var result = theApp.InitialisePlayer(); //Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IPlayer)); Assert.AreEqual("James Kibirige", result.Name); Assert.IsNotNull(result.Rules); UiControllerMock.Verify(m => m.RequestInput(It.IsAny <string>()), Times.Once); }
public void DisplayMatchResults_DisplaysMatchResultMessageToUser_ExecutesExpectedMethods() { //Arrange var displayMatchResultString = "Match<1>: Result\n\nMatch Outcome<Win>\n\nPlayer<James Kibirige>: Score <2>\nPlayer<Jonita Laidley>: Score <0>\n\nPlayer<James Kibirige> Is the Winner!!!\n"; MatchResultMock = new Mock <IContestResult>(); MatchResultMock.Setup(m => m.ToString()).Returns(displayMatchResultString); UiControllerMock.Setup(m => m.Output(displayMatchResultString)); var theApp = (Application)Application; theApp.MatchResult = MatchResultMock.Object; //Act theApp.DisplayMatchResult(); //Assert UiControllerMock.Verify(m => m.Output(It.IsAny <string>()), Times.Once); }
public void InitialiseMatch_InitialisesMatchInstanceWithInValidNumRounds_LoopsUntilValidInputProvided(string input) { //Arrange var initialisingMessage = $"Initialising Match..."; var requestMessage = $"Please Specify number of rounds to play in Match:\n\n{{number using digits 1-9 between 1 and 10}}"; Regex rgx = new Regex(@"^[0-9]+$"); const int expectedNumRounds = 5; UiControllerMock.Setup(m => m.Output(initialisingMessage)); UiControllerMock.Setup(m => m.RequestInput(requestMessage)).Returns(input); UiControllerMock.Setup(m => m.RequestValidInput(It.IsAny <string>(), It.IsAny <Regex>())).Returns(expectedNumRounds.ToString()); var player1Mock = new Mock <IPlayer>(); player1Mock.Setup(m => m.Name).Returns("James Kibirige"); player1Mock.Setup(m => m.Rules); var player2Mock = new Mock <IPlayer>(); player2Mock.Setup(m => m.Name).Returns("Jonita Laidley"); player2Mock.Setup(m => m.Rules); Players.Add(player1Mock.Object); Players.Add(player2Mock.Object); var expectedScoreItemCount = 2; //Act var theApp = (Application)Application; var result = (Match)theApp.InitialiseMatch(1); //Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(Match)); Assert.AreEqual(expectedNumRounds, result.NumRounds); Assert.AreEqual(expectedScoreItemCount, result.Score.Scores.Count); UiControllerMock.Verify(m => m.Output(It.IsAny <string>()), Times.Once); UiControllerMock.Verify(m => m.RequestInput(It.IsAny <string>()), Times.Once); UiControllerMock.Verify(m => m.RequestValidInput(It.IsAny <string>(), It.IsAny <Regex>()), Times.Once); }
public void InitialiseOpponent_InitialisesOpponentInstance_ReturnsExpectedPlayerInstance(string opponentType) { //Arrange var opponentTypeEnumerator = new EnumEnumerator <OpponentType>(); var opponentTypeOptions = string.Join(",", opponentTypeEnumerator.EnumerateEnum()); var requestMessage = $"Select an Opponent Type from the choices:\n\n{{{opponentTypeOptions}}}"; UiControllerMock.Setup(m => m.RequestInput(requestMessage)).Returns(opponentType); Type expectedType; switch (opponentType) { case "Human": expectedType = typeof(HumanPlayer); break; case "Random": expectedType = typeof(RandomPlayer); break; case "Tactical": expectedType = typeof(TacticalPlayer); break; default: expectedType = null; break; } //Act var theApp = (Application)Application; var result = theApp.InitialiseOpponent(); //Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, expectedType); Assert.AreEqual(opponentType, result.Name); Assert.IsNotNull(result.Rules); UiControllerMock.Verify(m => m.RequestInput(It.IsAny <string>()), Times.Once); }
public void Run_ExecutesGamePipeLineWithTacticalOpponent_DoesAllExpectedWork() { //Arrange const string playerName = "James Kibirige"; const string nameRequestMessage = "Please Enter your name:\n\n{A-Z,a-z, }"; UiControllerMock.Setup(m => m.RequestInput(nameRequestMessage)).Returns("James Kibirige"); var opponentTypeEnumerator = new EnumEnumerator <OpponentType>(); var opponentTypeOptions = string.Join(",", opponentTypeEnumerator.EnumerateEnum()); var opponentTypeRequestMessage = $"Select an Opponent Type from the choices:\n\n{{{opponentTypeOptions}}}"; UiControllerMock.Setup(m => m.RequestInput(opponentTypeRequestMessage)).Returns("Tactical"); var numRoundsrequestMessage = $"Please Specify number of rounds to play in Match:\n\n{{number using digits 1-9 between 1 and 10}}"; UiControllerMock.Setup(m => m.RequestInput(numRoundsrequestMessage)).Returns("3"); var possibleActions = string.Join(",", Rules.PossibleGameActions());//String literal listing possible game actions var playerSelectMoveMessage = string.Format("Player <{0}>\nSelect a Move from the possible choices:\n{{{1}}}", playerName, possibleActions); UiControllerMock.SetupSequence(m => m.RequestInput(playerSelectMoveMessage)).Returns("Rock").Returns("Paper").Returns("Scissors"); var yesNoOptions = "Yes,Y,No,N"; var playAgainRequestMessage = $"Do want to Play Again?\n\n{{{yesNoOptions}}}"; UiControllerMock.Setup(m => m.RequestInput(playAgainRequestMessage)).Returns("no"); var theApp = (Application)Application; //Act theApp.Run(); //Assert UiControllerMock.Verify(m => m.Output(It.IsAny <string>()), Times.AtLeast(3)); UiControllerMock.Verify(m => m.RequestInput(It.IsAny <string>()), Times.AtLeast(3)); }