Example #1
0
        static void Main(string[] args)
        {
            var input      = "";
            var calculator = new BasicStringCalculator();

            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Basic String Calculator. Type 'Exit' to quit.");
            Console.WriteLine("Multi line input not supported - run Acceptance Tests to demo multi line support.");
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine();

            do
            {
                try
                {
                    var result = calculator.Add(input);
                    Console.WriteLine($"Sum = {result}");
                    Console.WriteLine();
                }
                catch (InvalidNumbersException iex)
                {
                    Console.WriteLine(iex.Message);
                }

                Console.WriteLine("Enter some delimited numbers...");
                input = Console.ReadLine();
            } while (!input.Equals("exit", StringComparison.InvariantCultureIgnoreCase));
        }
        public void Does_Add_Multi_Line_Comma_Delimited_And_Ignores_Too_Large_Numbers()
        {
            //Arrange
            var calculator  = new BasicStringCalculator();
            var inputString = File.ReadAllText(@".\Samples\MultiLineCommaSeparatedWithIgnoredNumbers.txt");

            //Act
            var result = calculator.Add(inputString);

            //Assert
            Assert.AreEqual(41, result);
        }
        public void Does_Add_Multi_Line_With_Multiple_Custom_Delimiters()
        {
            //Arrange
            var calculator  = new BasicStringCalculator();
            var inputString = File.ReadAllText(@".\Samples\MultiDelimiterMultiLine.txt");

            //Act
            var result = calculator.Add(inputString);

            //Assert
            Assert.AreEqual(42, result);
        }
        public void Does_Add_Multi_Line_Comma_Delimited()
        {
            //Arrange
            var calculator  = new BasicStringCalculator();
            var inputString = File.ReadAllText(@".\Samples\MultiLineCommaSeparated.txt");

            //Act
            var result = calculator.Add(inputString);

            //Assert
            Assert.AreEqual(51, result);
        }
        public void Does_Return_Zero_When_Empty_String()
        {
            //Arrange
            var calculator  = new BasicStringCalculator();
            var inputString = "";

            //Act
            var result = calculator.Add(inputString);

            //Assert
            Assert.AreEqual(0, result);
        }
        public void Does_Throw_Error_When_Negative_Numbers_Found()
        {
            //Arrange
            var       calculator              = new BasicStringCalculator();
            var       inputString             = File.ReadAllText(@".\Samples\MultiLineCommaSeparatedWithNegativeNumbers.txt");
            var       expectedNegativeNumbers = new[] { -1, -2, -3, -4 };
            Exception ex = null;

            //Act
            try
            {
                var result = calculator.Add(inputString);
            } catch (Exception nex)
            {
                ex = nex;
            }

            //Assert
            Assert.IsInstanceOfType(ex, typeof(InvalidNumbersException));
            var invalidNumbers = ((InvalidNumbersException)ex).InvalidNumbers;

            Assert.AreEqual(4, invalidNumbers.Count());
            Assert.AreEqual(4, invalidNumbers.Intersect(expectedNegativeNumbers).Count());
        }