Exemplo n.º 1
0
        public virtual void TestDefaultList()
        {
            IDictionary <string, MethodInfo> functions = JavascriptCompiler.DEFAULT_FUNCTIONS;
            var expr = JavascriptCompiler.Compile("sqrt(20)", functions);

            Assert.AreEqual(Math.Sqrt(20), expr.Evaluate(0, null), DELTA);
        }
Exemplo n.º 2
0
 public virtual void TestEmpty()
 {
     try
     {
         JavascriptCompiler.Compile(string.Empty);
         Assert.Fail();
     }
     catch (Exception)
     {
     }
     // expected exception
     try
     {
         JavascriptCompiler.Compile("()");
         Assert.Fail();
     }
     catch (Exception)
     {
     }
     // expected exception
     try
     {
         JavascriptCompiler.Compile("   \r\n   \n \t");
         Assert.Fail();
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 3
0
        public virtual void TestEmpty()
        {
            try
            {
                JavascriptCompiler.Compile(string.Empty);
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("()");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("   \r\n   \n \t");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }
        }
Exemplo n.º 4
0
        private void AssertEvaluatesTo(string expression, double expected)
        {
            Expression evaluator = JavascriptCompiler.Compile(expression);
            double     actual    = evaluator.Evaluate(0, null);

            Assert.AreEqual(expected, actual, DELTA);
        }
Exemplo n.º 5
0
        public virtual void TestEmpty()
        {
            try
            {
                JavascriptCompiler.Compile(string.Empty);
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("()");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("   \r\n   \n \t");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }
        }
Exemplo n.º 6
0
 public virtual void TestValidCompiles()
 {
     Assert.IsNotNull(JavascriptCompiler.Compile("100"));
     Assert.IsNotNull(JavascriptCompiler.Compile("valid0+100"));
     Assert.IsNotNull(JavascriptCompiler.Compile("valid0+\n100"));
     Assert.IsNotNull(JavascriptCompiler.Compile("logn(2, 20+10-5.0)"));
 }
Exemplo n.º 7
0
        private void AssertEvaluatesTo(string expression, long expected)
        {
            Expression evaluator = JavascriptCompiler.Compile(expression);
            long       actual    = (long)evaluator.Evaluate(0, null);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 8
0
        public virtual void TestNoArgMethod()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = GetType().GetMethod("ZeroArgMethod");
            var expr = JavascriptCompiler.Compile("foo()", functions);

            Assert.AreEqual(5, expr.Evaluate(0, null), DELTA);
        }
Exemplo n.º 9
0
        public virtual void TestThreeArgMethod()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = GetType().GetMethod("ThreeArgMethod", new [] { typeof(double), typeof(double), typeof(double) });
            var expr = JavascriptCompiler.Compile("foo(3, 4, 5)", functions);

            Assert.AreEqual(12, expr.Evaluate(0, null), DELTA);
        }
Exemplo n.º 10
0
        public virtual void TestNamespaces()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo.bar"] = GetType().GetMethod("ZeroArgMethod");
            string source = "foo.bar()";
            var    expr   = JavascriptCompiler.Compile(source, functions);

            Assert.AreEqual(5, expr.Evaluate(0, null), DELTA);
        }
Exemplo n.º 11
0
        public virtual void TestTwoMethods()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = GetType().GetMethod("ZeroArgMethod");
            functions["bar"] = GetType().GetMethod("OneArgMethod", new [] { typeof(double) });
            var expr = JavascriptCompiler.Compile("foo() + bar(3)", functions);

            Assert.AreEqual(11, expr.Evaluate(0, null), DELTA);
        }
Exemplo n.º 12
0
 public virtual void TestNull()
 {
     try
     {
         JavascriptCompiler.Compile(null);
         Assert.Fail();
     }
     catch (ArgumentNullException)
     {
     }
 }
Exemplo n.º 13
0
 public virtual void TestNull()
 {
     try
     {
         JavascriptCompiler.Compile(null);
         Assert.Fail();
     }
     catch (Exception expected) when(expected.IsNullPointerException())
     {
         // expected
     }
 }
Exemplo n.º 14
0
        public virtual void TestInvalidCompiles()
        {
            try
            {
                JavascriptCompiler.Compile("100 100");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }
            try
            {
                JavascriptCompiler.Compile("7*/-8");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("0y1234");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("500EE");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("500.5EE");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                // expected
            }
        }
Exemplo n.º 15
0
        public virtual void TestInvalidCompiles()
        {
            try
            {
                JavascriptCompiler.Compile("100 100");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }
            try
            {
                JavascriptCompiler.Compile("7*/-8");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("0y1234");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("500EE");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }

            try
            {
                JavascriptCompiler.Compile("500.5EE");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                // expected
            }
        }
Exemplo n.º 16
0
        public virtual void TestEmpty()
        {
            IDictionary <string, MethodInfo> functions = Collections.EmptyMap <string, MethodInfo>();

            try
            {
                JavascriptCompiler.Compile("sqrt(20)", functions);
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Assert.IsTrue(e.Message.Contains("Unrecognized method"));
            }
        }
Exemplo n.º 17
0
        public virtual void TestWrongNotStatic()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = GetType().GetMethod("NonStaticMethod");
            try
            {
                JavascriptCompiler.Compile("foo()", functions);
                Fail();
            }
            catch (ArgumentException e)
            {
                IsTrue(e.Message.Contains("is not static"));
            }
        }
