Exemple #1
0
        static void Main(string[] args)
        {
            try
            {
                //Create the FizzBuzzProcess.
                //custom output list is injected to allow for unit testing.
                var fizzBuzz = new FizzBuzzProcess();

                //The output delegate provides a way to customize how the output will be
                //used. This allows for the caller to determine what form the output will take and
                //how it will be used.
                fizzBuzz.OnOutputText += (string text) =>
                                                    {
                                                        Console.WriteLine(text);
                                                    };

                //Add the custom output (3, 5, 8)
                fizzBuzz.AddCustomOutput(new CustomModuloOutput(3, "Fizz"));
                fizzBuzz.AddCustomOutput(new CustomModuloOutput(5, "Buzz"));
                fizzBuzz.AddCustomOutput(new CustomModuloOutput(8, "Cazz"));

                fizzBuzz.OrderListByDivisor();

                //Process the list for 100 integers
                fizzBuzz.ProcessCount(100);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }

            Console.ReadLine();
        }
        public void ProcessCount_ForOneBuzzOutput_ReturnsCustomOutput()
        {
            var customStub = new Mock<ICustomModuloOutput>();
            customStub.Setup(m => m.Process(5)).Returns("Buzz");
            customStub.Setup(m => m.Process(10)).Returns("Buzz");
            customStub.Setup(m => m.Process(15)).Returns("Buzz");

            var listString = new StringBuilder();

            var fizzBuzzProc = new FizzBuzzProcess();
            fizzBuzzProc.AddCustomOutput(customStub.Object);

            fizzBuzzProc.OnOutputText += (string text) =>
            {
                listString.Append(text + " ");
            };

            fizzBuzzProc.ProcessCount(10);

            Assert.IsTrue(listString.ToString() == "1 2 3 4 Buzz 6 7 8 9 Buzz ", "Output did not match (" + listString.ToString() + ")");
        }
        public void ProcessCount_NormalListProcess_CorrectDelegateCallCount()
        {
            var customFizzMock = new Mock<ICustomModuloOutput>();
            customFizzMock.Setup(m => m.Process(It.IsAny<int>()));

            var customBuzzMock = new Mock<ICustomModuloOutput>();
            customBuzzMock.Setup(m => m.Process(It.IsAny<int>()));

            var delegateCount = 0;

            var fizzBuzzProc = new FizzBuzzProcess();
            fizzBuzzProc.AddCustomOutput(customFizzMock.Object);
            fizzBuzzProc.AddCustomOutput(customBuzzMock.Object);

            fizzBuzzProc.OnOutputText += (string text) =>
                                            {
                                                delegateCount += 1;
                                            };

            fizzBuzzProc.ProcessCount(10);

            Assert.IsTrue(delegateCount == 10, "Was expecting 10 but found " + delegateCount);

            customFizzMock.Verify(m => m.Process(It.IsAny<int>()), Times.Exactly(10), "Process(int) method was not called the correct number of times");
            customBuzzMock.Verify(m => m.Process(It.IsAny<int>()), Times.Exactly(10), "Process(int) method was not called the correct number of times");
        }
        public void ProcessCount_NormalListProcess_CorrectCustomModOutputCallCount()
        {
            var customMock = new Mock<ICustomModuloOutput>();
            customMock.Setup(m => m.Process(It.IsAny<int>()));

            var fizzBuzzProc = new FizzBuzzProcess();
            fizzBuzzProc.AddCustomOutput(customMock.Object);
            fizzBuzzProc.AddCustomOutput(customMock.Object);
            fizzBuzzProc.AddCustomOutput(customMock.Object);
            fizzBuzzProc.AddCustomOutput(customMock.Object);

            fizzBuzzProc.ProcessCount(10);

            customMock.Verify(m => m.Process(It.IsAny<int>()), Times.Exactly(40), "Process(int) method was not called the correct number of times");
        }