コード例 #1
0
        public void Test(DekiScriptExpression expr, string resultValue, Type expectedType, bool safe)
        {
            var env = Runtime.CreateEnv();

            env.Vars.Add(DekiScriptEnv.SAFEMODE, DekiScriptExpression.Constant(safe));
            DekiScriptExpression result = Runtime.Evaluate(expr, safe ? DekiScriptEvalMode.EvaluateSafeMode : DekiScriptEvalMode.Evaluate, env);

            Assert.IsAssignableFrom(expectedType, result);
            string value;

            if (result is DekiScriptString)
            {
                value = ((DekiScriptString)result).Value;
            }
            else if (result is DekiScriptXml)
            {
                value = ((DekiScriptXml)result).Value.ToString();
            }
            else
            {
                value = result.ToString();
            }
            Assert.AreEqual(resultValue, value);
        }
コード例 #2
0
        public void Execute(ExecutionPlan plan)
        {
            var service = DreamTestHelper.CreateService(
                _hostinfo,
                "sid://mindtouch.com/2007/12/dekiscript",
                "dekiscript",
                new XDoc("config").Elem("manifest", _manifestUri)
                );

            foreach (var functionName in _manifest["function/name"])
            {
                var name = functionName.AsText;
                _tester.Runtime.RegisterFunction(name, service.AtLocalHost.At(name));
            }
            try {
                var expr = _tester.Parse(plan.Expr);
                var env  = _tester.Runtime.CreateEnv();
                env.Vars.Add(DekiScriptEnv.SAFEMODE, DekiScriptExpression.Constant(plan.Safe));
                DekiScriptExpression result = _tester.Runtime.Evaluate(expr, plan.Safe ? DekiScriptEvalMode.EvaluateSafeMode : DekiScriptEvalMode.Evaluate, env);
                if (plan.TypedVerification != null)
                {
                    Assert.AreEqual(plan.ExpectedType, result.GetType());
                    plan.TypedVerification(result);
                }
                else if (plan.DocVerification != null)
                {
                    if (!(result is DekiScriptXml))
                    {
                        Assert.Fail(string.Format("return type was '{0}' not DekiScriptXml", result.GetType()));
                    }
                    var doc = ((DekiScriptXml)result).Value;
                    plan.DocVerification(doc);
                }
                else if (plan.StringVerification != null)
                {
                    string value;
                    if (result is DekiScriptString)
                    {
                        value = ((DekiScriptString)result).Value;
                    }
                    else if (result is DekiScriptXml)
                    {
                        value = ((DekiScriptXml)result).Value.ToString();
                    }
                    else
                    {
                        value = result.ToString();
                    }
                    plan.StringVerification(value, result.GetType());
                }
                else
                {
                    Assert.Fail("Execution completed without exception");
                }
            } catch (Exception e) {
                if (plan.ExceptionVerification != null)
                {
                    plan.ExceptionVerification(e);
                }
                else
                {
                    throw;
                }
            } finally {
                service.WithPrivateKey().AtLocalHost.Delete();
            }
        }
コード例 #3
0
        public void Dom_partial_evaluation_for_foreach2()
        {
            const string source = "<html xmlns:eval='http://mindtouch.com/2007/dekiscript'><body init='var x = 2'><div foreach='var y in num.series(1, 5)' where='y != x'><eval:expr>y</eval:expr></div></body></html>";
            XDoc         doc    = XDocFactory.From(source, MimeType.XML);

            DekiScriptExpression node = DekiScriptParser.Parse(doc);

            node = _dekiScriptTester.Runtime.Optimize(node, DekiScriptEvalMode.Evaluate, _dekiScriptTester.Runtime.CreateEnv());
            Assert.AreEqual("<html><body><div>1; </div><div>3; </div><div>4; </div><div>5; </div></body></html>", node.ToString());
        }
コード例 #4
0
        public void Dom_evaluation_with_dynamic_content()
        {
            XDoc doc = new XDoc("html").UsePrefix("eval", "http://mindtouch.com/2007/dekiscript")
                       .Start("body")
                       .Start("div").Attr("block", "var x = string.toupper(date.now)")
                       .Elem("eval:expr", "x .. 3; string.nbsp")
                       .End()
                       .End();

            // parse node
            DekiScriptExpression node = DekiScriptParser.Parse(doc);

            Assert.AreEqual("<html><body>(discard (var x = string.toupper(date.now)); <div>(x .. 3; string.nbsp)</div>) !! web.showerror(__error)</body></html> !! web.showerror(__error)", node.ToString());

            // TODO (steveb): disabled the partial evaluation test for now

            // partial evaluation
            //node = node.Optimize(DekiScriptEvalMode.Evaluate, DekiScriptEnv.Create());
            //Assert.AreEqual("<html><body>discard (var x = \"native:///string.toupper\"( \"native:///$date.now\"())); <div>x .. 3; \"�\"; </div>; </body></html>", node.ToString());

            // full evaluation
            //string now = DekiScriptLibrary.DateNow();
            //XDoc value = node.Evaluate(DekiScriptEvalMode.Evaluate, DekiScriptEnv.Create()).AsEmbeddableXml(false);
            //Assert.AreEqual("<html><body><div>" + now.ToUpperInvariant() + "3&nbsp;</div></body></html>", value.ToXHtml());
        }
コード例 #5
0
        public void Dom_evaluation_with_static_content()
        {
            XDoc doc = new XDoc("html").UsePrefix("eval", "http://mindtouch.com/2007/dekiscript")
                       .Start("body")
                       .Start("div").Attr("block", "var x = string.toupper('foo')")
                       .Elem("eval:expr", "x .. 3; string.nbsp")
                       .End()
                       .End();

            // parse node
            DekiScriptExpression node = DekiScriptParser.Parse(doc);

            Assert.AreEqual("<html><body>(discard (var x = string.toupper(\"foo\")); <div>(x .. 3; string.nbsp)</div>) !! web.showerror(__error)</body></html> !! web.showerror(__error)", node.ToString());

            // TODO (steveb): disabled the partial evaluation test for now

            // partial evaluation
            //node = node.Optimize(DekiScriptEvalMode.Evaluate, DekiScriptEnv.Create());
            //Assert.AreEqual("<html><body><div>\"FOO3�\"; </div></body></html>", node.ToString());

            // full evaluation
            //XDoc value = node.Evaluate(DekiScriptEvalMode.Evaluate, DekiScriptEnv.Create()).AsEmbeddableXml(false);
            //Assert.AreEqual("<html><body><div>FOO3&nbsp;</div></body></html>", value.ToXHtml());
        }
コード例 #6
0
        private void Test(string expression)
        {
            DekiScriptExpression expr = DekiScriptParser.Parse(Location.Start, expression);

            Assert.AreEqual(expression, expr.ToString());
        }