Example #1
0
        public void SystemTimeCanBeResetAfterBeingMocked()
        {
            SystemTimeMocker.SetOneSecondAfter(DateTime.Now.AddDays(oneDay));
            SystemTimeMocker.Reset();

            SystemTime.Now.Should().BeCloseTo(DateTime.Now, acceptableInaccuracy);
        }
Example #2
0
        public void GivenScoreIsAddedToPlayersInGivenMatchesInGroups(Table table)
        {
            if (table == null)
            {
                throw new ArgumentNullException(nameof(table));
            }

            foreach (TableRow row in table.Rows)
            {
                TestUtilities.ParseScoreAddedToMatchPlayer(row, out int _, out int groupIndex, out int matchIndex, out string scoringPlayer, out int scoreAdded);

                GroupBase group = createdGroups[groupIndex];
                Match     match = group.Matches[matchIndex];

                SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

                Player player = match.FindPlayer(scoringPlayer);

                if (player != null)
                {
                    player.IncreaseScore(scoreAdded);
                }
                else
                {
                    throw new Exception("Invalid player name in given match within given group");
                }
            }
        }
Example #3
0
        public void SolvingTieWhenThereIsNoTieDoesNothing()
        {
            PlayerReference maruPlayerReference  = tournament.RegisterPlayerReference("Maru");
            PlayerReference storkPlayerReference = tournament.RegisterPlayerReference("Stork");
            PlayerReference taejaPlayerReference = tournament.RegisterPlayerReference("Taeja");

            round.SetPlayersPerGroupCount(3);

            GroupBase group = round.Groups.First();
            Match     match;

            match = round.Groups.First().Matches[0];
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(2);

            match = round.Groups.First().Matches[1];
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player2.IncreaseScore(2);

            match = round.Groups.First().Matches[2];
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(2);

            group.HasProblematicTie().Should().BeFalse();
            group.SolveTieByChoosing(maruPlayerReference.Id).Should().BeFalse();
        }
Example #4
0
        public void CanSolveTie()
        {
            PlayerReference maruPlayerReference = tournament.RegisterPlayerReference("Maru");

            round.SetPlayersPerGroupCount(3);
            PlayerReference storkPlayerReference = tournament.RegisterPlayerReference("Stork");

            round.SetAdvancingPerGroupCount(2);
            PlayerReference taejaPlayerReference = tournament.RegisterPlayerReference("Taeja");

            BracketRound bracketRound = tournament.AddBracketRound();

            GroupBase group = round.Groups.First();

            foreach (Match match in round.Groups.First().Matches)
            {
                SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
                match.Player1.IncreaseScore(2);
            }

            group.HasProblematicTie().Should().BeTrue();
            group.HasSolvedTie().Should().BeFalse();
            group.SolveTieByChoosing(taejaPlayerReference.Id).Should().BeFalse();

            group.HasProblematicTie().Should().BeTrue();
            group.HasSolvedTie().Should().BeFalse();
            group.SolveTieByChoosing(storkPlayerReference.Id).Should().BeTrue();

            group.HasProblematicTie().Should().BeTrue();
            group.HasSolvedTie().Should().BeTrue();

            bracketRound.Groups.First().Matches[0].Player1.GetName().Should().Be("Taeja");
            bracketRound.Groups.First().Matches[0].Player2.GetName().Should().Be("Stork");
        }
Example #5
0
        public void MockedSystemTimeReturnsMockedNow()
        {
            DateTime tomorrow = DateTime.Now.AddDays(oneDay);

            SystemTimeMocker.SetOneSecondAfter(tomorrow);

            SystemTime.Now.Should().BeCloseTo(tomorrow, acceptableInaccuracy);
        }
Example #6
0
        public void CannotDecreasePlayerScoreBelowZero()
        {
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

            player.DecreaseScore(1).Should().BeTrue();

            player.Score.Should().Be(0);
        }
Example #7
0
        public void CannotCreateMatchBetForMatchThatIsOngoing()
        {
            SystemTimeMocker.SetOneSecondAfter(firstMatch.StartDateTime);

            MatchBet matchBet = MatchBet.Create(better, firstMatch, firstMatch.Player1);

            matchBet.Should().BeNull();
        }
Example #8
0
        public void PlayStateIsEqualToOngoingWhenMatchHasStartedButNotFinished()
        {
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(GetWinningScore() - 1);

            PlayStateEnum playState = match.GetPlayState();

            playState.Should().Be(PlayStateEnum.Ongoing);
        }
Example #9
0
        public void CanGetLosingPlayerWhenMatchIsFinished()
        {
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(GetWinningScore());

            Player player = match.GetLosingPlayer();

            player.Should().Be(match.Player2);
        }
Example #10
0
        public void CannotGetLosingPlayerWhileMatchIsOngoing()
        {
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(GetWinningScore() - 1);

            Player player = match.GetLosingPlayer();

            player.Should().BeNull();
        }
