예제 #1
0
        public void EmptyStringReturnsZero()
        {
            var input  = "";
            var output = StringCalculator.Calculate(input);

            Assert.AreEqual(output, 0);
        }
예제 #2
0
        public void SingleNumberReturnsValue()
        {
            var input  = "8";
            var output = StringCalculator.Calculate(input);

            Assert.AreEqual(8, output);
        }
예제 #3
0
        public void EmptyStringReturnsZero()
        {
            var stringCalculator = new StringCalculator();


            Assert.AreEqual(0, stringCalculator.Calculate(""));
        }
예제 #4
0
        public void TwoNumbersNewLineDelimitedReturnsSum()
        {
            var input  = "8\n2";
            var output = StringCalculator.Calculate(input);

            Console.WriteLine(output);
            Assert.AreEqual(output, 10);
        }
예제 #5
0
        public void Should_Calculate_NegativeNumbers_ThrowsException()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("-2\n3,4");

            //Assert
        }
예제 #6
0
        public void Should_Calculate_WithMultiCharDelimiter_ReturnsSum()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("//[###]2###3###4");

            //Assert
            Assert.AreEqual(9, result);
        }
예제 #7
0
        public void Should_Calculate_WithManySingleOrMultiCharDelimiter_ReturnsSum()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("//[$][,]2$3,4,5");

            //Assert
            Assert.AreEqual(14, result);
        }
예제 #8
0
        public void Should_Calculate_ThreeNumbersEitherDelimited_ReturnsSum()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("2\n3,4");

            //Assert
            Assert.AreEqual(9, result);
        }
예제 #9
0
        public void Should_Calculate_TwoNumbersNewLineDelimited_ReturnsSum()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("2\n3");

            //Assert
            Assert.AreEqual(5, result);
        }
예제 #10
0
        public void Should_Calculate_NumbersGreaterThan1000_AreIgnored()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("2\n1001,4");

            //Assert
            Assert.AreEqual(6, result);
        }
예제 #11
0
        public void Should_Calculate_WithSingleLineDelimiter_ReturnsSum()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("//#2#3#4");

            //Assert
            Assert.AreEqual(9, result);
        }
예제 #12
0
        public void Should_Calculate_EmptyString_ReturnsZero()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("");

            //Assert
            Assert.AreEqual(0, result);
        }
예제 #13
0
        public void Should_Calculate_SingleNumber_ReturnsNumber()
        {
            //Arrange

            //Act
            int result = StringCalculator.Calculate("2");

            //Assert
            Assert.AreEqual(2, result);
        }
예제 #14
0
        public void Calculator_GetsSumNumberSentenceForString(string inputString, string expectedNumberSentence)
        {
            var parser = new InputStringParser();

            parser.Settings.AllowNegativeValues = true;

            var sut            = new StringCalculator(parser);
            var numberSentence = sut.Calculate(inputString, new AdditionOperation()).NumberSentence;

            Assert.Equal(expectedNumberSentence, numberSentence);
        }
예제 #15
0
        public void Calculator_GetsProductForString(string inputString, int expectedProduct)
        {
            var parser = new InputStringParser();

            parser.Settings.AllowNegativeValues = true;
            parser.Settings.Delimiters.Add("\n");

            var sut     = new StringCalculator(parser);
            var product = sut.Calculate(inputString, new MultiplicationOperation()).Result;

            Assert.Equal(expectedProduct, product);
        }
예제 #16
0
        public void Calculator_GetsDifferenceForString(string inputString, int expectedDifference)
        {
            var parser = new InputStringParser();

            parser.Settings.AllowNegativeValues = true;
            parser.Settings.Delimiters.Add("\n");

            var sut        = new StringCalculator(parser);
            var difference = sut.Calculate(inputString, new SubtractionOperation()).Result;

            Assert.Equal(expectedDifference, difference);
        }
예제 #17
0
        public void Calculator_GetsQuotientForString(string inputString, int expectedQuotient)
        {
            var parser = new InputStringParser();

            parser.Settings.AllowNegativeValues = true;
            parser.Settings.Delimiters.Add("\n");

            var sut      = new StringCalculator(parser);
            var quotient = sut.Calculate(inputString, new DivisionOperation()).Result;

            Assert.Equal(expectedQuotient, quotient);
        }
예제 #18
0
        public void Calculator_GetsSumForString(string inputString, int expectedSum)
        {
            var parser = new InputStringParser();

            parser.Settings.AllowNegativeValues = true;
            parser.Settings.Delimiters.Add("\n");

            var sut = new StringCalculator(parser);
            var sum = sut.Calculate(inputString, new AdditionOperation()).Result;

            Assert.Equal(expectedSum, sum);
        }
