Exemplo n.º 1
0
        public void TestZero()
        {
            string numbers = "0";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(0, sc.Add(numbers));
        }
Exemplo n.º 2
0
        public void TestOneNumber()
        {
            string numbers = "5";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(5, sc.Add(numbers));
        }
Exemplo n.º 3
0
        public void TestMultipleDelimitersWithLengthGreaterThanOne()
        {
            string numbers = "//[***][---][%@!]\n 1---2\n3\n4***5%@!6,1001\n1002";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(21, sc.Add(numbers));
        }
        public void Add_WhenEmptyString_ReturnsZero()
        {
            StringCalculator stringCalculator = new StringCalculator();

            var result = stringCalculator.Add("");
            Assert.AreEqual(result, 0);
        }
Exemplo n.º 5
0
        public void TestTwoNumbers()
        {
            string numbers = "1,5";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(6, sc.Add(numbers));
        }
Exemplo n.º 6
0
        public void TestNewLineSeperator()
        {
            string numbers = "1\n2,3\n4";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(10, sc.Add(numbers));
        }
Exemplo n.º 7
0
        public void TestException()
        {
            string numbers = "1000000000,10000000000";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(-1, sc.Add(numbers));
        }
Exemplo n.º 8
0
        public void TestNegativeNumberException()
        {
            string numbers = "1,-2,-3";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(-1, sc.Add(numbers));
        }
Exemplo n.º 9
0
        public void TestDifferentDelimiter()
        {
            string numbers = "//[;]\n1;2\n3\n4;5;6";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(21, sc.Add(numbers));
        }
Exemplo n.º 10
0
        public void TestIgnoreNumberGreaterThan1000()
        {
            string numbers = "//[;]\n1;2\n3\n4;5;6;1001;1002";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            Assert.AreEqual(21, sc.Add(numbers));
        }