Example #11
0
        public void CannotChangeBestOfWhenMatchHasStarted()
        {
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

            bool setResult = match.SetBestOf(7);

            setResult.Should().BeFalse();
            match.BestOf.Should().Be(3);
        }
Example #12
0
        public void CanIncreaseScore()
        {
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            int score = 1;

            player.IncreaseScore(score).Should().BeTrue();

            player.Score.Should().Be(score);
        }
Example #13
0
        public void PlayStateIsEqualToFinishedWhenMatchHasAWinner()
        {
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(GetWinningScore());

            PlayStateEnum playState = match.GetPlayState();

            playState.Should().Be(PlayStateEnum.Finished);
        }
Example #14
0
        public void CannotPlaceMatchBetOnMatchThatIsOngoing()
        {
            Better better = tournament.AddBetter(user);

            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

            better.PlaceMatchBet(match, match.Player1);

            better.Bets.Should().BeEmpty();
        }
Example #15
0
        public void CannotDecreasePlayerScoreWhenTournamentHasIssues()
        {
            TurnTournamentInvalid();

            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

            player.DecreaseScore(1).Should().BeFalse();

            player.Score.Should().Be(0);
        }
Example #16
0
        public void CannotCreateMatchBetForMatchThatIsFinished()
        {
            SystemTimeMocker.SetOneSecondAfter(firstMatch.StartDateTime);

            int winningScore = (int)Math.Ceiling(firstMatch.BestOf / 2.0);

            firstMatch.Player1.IncreaseScore(winningScore);

            MatchBet matchBet = MatchBet.Create(better, firstMatch, firstMatch.Player1);

            matchBet.Should().BeNull();
        }
Example #17
0
        public void GivenGroupsWithinTournamentIsPlayedOutAndBettedOn(Table table)
        {
            if (table == null)
            {
                throw new ArgumentNullException(nameof(table));
            }

            foreach (TableRow row in table.Rows)
            {
                TestUtilities.ParseTargetGroupToPlay(row, out int tournamentIndex, out int roundIndex, out int groupIndex);

                bool tournamentIndexIsValid = createdTournaments.Count > tournamentIndex;
                bool roundIndexIsValid      = createdTournaments[tournamentIndex].Rounds.Count > roundIndex;
                bool groupIndexIsValid      = createdTournaments[tournamentIndex].Rounds[roundIndex].Groups.Count > groupIndex;

                if (!tournamentIndexIsValid || !roundIndexIsValid || !groupIndexIsValid)
                {
                    throw new IndexOutOfRangeException("Tournament, round, or group with given index does not exist");
                }

                SystemTimeMocker.Reset();
                Tournament tournament = createdTournaments[tournamentIndex];
                RoundBase  round      = tournament.Rounds[roundIndex];
                GroupBase  group      = round.Groups[groupIndex];

                while (group.GetPlayState() != PlayStateEnum.Finished)
                {
                    bool tournamentHasBetters = tournament.Betters.Count > 0;
                    if (tournamentHasBetters)
                    {
                        PlaceBetsOnAvailableMatchesInGroup(tournament.Betters, group);
                    }

                    foreach (Match match in group.Matches)
                    {
                        if (match.IsReady() && match.GetPlayState() == PlayStateEnum.NotBegun)
                        {
                            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
                            break;
                        }
                    }

                    bool playedMatchesSuccessfully = PlayAvailableMatches(group);

                    if (!playedMatchesSuccessfully)
                    {
                        break;
                    }
                }
            }
        }
Example #18
0
        public void CannotPlaceMatchBetOnMatchThatIsFinished()
        {
            Better better = tournament.AddBetter(user);

            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

            int winningScore = (int)Math.Ceiling(match.BestOf / 2.0);

            match.Player1.IncreaseScore(winningScore);

            better.PlaceMatchBet(match, match.Player1);

            better.Bets.Should().BeEmpty();
        }
Example #19
0
        public void CanDetectWhenRoundRobinRoundContainsProblematicTie()
        {
            round.SetPlayersPerGroupCount(3);
            tournament.RegisterPlayerReference("Maru");
            tournament.RegisterPlayerReference("Stork");
            tournament.RegisterPlayerReference("Taeja");

            foreach (Match match in round.Groups.First().Matches)
            {
                SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
                match.Player1.IncreaseScore(2);
            }

            round.HasProblematicTie().Should().BeTrue();
        }
Example #20
0
        public void RoundIsOngoingUntilTieIsSolved()
        {
            round.SetPlayersPerGroupCount(3);
            tournament.RegisterPlayerReference("Maru");
            tournament.RegisterPlayerReference("Stork");
            tournament.RegisterPlayerReference("Taeja");

            foreach (Match match in round.Groups.First().Matches)
            {
                SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
                match.Player1.IncreaseScore(2);
            }

            round.GetPlayState().Should().Be(PlayStateEnum.Ongoing);
        }