Exemplo n.º 18
0
        public virtual void TestWrongReturnType()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = GetType().GetMethod("BogusReturnType");
            try
            {
                JavascriptCompiler.Compile("foo()", functions);
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Assert.IsTrue(e.Message.Contains("does not return a double"));
            }
        }
Exemplo n.º 19
0
        public virtual void TestWrongParameterType()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = GetType().GetMethod("BogusParameterType", new [] { typeof(string) });
            try
            {
                JavascriptCompiler.Compile("foo(2)", functions);
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Assert.IsTrue(e.Message.Contains("must take only double parameters"));
            }
        }
Exemplo n.º 20
0
        public virtual void TestWrongNestedNotPublic()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = typeof(NestedNotPublic).GetMethod("Method");
            try
            {
                JavascriptCompiler.Compile("foo()", functions);
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Assert.IsTrue(e.Message.Contains("is not public"));
            }
        }
Exemplo n.º 21
0
        public virtual void TestWrongNotPublic()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = GetType().GetMethod("NonPublicMethod", BindingFlags.NonPublic | BindingFlags.Static);

            try
            {
                JavascriptCompiler.Compile("foo()", functions);
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Assert.IsTrue(e.Message.Contains("is not public"));
            }
        }
Exemplo n.º 22
0
 public virtual void TestInvalidCompiles()
 {
     try
     {
         JavascriptCompiler.Compile("100 100");
         Fail();
     }
     catch (Exception)
     {
     }
     // expected exception
     try
     {
         JavascriptCompiler.Compile("7*/-8");
         Fail();
     }
     catch (Exception)
     {
     }
     // expected exception
     try
     {
         JavascriptCompiler.Compile("0y1234");
         Fail();
     }
     catch (Exception)
     {
     }
     // expected exception
     try
     {
         JavascriptCompiler.Compile("500EE");
         Fail();
     }
     catch (Exception)
     {
     }
     // expected exception
     try
     {
         JavascriptCompiler.Compile("500.5EE");
         Fail();
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 23
0
        public virtual void TestInvalidNamespaces()
        {
            try
            {
                JavascriptCompiler.Compile("object.0invalid");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                //expected
            }

            try
            {
                JavascriptCompiler.Compile("0.invalid");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                //expected
            }

            try
            {
                JavascriptCompiler.Compile("object..invalid");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                //expected
            }

            try
            {
                JavascriptCompiler.Compile(".invalid");
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                //expected
            }
        }
Exemplo n.º 24
0
        public virtual void TestInvalidNamespaces()
        {
            try
            {
                JavascriptCompiler.Compile("object.0invalid");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                //expected
            }

            try
            {
                JavascriptCompiler.Compile("0.invalid");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                //expected
            }

            try
            {
                JavascriptCompiler.Compile("object..invalid");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                //expected
            }

            try
            {
                JavascriptCompiler.Compile(".invalid");
                Assert.Fail();
            }
            catch (Exception expected) when(expected.IsParseException())
            {
                //expected
            }
        }
Exemplo n.º 25
0
        public virtual void TestWrongArity()
        {
            try
            {
                JavascriptCompiler.Compile("tan()");
                Assert.Fail();
            }
            catch (ArgumentException expected)
            {
                Assert.IsTrue(expected.Message.Contains("arguments for method call"));
            }

            try
            {
                JavascriptCompiler.Compile("tan(1, 1)");
                Assert.Fail();
            }
            catch (ArgumentException expected)
            {
                Assert.IsTrue(expected.Message.Contains("arguments for method call"));
            }
        }
Exemplo n.º 26
0
        public virtual void TestThrowingException()
        {
            IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>();

            functions["foo"] = typeof(StaticThrowingException).GetMethod("Method");
            string source = "3 * foo() / 5";
            var    expr   = JavascriptCompiler.Compile(source, functions);

            try
            {
                expr.Evaluate(0, null);
                Assert.Fail();
            }
            catch (ArithmeticException e)
            {
                Assert.AreEqual(MESSAGE, e.Message);
                StringWriter sw = new StringWriter();
                e.printStackTrace();
                //.NET Port
                Assert.IsTrue(e.StackTrace.Contains("Lucene.Net.Expressions.CompiledExpression.Evaluate(Int32 , FunctionValues[] )"));
            }
        }
Exemplo n.º 27
0
 public virtual void TestInvalidNamespaces()
 {
     try
     {
         JavascriptCompiler.Compile("object.0invalid");
         Fail();
     }
     catch (Exception)
     {
     }
     //expected
     try
     {
         JavascriptCompiler.Compile("0.invalid");
         Fail();
     }
     catch (Exception)
     {
     }
     //expected
     try
     {
         JavascriptCompiler.Compile("object..invalid");
         Fail();
     }
     catch (Exception)
     {
     }
     //expected
     try
     {
         JavascriptCompiler.Compile(".invalid");
         Fail();
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 28
0
 public virtual void TestValidNamespaces()
 {
     Assert.IsNotNull(JavascriptCompiler.Compile("object.valid0"));
     Assert.IsNotNull(JavascriptCompiler.Compile("object0.object1.valid1"));
 }