public void WhenWrongFunctionExpressionIsPassed_Then_ExceptionShouldBeThrown(string expression)
        {
            var sut = new FunctionOrConstTermParser();

            Assert.Throws <InvalidOperationException>(() =>
            {
                var result = sut.Parse(expression, 0);
            });
        }
        public void WhenExpressionConsistOnlyFromLetters_Then_ConstTermShouldBeReturned()
        {
            var sut = new FunctionOrConstTermParser();

            var result = sut.Parse("pi", 0);

            Assert.True(result.TermType == TermType.Const);
            Assert.True(result.Term == "pi");
        }
Exemple #3
0
        public List <MathParserTermResult> Parse(string expression, bool allowParameters = false)
        {
            var terms = new List <MathParserTermResult>();

            if (String.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentNullException("Expression cannot be empty!");
            }

            var i = 0;

            while (i < expression.Length)
            {
                MathParserTermResult term = null;

                if (Char.IsDigit(expression[i]))
                {
                    term = new NumberTermParser().Parse(expression, i);
                }
                else
                if (OperatorTermParser.IsOperator(expression[i]))
                {
                    term = new OperatorTermParser().Parse(expression, i);
                }
                else
                if (Char.IsLetter(expression[i]))
                {
                    term = new FunctionOrConstTermParser().Parse(expression, i);
                }
                else
                if (SubExpressionTermParser.IsOpenParenthesis(expression[i]))
                {
                    term = new SubExpressionTermParser().Parse(expression, i);
                }
                else
                if (allowParameters && expression[i] == ',')
                {
                    i++;
                    continue;
                }

                if (term != null)
                {
                    i = term.EndPos;

                    terms.Add(term);
                }
                else
                {
                    throw new InvalidOperationException($"Wrong character '{expression[i]}' at [{i}]!");
                }
            }

            return(terms);
        }
        public void WhenExpressionConsistFromLettersAndParenthesis_Then_FunctionTermShouldBeReturned(
            string expression, string expectedFunction, string expectedParameters, int numberOfParams)
        {
            var sut = new FunctionOrConstTermParser();

            var result = sut.Parse(expression, 0) as ComplexMathParserResult;

            Assert.True(result.TermType == TermType.Function);
            Assert.True(result.Term == expectedFunction);
            Assert.True(result.InternalTerm != null);
            Assert.True(result.InternalTerm.Term == expectedParameters);
            Assert.True(result.InternalTerm.TermType == TermType.Expression);
            Assert.True(result.NumberOfParams == numberOfParams);
        }