예제 #19
0
        public void Calculator_ShouldThrowExceptionForNegativeValues(string inputString, int[] valuesInExceptionMessage)
        {
            var parser = new InputStringParser();

            parser.Settings.AllowNegativeValues = false;
            parser.Settings.Delimiters.Add("\n");

            var sut = new StringCalculator(parser);

            var ex = Assert.Throws <NegativeValuesNotSupportedException>(() => sut.Calculate(inputString, new AdditionOperation()));

            foreach (var number in valuesInExceptionMessage)
            {
                Assert.Contains(number.ToString(), ex.Message);
            }
        }
예제 #20
0
        public void NumbersGreaterThan1000()
        {
            var stringCalculator = new StringCalculator();

            Assert.Throws <NumberGreaterThanThousandException>(() => stringCalculator.Calculate("1001"));
        }
예제 #21
0
        public void NumbersMixedWithStrings()
        {
            var stringCalculator = new StringCalculator();

            Assert.AreEqual(2, stringCalculator.Calculate("abc,2"));
        }
 public void CalculateWorks(String expression, Double expectedValue)
 {
     Assert.That(calculator.Calculate(expression), Is.EqualTo(expectedValue));
 }
예제 #23
0
        public void SumOfTenNumbersWithDotAsDelimiter()
        {
            var stringCalculator = new StringCalculator();

            Assert.AreEqual(55, stringCalculator.Calculate("1,2.3.4.5.6.7.8.9.10"));
        }
예제 #24
0
        public void NegativeNumbersThrowException()
        {
            var stringCalculator = new StringCalculator();

            Assert.Throws <NegativeNumbersException>(() => stringCalculator.Calculate("-12"));
        }
예제 #25
0
        public void TwoNumbersAndCommaDelimited()
        {
            var stringCalculator = new StringCalculator();

            Assert.AreEqual(4, stringCalculator.Calculate("1,3"));
        }
예제 #26
0
        public void ThreeNumbersDelimited()
        {
            var stringCalculator = new StringCalculator();

            Assert.AreEqual(6, stringCalculator.Calculate("1\n2,3"));
        }
예제 #27
0
        public void OnehundredAddedThirtyIsOnehundredthirty()
        {
            var stringCalculator = new StringCalculator();

            Assert.AreEqual(130, stringCalculator.Calculate("100,30"));
        }
예제 #28
0
        public void TwoNumbersNewLineDelimited()
        {
            var stringCalculator = new StringCalculator();

            Assert.AreEqual(3, stringCalculator.Calculate("1\n2"));
        }
예제 #29
0
        static void Main(string[] args)
        {
            var        parser    = new InputStringParser();
            IOperation operation = new AdditionOperation();;
            var        argValues = CommandLine.Parser.Default.ParseArguments <CommandLineArgumentOptions>(args);

            if (!argValues.Errors.Any())
            {
                // Values are available here
                if (!string.IsNullOrEmpty(argValues.Value.AlternateDelimiter))
                {
                    parser.Settings.Delimiters.Add(argValues.Value.AlternateDelimiter);
                }
                parser.Settings.AllowNegativeValues = argValues.Value.AllowNegativeValues;
                parser.Settings.MaximumValue        = argValues.Value.MaximumValue;
                if (argValues.Value.Operation == Operations.Add)
                {
                    operation = new AdditionOperation();
                }
                else if (argValues.Value.Operation == Operations.Subtract)
                {
                    operation = new SubtractionOperation();
                }
                else if (argValues.Value.Operation == Operations.Multiply)
                {
                    operation = new MultiplicationOperation();
                }
                else if (argValues.Value.Operation == Operations.Divide)
                {
                    operation = new DivisionOperation();
                }
            }
            else
            {
                return;
            }

            var calculator = new StringCalculator(parser);

            Console.WriteLine(calculator.PromptString);
            Console.WriteLine("Multiline strings may be entered. Input an empty line to terminate the input.  " +
                              "The application will repeat execution until the console is closed or terminated with 'Ctrl+C'");
            while (true)
            {
                try
                {
                    string line;
                    string input = string.Empty;
                    while (!String.IsNullOrWhiteSpace(line = Console.ReadLine()))
                    {
                        input += line + "\n";
                    }
                    var result = calculator.Calculate(input.Trim(), operation).NumberSentence;
                    Console.WriteLine($"Result: {result}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error Calculating Value.  Error details: {ex.Message}");
                }
                Console.WriteLine("\n");
            }
        }
예제 #30
0
        public void SingleNumberReturnsValue()
        {
            var stringCalculator = new StringCalculator();

            Assert.AreEqual(1, stringCalculator.Calculate("1"));
        }