Exemplo n.º 11
0
        public void AddReturnZeroWhenStringEmpty()
        {
            var calculator = new StringCalculator();
            var res = calculator.AddString("");

            Assert.AreEqual(0,res);
        }
 public void CustomDemiters()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     Assert.AreEqual(3, stringCalculator.Add("//;\n1;2"));
     Assert.AreEqual(15, stringCalculator.Add("//;\n12;3"));
     Assert.AreEqual(16, stringCalculator.Add("//+\n12+3+1"));
 }
		public int AddTest(string input)
		{
			var stringCalculator = new StringCalculator();
			int result = stringCalculator.Add(input);

			return result;
		}
        public void Add_EmptyString_ReturnsZero()
        {
            StringCalculator calculator = new StringCalculator();

            int total = calculator.Add( "" );

            Assert.AreEqual( 0, total );
        }
        public void Add_TwoNumbers_ReturnsTheTotal()
        {
            StringCalculator calculator = new StringCalculator();

            int total = calculator.Add( "1,2" );

            Assert.AreEqual( 3, total );
        }
        public void Add_SingleNumber_ReturnsTheNumber()
        {
            StringCalculator calculator = new StringCalculator();

            int total = calculator.Add( "1" );

            Assert.AreEqual( 1, total );
        }
        public void Add_CountTheCalls_ValidOutputAndTheCalls(string input, int expected)
        {
            var stringCalculator = new StringCalculator.StringCalculator();
            var actual           = stringCalculator.Add(input);

            Assert.AreEqual(expected, actual);
            Assert.AreEqual(5, stringCalculator.CalledCount);
        }
 public void TestSimpleNumbers()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     Assert.AreEqual(0, stringCalculator.Add(""));
     Assert.AreEqual(1, stringCalculator.Add("1"));
     Assert.AreEqual(2, stringCalculator.Add("2"));
     Assert.AreEqual(21, stringCalculator.Add("21"));
 }
        public void Add_SimpleInputs_AddsNumbers( string numbers, int expectedTotal )
        {
            StringCalculator calculator = new StringCalculator();

            int total = calculator.Add( numbers );

            Assert.AreEqual( expectedTotal, total );
        }
        public void Add_CallsEventOnAdditionComplete(string input, int expected)
        {
            var stringCalculator = new StringCalculator.StringCalculator();

            stringCalculator.AdditionComplete += StringCalculator_AdditionComplete;
            var actual = stringCalculator.Add(input);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 21
0
        public void TestThreeNumbers()
        {
            string numbers = "1,5,6";

            StringCalculator.StringCalculator sc = new StringCalculator.StringCalculator();
            //Assert.AreEqual(-1, sc.Add(numbers));
            // JK 10/9 code exercise 2 - Added support for unknown amount of numbers
            Assert.AreEqual(12, sc.Add(numbers));
        }
        public void Add_NullOrBlankFromValues_ReturnsZero(
            [Values( null, "", " ", "\t", "\n" )] string input
        )
        {
            StringCalculator calculator = new StringCalculator();

            int total = calculator.Add( input );

            Assert.AreEqual( 0, total );
        }
        public void Add_NullOrBlankFromSource_ReturnsZero(
            [ValueSource( "NullOrBlankCases" )] string input
        )
        {
            StringCalculator calculator = new StringCalculator();

            int total = calculator.Add( input );

            Assert.AreEqual( 0, total );
        }
        public void Add_EvenNumbersBetweenTwoAndTen_ReturnsTheNumber(
            [Range( 2, 10, 2 )] int number
        )
        {
            StringCalculator calculator = new StringCalculator();

            string numbers = number.ToString();
            int total = calculator.Add( numbers );

            Assert.AreEqual( number, total );
        }
        public void Add_AnyRandomNumbers_AddsNumbers(
            [ValueSource( "AnyRandomNumber" )] int a,
            [ValueSource( "AnyRandomNumber" )] int b
        )
        {
            StringCalculator calculator = new StringCalculator();

            string numbers = a + "," + b;
            int total = calculator.Add( numbers );

            Assert.AreEqual( a + b, total );
        }
        public void Add_AnyPairBetweenOneAndFive_AddsNumbers(
            [Values( 1, 2, 3, 4, 5 )] int a,
            [Values( 1, 2, 3, 4, 5 )] int b
        )
        {
            StringCalculator calculator = new StringCalculator();

            string numbers = a + "," + b;
            int total = calculator.Add( numbers );

            Assert.AreEqual( a + b, total );
        }
		public void AddToConsoleTest()
		{
			var input = "1\n2,3";

			var textWriter = Substitute.For<TextWriter>();
			Console.SetOut(textWriter);

			var stringCalculator = new StringCalculator();
			stringCalculator.AddToConsole(input);

			textWriter.Received().WriteLine(6);
		}
Exemplo n.º 28
0
        public void TestLargeNumbers()
        {
            // Arrange
            string input            = "1\n2000, 123456789,80,";
            var    expected         = "1+0+0+80+0 = 81";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
 public void TestNegativeNumbers()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     try
     {
         stringCalculator.Add("-1,2");
     }
     catch (ArgumentException exception)
     {
         Assert.AreEqual("negatives not allowed", exception.Message);
         throw;
     }
 }
Exemplo n.º 30
0
        public void TestSingleCharacterDelimiter()
        {
            // Arrange
            string input            = "//;\n2;5";
            var    expected         = "0+2+5 = 7";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void Add_NegativeNumbersInput_ThrowsException(string input, string exceptionMessage)
        {
            var stringCalculator = new StringCalculator.StringCalculator();

            Assert.Throws(Is.TypeOf <ArgumentException>()
                          .And.Message.EqualTo(exceptionMessage),
                          delegate { stringCalculator.Add(input); });

            var ex = Assert.Throws <ArgumentException>(
                delegate { stringCalculator.Add(input); });

            Assert.That(ex.Message, Is.EqualTo(exceptionMessage));
        }
Exemplo n.º 32
0
        public void TestMultipleDelimiters()
        {
            // Arrange
            string input            = "//[**][((]\n11((22**33";
            var    expected         = "11+22+33 = 66";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 33
0
        public void TestHappyPath()
        {
            // Arrange
            var input            = "1,2,3,4,5,6,7,8,9,10,11,12";
            var expected         = "1+2+3+4+5+6+7+8+9+10+11+12 = 78";
            var stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 34
0
        public void TestMultipleDelimitersWithCharacters()
        {
            // Arrange
            string input            = "//[*][!!][r9r]\n11r9r22*33!!44";
            var    expected         = "11+22+33+44 = 110";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 35
0
        public void TestMultipleDelimitersWithLargeNumber()
        {
            // Arrange
            string input            = "//[**][((]\n11((22**33**500000**10";
            var    expected         = "11+22+33+0+10 = 76";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 36
0
        public void TestWhitespaces()
        {
            // Arrange
            string input            = "20     ,   40   ";
            var    expected         = "20+40 = 60";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 37
0
        public void TestOneInput()
        {
            // Arrange
            string input            = "5";
            var    expected         = "5 = 5";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 38
0
        public void TestTwoDelimiters()
        {
            // Arrange
            string input            = "1\n2,3";
            var    expected         = "1+2+3 = 6";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 39
0
        public void TestSingleDelimiterWithEmptyEntry()
        {
            // Arrange
            string input            = "1\n2,";
            var    expected         = "1+2+0 = 3";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 40
0
        public void TestParenthesisMultipleCharacterDelimiter()
        {
            // Arrange
            string input            = "//[((]\n11((22((33";
            var    expected         = "11+22+33 = 66";
            var    stringCalculator = new StringCalculator.StringCalculator(input);

            // Act
            var actual = stringCalculator.CalculateSum();

            // Assert
            Assert.AreEqual(expected, actual);
        }
		public string ExecuteAddTest(string command)
		{
			var textReader = Substitute.For<TextReader>();
			Console.SetIn(textReader);
			var textWriter = Substitute.For<TextWriter>();
			Console.SetOut(textWriter);

			textReader.ReadLine().Returns(command);

			string result = null;
			textWriter
				.When(w => w.WriteLine(Arg.Any<string>()))
				.Do(c => result = c.Arg<string>());

			var stringCalculator = new StringCalculator();
			stringCalculator.ExecuteAdd(Console.ReadLine());

			return result;
		}
Exemplo n.º 42
0
        public void TestExceptionForNegativeNumbers()
        {
            // Arrange
            string input = "-50,100,-100,200";
            var    expectedExceptionMessage = "Unable to process negative numbers: -50, -100";
            var    stringCalculator         = new StringCalculator.StringCalculator(input);

            // Act
            try
            {
                var actual = stringCalculator.CalculateSum();
                // Fail if no exception thrown
                Assert.Fail();
            }
            // Assert
            catch (ArgumentOutOfRangeException ex)
            {
                Assert.IsTrue(ex.Message.Contains(expectedExceptionMessage));
            }
            catch (Exception)
            {
                Assert.Fail();
            }
        }
 public void MoreThan2()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     Assert.AreEqual(3, stringCalculator.Add("1,1,1"));
     Assert.AreEqual(6, stringCalculator.Add("1,2,3"));
 }
 public void IgnoredNumbersHigherThan1000()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     Assert.AreEqual(2, stringCalculator.Add("2,1000"));
 }
 public void CustomDemitersVariousCharacters()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     Assert.AreEqual(3, stringCalculator.Add("//[**]\n1**2"));
 }
		public void ExecuteCommandCyclicTes()
		{
			var textReader = Substitute.For<TextReader>();
			Console.SetIn(textReader);
			var textWriter = Substitute.For<TextWriter>();
			Console.SetOut(textWriter);

			textReader.ReadLine().Returns("scalc '1,2,3");

			textWriter
				.When(t => t.WriteLine("another input please"))
				.Do(c => textReader.ReadLine().Returns(string.Empty));

			var stringCalculator = new StringCalculator();
			stringCalculator.ExecuteAddCyclic();

			textWriter.Received().WriteLine("The result is 6");
			textWriter.Received().WriteLine("another input please");
		}
 private int Add(string input)
 {
     StringCalculator stringCalculator = new StringCalculator();
     int result = stringCalculator.Add(input);
     return result;
 }
Exemplo n.º 48
0
 public void SetUp()
 {
     _calculator = new StringCalculator();
 }
Exemplo n.º 49
0
        static void Main(string[] args)
        {
            StringCalculator c       = new StringCalculator();
            string           numbers = "// \n4 3 8 5";

            Console.WriteLine(c.Add(numbers));
            Console.WriteLine();

            Match match = Regex.Match(numbers, @"\/\/.{1}\n");

            Console.WriteLine(match.Success);

            string numbers2       = "3,4\n3";
            Match  matchDelimiter = Regex.Match(numbers2, @"(?<=\/\/).+(?=\n)");

            Console.WriteLine(matchDelimiter.Success);
            Console.WriteLine(c.Add(numbers2));
            Console.WriteLine();

            string        delimiter     = "[!!!][**]";
            List <string> delimiterList = new List <string>();           //Regex.Split(delimiter, @"(?<=[\[.+\]])").ToList();

            foreach (Match m in Regex.Matches(delimiter, @"\[.+?\]"))
            {
                delimiterList.Add(m.Value);
            }

            Console.WriteLine(delimiterList.Count + " elements in list:");
            foreach (string s in delimiterList)
            {
                Console.WriteLine(s);
            }


            /*
             * int[] arr = { 3, 6, 1, 667, 1615, 33 };
             * int[] arr2 = arr.Select(n => { if (n > 1000) n = 0; return n; }).ToArray();
             * foreach(int i in arr2) {
             *      Console.WriteLine(i);
             * }
             */

            Console.ReadKey();

            /*
             * char delimiter = ';';
             *
             * string numbers = "//;\n2;2;2";
             * //Regex.Split();
             *
             * Match match = Regex.Match(numbers, @"\/\/[^\s]\n");
             * if (match.Success) {
             *      //set delimiter
             *      delimiter = match.Value[2];
             *      numbers = numbers.Substring(4);
             * }
             * string[] arr = numbers.Split(delimiter, '\n');
             * Console.WriteLine(numbers);
             * foreach (string s in arr) {
             *      Console.WriteLine(s);
             * }
             * Console.WriteLine("Printed out.");
             * Console.ReadKey();
             */
        }
 public void TestAdd(int expected, string stringOfSeperatedNumbers, char separator = ',')
 {
     Assert.AreEqual(expected, StringCalculator.Add(stringOfSeperatedNumbers, separator));
 }
        private static void Main(string[] args)
        {
            var result = StringCalculator.Add(null);

            Console.WriteLine(result);
        }
Exemplo n.º 52
0
        public static void CalculateTo(this string input, int expected)
        {
            var item = new StringCalculator();

            Assert.Equal(expected, StringCalculator.Add(input));
        }
Exemplo n.º 53
0
        public void GivenNegNumberErrorMessageShouldContainNegNumber()
        {
            var error = Assert.Throws <ArgumentOutOfRangeException>(() => StringCalculator.Add("-1,2"));

            Assert.Contains("-1", error.Message);
        }
        public void Add_RangeBetweenOneAndFive_AddsNumbers(
            [Range( 1, 5 )] int a,
            [Range( 1, 5 )] int b
        )
        {
            StringCalculator calculator = new StringCalculator();

            string numbers = a + "," + b;
            int total = calculator.Add( numbers );

            Assert.AreEqual( a + b, total );
        }
 public void MultipleDelimiters()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     Assert.AreEqual(10, stringCalculator.Add("//[**][$$]\n1**2$$3,4"));
 }
        public void Add_RandomNumbers_AddsNumbers(
            [Random( 0, 10, 1 )] int a,
            [Random( 0, 10, 1 )] int b
        )
        {
            StringCalculator calculator = new StringCalculator();

            string numbers = a + "," + b;
            int total = calculator.Add( numbers );

            Assert.AreEqual( a + b, total );
        }
        public void Add_RandomNumberBetweenZeroAndTen_ReturnsTheNumber(
            [Random( 0, 10, 1 )] int number
        )
        {
            StringCalculator calculator = new StringCalculator();

            string numbers = number.ToString();
            int total = calculator.Add( numbers );

            Assert.AreEqual( number, total );
        }
        public void Add_SimpleInputs_AddsNumbers(
            [ValueSource( "TestNumbers" )] string numbers,
            [ValueSource( "TestTotals" )] int expectedTotal
        )
        {
            StringCalculator calculator = new StringCalculator();

            int total = calculator.Add( numbers );

            Assert.AreEqual( expectedTotal, total );
        }
 public void TwoNumbers()
 {
     var stringCalculator = new StringCalculator.StringCalculator();
     Assert.AreEqual(2, stringCalculator.Add("1,1"));
     Assert.AreEqual(3, stringCalculator.Add("1,2"));
     Assert.AreEqual(33, stringCalculator.Add("11,22"));
 }
		public void CtorTest()
		{
			var stringCalculator = new StringCalculator();
			stringCalculator.Should().NotBeNull();
		}