private static string BuildParseTree(string exp)
        {
            var             stack  = new Stack <Token>();
            TokenCollection tokens = GetTokens(exp);

            tokens.CreateRPN();
            return(tokens.Calculate());
        }
        public void Calculate_WithoutRPN_ReturnExceptionTest()
        {
            //Arrange
            var                  stringTokens = "2 + 2".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>();

            //Act
            tokens.AddMany(stringTokens);
            //Assert
            Assert.ThrowsException <InvalidOperationException>(() => tokens.Calculate(formulas));
        }
        public void Calculate_DivideByZeroTest()
        {
            //Arrange
            var                  stringTokens = "2 / 0".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>();

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            //Assert
            Assert.ThrowsException <DivideByZeroException>(() => tokens.Calculate(formulas));
        }
        public void CalcuateTest(string expression, double expectedResult)
        {
            //Arrange
            var                  stringTokens = expression.Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>();

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            var result = tokens.Calculate(formulas);

            //Assert
            Assert.AreEqual(expectedResult, result, 0.01);
        }
        public void Calcuate_WithIncorrectListOfFormulaTest()
        {
            //Arrange
            var                  stringTokens = "2 + a * c".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>()
            {
                new ServerFormula {
                    Name = "a", Value = 13
                }
            };

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            //Assert
            Assert.ThrowsException <ArgumentException>(() => tokens.Calculate(formulas));
        }
        public void Calcuate_WithCorrectListOfFormulaTest()
        {
            //Arrange
            var                  stringTokens = "2 + a * 10".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>()
            {
                new ServerFormula {
                    Name = "a", Value = 13
                }
            };

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            var result = tokens.Calculate(formulas);

            //Assert
            Assert.AreEqual(132, result, 0.01);
        }