public void Read_ValidPath_ShouldReturnSavedValue()
        {
            // arrange
            const string repoDirectory = "Test4/";

            Assert.False(Directory.Exists(repoDirectory));
            const string repoPath  = "Read_ValidPath_ShouldReturnSavedValue.bin";
            const string entryPath = "Test_Read_ValidPath_ShouldReturnSavedValue.bin";
            var          gameState = new ChessGameState(null, true, null, TeamColor.None, PlayerMode.SinglePlayer, 0);
            var          repoSaver = new SaveRepository(repoDirectory, repoPath);

            repoSaver.Save(entryPath, gameState);

            var repoReader = new SaveRepository(repoDirectory, repoPath);

            // act
            bool isInRepo = repoReader.Contains(entryPath);
            var  fromRepo = repoReader.Read(entryPath);

            // assert
            Assert.True(isInRepo);
            Assert.AreEqual(fromRepo.IsEnded, gameState.IsEnded);
            Assert.AreEqual(fromRepo.PlayerMode, gameState.PlayerMode);

            // clear
            Directory.Delete(repoDirectory, true);
        }
        public void Save_FileAlreadySaved_ShouldOverwrite()
        {
            // arrange
            const string repoDirectory = "Test2/";

            Assert.False(Directory.Exists(repoDirectory));
            const string repoPath     = "Save_FileAlreadySaved_ShouldOverwrite.bin";
            const string entryPath    = "Test_Save_FileAlreadySaved_ShouldOverwrite.bin";
            var          gameStateOld = new ChessGameState(null, false, null, TeamColor.None, PlayerMode.SinglePlayer, 0);
            var          gameStateNew = new ChessGameState(null, true, null, TeamColor.None, PlayerMode.SinglePlayer, 0);
            var          repo         = new SaveRepository(repoDirectory, repoPath);

            repo.Save(entryPath, gameStateOld);

            // act
            bool isInRepoBefore = repo.Contains(entryPath);

            repo.Save(entryPath, gameStateNew);
            bool isInRepoAfter = repo.Contains(entryPath);
            var  fromRepo      = repo.Read(entryPath);

            // assert
            Assert.True(isInRepoBefore);
            Assert.True(isInRepoAfter);
            Assert.NotNull(fromRepo);
            Assert.AreEqual(fromRepo.IsEnded, gameStateNew.IsEnded);
            Assert.AreEqual(fromRepo.PlayerMode, gameStateNew.PlayerMode);

            // clear
            Directory.Delete(repoDirectory, true);
        }
        public void Delete_EntryInRepo_ShouldDelete_ShouldReturnTrue()
        {
            // arrange
            const string repoDirectory = "Test6/";

            Assert.False(Directory.Exists(repoDirectory));
            const string repoPath  = "Delete_EntryInRepo_ShouldDelete_ShouldReturnTrue.bin";
            const string entryPath = "Test_Delete_EntryInRepo_ShouldDelete_ShouldReturnTrue.bin";
            var          gameState = new ChessGameState(null, true, null, TeamColor.None, PlayerMode.SinglePlayer, 0);
            var          repoSaver = new SaveRepository(repoDirectory, repoPath);

            repoSaver.Save(entryPath, gameState);

            var repoDeleter = new SaveRepository(repoDirectory, repoPath);

            // act
            bool isInRepo = repoDeleter.Contains(entryPath);
            bool result   = repoDeleter.Delete(entryPath);

            // assert
            Assert.True(isInRepo);
            Assert.True(result);

            // clear
            Directory.Delete(repoDirectory, true);
        }
        private static RepoFilesChoice CreateRepositoryFilesAndGetUserChoice(string message)
        {
            var repo   = SaveRepository.GetDefaultRepository();
            var files  = ShowSavedGamesAndReturnAsList(repo);
            var choice = UserInteraction.GetNumberFromUser(
                message, "Option not found. Please try again", 0, files.Count);

            return(new RepoFilesChoice(repo, files, choice));
        }
        private static List <string> ShowSavedGamesAndReturnAsList(SaveRepository repo)
        {
            Console.WriteLine("Saved games: ");
            var files = repo.GetAllFiles().ToList();

            Console.WriteLine("0. Exit");
            for (int i = 0; i < files.Count(); i++)
            {
                Console.WriteLine($"{i+1}. {files[i]}");
            }
            return(files);
        }
        private IMoveResult SaveGame(IMoveResult moveResult, int currentPlayer)
        {
            Console.WriteLine("Under what name save the game?");
            string file           = UserInteraction.ReadNotEmptyStringFromUser();
            var    saveRepository = SaveRepository.GetDefaultRepository();

            bool isEnded = moveResult.IsCheckMate(TeamColor.Black) || moveResult.IsCheckMate(TeamColor.White);
            var  state   = new ChessGameState(moveResult, isEnded, _players,
                                              _players[currentPlayer].TeamColor, PlayerMode.TwoPlayers, 0);

            saveRepository.Save(file + ".bin", state);
            Console.WriteLine("Game saved.");
            return(new StoppedMoveResult());
        }
        private IMoveResult SaveGame(IMoveResult moveResult)
        {
            Console.WriteLine("Under what name save the game?");
            string file           = UserInteraction.ReadNotEmptyStringFromUser();
            var    saveRepository = SaveRepository.GetDefaultRepository();

            bool isEnded = moveResult.IsCheckMate(TeamColor.Black) || moveResult.IsCheckMate(TeamColor.White);
            var  state   = new ChessGameState(moveResult, isEnded, new [] { _player },
                                              _computer.MyTeamColor, PlayerMode.SinglePlayer, _difficulty);

            saveRepository.Save(file + ".bin", state);
            Console.WriteLine("Game saved.");
            return(new StoppedMoveResult());
        }
        public void Delete_EntryNotRepo_ShouldDelete_ShouldReturnFalse()
        {
            // arrange
            const string repoDirectory = "Test7/";

            Assert.False(Directory.Exists(repoDirectory));
            const string repoPath  = "Delete_EntryInRepo_ShouldDelete_ShouldReturnTrue.bin";
            const string entryPath = "Test_Delete_EntryInRepo_ShouldDelete_ShouldReturnTrue.bin";

            var repoDeleter = new SaveRepository(repoDirectory, repoPath);

            // act
            bool isInRepo = repoDeleter.Contains(entryPath);
            bool result   = repoDeleter.Delete(entryPath);

            // assert
            Assert.False(isInRepo);
            Assert.False(result);

            // clear
            Directory.Delete(repoDirectory, true);
        }
        public void Read_InValidPath_ShouldThrowException()
        {
            // arrange
            const string repoDirectory = "Test5/";

            Assert.False(Directory.Exists(repoDirectory));
            const string repoPath  = "Read_InValidPath_ShouldThrowException.bin";
            const string entryPath = "Test_Read_InValidPath_ShouldThrowException.bin";

            var repoReader = new SaveRepository(repoDirectory, repoPath);

            // act
            bool isInRepo = repoReader.Contains(entryPath);

            Assert.Throws <ArgumentException>(() => repoReader.Read(entryPath));

            // assert
            Assert.False(isInRepo);

            // clear
            Directory.Delete(repoDirectory);
        }
        public void Contains_TrueAndFalse()
        {
            // arrange
            const string repoDirectory = "Test3/";

            Assert.False(Directory.Exists(repoDirectory));
            const string repoPath  = "Contains_Contains_ShouldReturnTrue.bin";
            const string entryPath = "Test_Contains_Contains_ShouldReturnTrue.bin";
            var          gameState = new ChessGameState(null, true, null, TeamColor.None, PlayerMode.SinglePlayer, 0);
            var          repo      = new SaveRepository(repoDirectory, repoPath);

            repo.Save(entryPath, gameState);

            // act
            bool isInValid   = repo.Contains(entryPath);
            bool isInInvalid = repo.Contains(entryPath + "!");

            // assert
            Assert.True(isInValid);
            Assert.False(isInInvalid);

            // clear
            Directory.Delete(repoDirectory, true);
        }
 public RepoFilesChoice(SaveRepository repository, List <string> files, int choice)
 {
     Repository = repository;
     Files      = files;
     Choice     = choice;
 }
예제 #12
0
 public ChangePasswordHandler(IUserRepository userRepository, IEncrypter encrypter, SaveRepository <User> saveRepository)
 {
     this.userRepository = userRepository;
     this.encrypter      = encrypter;
     this.saveRepository = saveRepository;
 }