Exemplo n.º 1
0
        public void Test_End_To_End()
        {
            string input = "9\r\n" +
                           "8\r\n" +
                           "nothing\r\n" +
                           "13\r\n" +
                           "-87\r\n" +
                           "2";

            string expected = "9:3,3\r\n" +
                              "8:2,2,2\r\n" +
                              "13:13\r\n" +
                              "-87:1\r\n" +
                              "2:2\r\n";

            var standardOut = new StringWriter();
            var errorOut = new StringWriter();
            var logger = new ConsoleLogger(standardOut, errorOut);

            IPrimeFactorCalculator calculator = new PrimeFactorCalculator(); // The main calculator.
            calculator = new PrimeFactorOutputWriter(calculator, logger);    // Our formatter for dumping to the console
            calculator = new PrimeFactorExceptionLogger(calculator, logger); // An exception handler that can log errors for us.

            var path = TempFileHelpers.CreateTestFile(input);
            IIntegerReader reader = new IntegerFileReader(path);

            int? number;
            while ((number = reader.ReadNext()) != null)
            {
                calculator.Execute(number.Value);
            }

            Assert.AreEqual(expected, standardOut.ToString());
            Assert.AreEqual(string.Empty, errorOut.ToString());
        }
        public void Test_Garbage_Multiple_Numbers()
        {
            var path = TempFileHelpers.CreateTestFile("321\r\n123");

            var reader = new IntegerFileReader(path);

            var number = reader.ReadNext();

            Assert.IsTrue(number.HasValue);
            Assert.AreEqual(321, number.Value);

            number = reader.ReadNext();

            Assert.IsTrue(number.HasValue);
            Assert.AreEqual(123, number.Value);
        }
        public void Test_Empty_File()
        {
            var path = TempFileHelpers.CreateTestFile(string.Empty);

            var reader = new IntegerFileReader(path);

            // Make sure that we don't read anything.
            Assert.AreEqual(reader.ReadNext(), null);
        }
        public void Test_Garbage_Before_Number_File()
        {
            var path = TempFileHelpers.CreateTestFile("Stuff\r\n123");

            var reader = new IntegerFileReader(path);

            var number = reader.ReadNext();

            Assert.IsTrue(number.HasValue);
            Assert.AreEqual(123, number.Value);
        }
        public void Test_Missing_File()
        {
            var path = Path.Combine(Path.GetTempPath(), "FileThatDoesNotExist.txt");

            // Ensure that the file wasn't created for whatever reason.
            Assert.IsFalse(File.Exists(path));

            var reader = new IntegerFileReader(path);

            Assert.Throws<FileNotFoundException>(() =>
                reader.ReadNext(),
                "A missing file exeption was not fired."
            );
        }