Ejemplo n.º 1
0
        private static int TryGetInputFieldSizeUntilValid(ConsoleBattleField game)
        {
            int fieldSize = -1;
            bool validSize = false;
            while (!validSize)
            {
                Console.Write("Enter the size of the battle field: [{0}..{1}]: ",
                    game.MinFieldSize, game.MaxFieldSize);
                validSize = game.TryGetInputFieldSize(out fieldSize);
            }

            Console.WriteLine();
            return fieldSize;
        }
        public void TestGetInputFieldSize_GibberishSize()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();

            using (var reader = new StringReader("4fsjdasdi"))
            {
                Console.SetIn(reader);

                int fieldSize;
                bool validFieldSize = testGame.TryGetInputFieldSize(out fieldSize);
                Assert.IsFalse(validFieldSize);
            }
        }
        public void TestGetInputFieldSize_OutOfRangeSize()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();

            using (var reader = new StringReader("125"))
            {
                Console.SetIn(reader);

                int fieldSize;
                bool validFieldSize = testGame.TryGetInputFieldSize(out fieldSize);
                Assert.IsFalse(validFieldSize);
            }
        }
        public void TestGetInputFieldSize_AcceptableSize()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            int validSize = 6;

            using (var reader = new StringReader(validSize.ToString()))
            {
                Console.SetIn(reader);

                int fieldSize;
                bool validFieldSize = testGame.TryGetInputFieldSize(out fieldSize);
                Assert.IsTrue(validFieldSize);
            }
        }