public void ComputedUnaryRandomFunctionCallExpression()
        {
            var r     = new Random();
            var limit = r.Next();

            using (var service = new ExpressionParsingService())
            {
                using (ComputedExpression del = service.Interpret("random(x)"))
                {
                    if (del == null)
                    {
                        throw new InvalidOperationException("No computed expression was generated!");
                    }

                    object result;
                    try
                    {
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation - Not consequential
                        result = del.Compute(limit);
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException(
                                  "The method should not have thrown an exception, but it did.",
                                  ex);
                    }

                    _ = Assert.IsType <double>(result);

                    Assert.True((double)result < limit);
                }
            }
        }
        public void ComputedRandomIntNonaryFunctionCallExpression()
        {
            using (var service = new ExpressionParsingService())
            {
                using (ComputedExpression del = service.Interpret("randomint()"))
                {
                    if (del == null)
                    {
                        throw new InvalidOperationException("No computed expression was generated!");
                    }

                    object result;
                    try
                    {
                        result = del.Compute();
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException(
                                  "The method should not have thrown an exception, but it did.",
                                  ex);
                    }

                    _ = Assert.IsType <long>(result);
                }
            }
        }
        public void GetAvailableFunctionsTest()
        {
            using (var service = new ExpressionParsingService())
            {
                string[] functions = service.GetRegisteredFunctions();

                Assert.True(functions.Length > 0);
            }
        }
Пример #4
0
        public void Test4()
        {
            using var eps = new ExpressionParsingService();

            eps.RegisterFunctionsAssembly(typeof(ExternalExtractorUnitTests).GetTypeInfo().Assembly);

            using ComputedExpression interpreted = eps.Interpret("substring(\"alabalaportocala\",bumbly dumb)");

            Assert.NotNull(interpreted);
            Assert.True(interpreted.RecognizedCorrectly);
            Assert.True(interpreted.IsConstant);
            Assert.Equal(
                "abalaportocala",
                interpreted.Compute());
        }
Пример #5
0
        public void Test2()
        {
            using var eps = new ExpressionParsingService();

            eps.RegisterFunctionsAssembly(typeof(ExternalExtractorUnitTests).GetTypeInfo().Assembly);

            using ComputedExpression interpreted = eps.Interpret("\"I am silly very much\"");

            Assert.NotNull(interpreted);
            Assert.True(interpreted.RecognizedCorrectly);
            Assert.True(interpreted.IsConstant);
            Assert.Equal(
                "I am stupid very much",
                interpreted.Compute());
        }
Пример #6
0
        public void ComputedExpressionWithParameters(
            string expression,
            object[] parameters,
            object expectedResult)
        {
            using var service = new ExpressionParsingService();

            ComputedExpression del;

            try
            {
                del = service.Interpret(expression);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                          "The generation process should not have thrown an exception, but it did.",
                          ex);
            }

            if (del == null)
            {
                throw new InvalidOperationException("No computed expression was generated!");
            }

            try
            {
                object result;
                try
                {
                    result = del.Compute(parameters);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException(
                              "The method should not have thrown an exception, but it did.",
                              ex);
                }

                Assert.Equal(
                    expectedResult,
                    result);
            }
            finally
            {
                del.Dispose();
            }
        }
        public void ComputedBinaryRandomIntFunctionCallExpression()
        {
            var r = new Random();
            int dingLimit;

            do
            {
                dingLimit = r.Next();
            }while (dingLimit <= 5);

            var highLimit = r.Next(
                dingLimit,
                int.MaxValue);
            var lowLimit = r.Next(dingLimit);

            using (var service = new ExpressionParsingService())
            {
                using (ComputedExpression del = service.Interpret("randomint(x, y)"))
                {
                    if (del == null)
                    {
                        throw new InvalidOperationException("No computed expression was generated!");
                    }

                    object result;
                    try
                    {
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation - Not consequential
                        result = del.Compute(
                            lowLimit,
                            highLimit);
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException(
                                  "The method should not have thrown an exception, but it did.",
                                  ex);
                    }

                    _ = Assert.IsType <long>(result);

                    Assert.True((long)result < highLimit);
                    Assert.True((long)result >= lowLimit);
                }
            }
        }
Пример #8
0
        public void Test1()
        {
            using var eps = new ExpressionParsingService();

            eps.RegisterFunctionsAssembly(typeof(ExternalExtractorUnitTests).GetTypeInfo().Assembly);

            using ComputedExpression interpreted = eps.Interpret("1+silly+3");

            Assert.NotNull(interpreted);
            Assert.True(interpreted.RecognizedCorrectly);
            Assert.Contains(
                interpreted.GetParameterNames(),
                p => p == "stupid");
            Assert.DoesNotContain(
                interpreted.GetParameterNames(),
                p => p == "silly");
        }
        public void Test1()
        {
            const string expression = "true&true|false&false";

            object result1, result2;

            using (var service = new ExpressionParsingService())
            {
                using (ComputedExpression del = service.Interpret(expression))
                {
                    if (del == null)
                    {
                        throw new InvalidOperationException("No computed expression was generated!");
                    }

                    result1 = del.Compute();
                }
            }

            using (var service = new ExpressionParsingService(new MathDefinition
            {
                Parentheses = ("(", ")"),
                SpecialSymbolIndicators = ("[", "]"),
                StringIndicator = "\"",
                ParameterSeparator = ",",
                AddSymbol = "+",
                AndSymbol = "&",
                DivideSymbol = "/",
                NotEqualsSymbol = "!=",
                EqualsSymbol = "=",
                MultiplySymbol = "*",
                NotSymbol = "!",
                OrSymbol = "|",
                PowerSymbol = "^",
                SubtractSymbol = "-",
                XorSymbol = "#",
                GreaterThanOrEqualSymbol = ">=",
                GreaterThanSymbol = ">",
                LessThanOrEqualSymbol = "<=",
                LessThanSymbol = "<",
                RightShiftSymbol = ">>",
                LeftShiftSymbol = "<<",
                OperatorPrecedenceStyle = OperatorPrecedenceStyle.CStyle
            }))
 public CachedExpressionProviderFixture()
 {
     CachedService = new CachedExpressionParsingService();
     Service       = new ExpressionParsingService();
 }