Example #21
0
        public void CannotReplaceMatchBetOnMatchThatIsOngoing()
        {
            Better better = tournament.AddBetter(user);

            better.PlaceMatchBet(match, match.Player1);
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

            better.PlaceMatchBet(match, match.Player2);

            better.Bets.Should().HaveCount(1);
            better.Bets.First().Should().NotBeNull();

            MatchBet matchBet = better.Bets.First() as MatchBet;

            ValidateMatchBet(matchBet, better, match, match.Player1);
        }
Example #22
0
        private void PlayMatch(TournamentRepository tournamentRepository, Match match)
        {
            bool matchHaveNotStarted = match.StartDateTime > SystemTime.Now;

            if (matchHaveNotStarted)
            {
                SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            }

            int winningScore = (int)Math.Ceiling(match.BestOf / 2.0);

            // Give points to player with name that precedes the other alphabetically
            bool increasePlayer1Score = match.Player1.GetName().CompareTo(match.Player2.GetName()) <= 0;

            Tournament tournament      = match.Group.Round.Tournament;
            Guid       scoringPlayerId = increasePlayer1Score ? match.Player1.Id : match.Player2.Id;

            tournamentRepository.AddScoreToPlayerInMatch(tournament, match.Id, scoringPlayerId, winningScore);
            tournamentRepository.Save();
        }
Example #23
0
        public void GivenGroupsWithinTournamentIsPlayedOut(string tournamentName, Table table)
        {
            using (TournamentRepository tournamentRepository = CreateTournamentRepository())
            {
                foreach (TableRow row in table.Rows)
                {
                    TestUtilities.ParseTargetGroupToPlay(row, out int _, out int roundIndex, out int groupIndex);

                    SystemTimeMocker.Reset();
                    Tournament tournament = tournamentRepository.GetTournamentByName(tournamentName);
                    RoundBase  round      = tournament.Rounds[roundIndex];
                    GroupBase  group      = round.Groups[groupIndex];

                    foreach (Match match in group.Matches)
                    {
                        PlayMatch(tournamentRepository, match);
                    }

                    tournamentRepository.Save();
                }
            }
        }
Example #24
0
        public void DoesNotFlagRoundAsProlematicTieWhenNoProblematicTieHappens()
        {
            round.SetPlayersPerGroupCount(3);
            tournament.RegisterPlayerReference("Maru");
            tournament.RegisterPlayerReference("Stork");
            tournament.RegisterPlayerReference("Taeja");

            Match match;

            match = round.Groups.First().Matches[0];
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(2);

            match = round.Groups.First().Matches[1];
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player2.IncreaseScore(2);

            match = round.Groups.First().Matches[2];
            SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);
            match.Player1.IncreaseScore(2);

            round.HasProblematicTie().Should().BeFalse();
        }
Example #25
0
        public void GivenScoreIsAddedToPlayersInGivenMatchesInGroups(string tournamentName, Table table)
        {
            if (table == null)
            {
                throw new ArgumentNullException(nameof(table));
            }

            using (TournamentRepository tournamentRepository = CreateTournamentRepository())
            {
                Tournament tournament = tournamentRepository.GetTournamentByName(tournamentName);

                foreach (TableRow row in table.Rows)
                {
                    TestUtilities.ParseScoreAddedToMatchPlayer(row, out int roundIndex, out int groupIndex, out int matchIndex, out string scoringPlayer, out int scoreAdded);

                    RoundBase round = tournament.Rounds[roundIndex];
                    GroupBase group = round.Groups[groupIndex];
                    Match     match = group.Matches[matchIndex];

                    SystemTimeMocker.SetOneSecondAfter(match.StartDateTime);

                    Player player = match.FindPlayer(scoringPlayer);

                    if (player != null)
                    {
                        tournamentRepository.AddScoreToPlayerInMatch(tournament, match.Id, player.Id, scoreAdded);
                    }
                    else
                    {
                        // LOG Error: Invalid player name in given match within given group
                        throw new NotImplementedException();
                    }
                }

                tournamentRepository.Save();
            }
        }
Example #26
0
 public SystemTimeTests()
 {
     SystemTimeMocker.Reset();
 }
Example #27
0
 public static void AfterScenario()
 {
     SystemTimeMocker.Reset();
 }
Example #28
0
        public void MockedSystemTimeReturnsMockedUtcNow()
        {
            SystemTimeMocker.SetOneSecondAfter(DateTime.Now.AddDays(oneDay));

            SystemTime.UtcNow.Should().BeCloseTo(DateTime.UtcNow.AddDays(oneDay), acceptableInaccuracy);
        }
Example #29
0
 public void Dispose()
 {
     SystemTimeMocker.Reset();
 }