コード例 #1
0
        public void ExpressionScript_HelperFunctions()
        {
            const string expressionScriptSourceCode =
                "IIf (doc.Name.Contains('Rec'), doc.Number, LazyIIf (doc.Number == 0, lambda:-1, lambda:1.0/doc.Number))";
            // TODO: Equivalent to "doc.Number if doc.Name.Contains('Rec') else -1 if doc.Number == 0 else 1.0/doc.Number"?;

            var privateScriptEnvironment = ScriptEnvironment.Create();

            privateScriptEnvironment.ImportClr();
            // Import script helper functions (IIf and LazyIIf)
            privateScriptEnvironment.ImportIifHelperFunctions();

            var checkDocumentExpressionScript =
                new ExpressionScript <float> (_scriptContext, ScriptLanguageType.Python, expressionScriptSourceCode, privateScriptEnvironment);

            var doc = new Document("Receipt", 4);

            privateScriptEnvironment.SetVariable("doc", doc);

            Assert.That(checkDocumentExpressionScript.Execute(), Is.EqualTo(4.0));
            doc.Name = "Document";
            Assert.That(checkDocumentExpressionScript.Execute(), Is.EqualTo(0.25));
            doc.Number = 0;
            Assert.That(checkDocumentExpressionScript.Execute(), Is.EqualTo(-1.0));
        }
コード例 #2
0
        public void EmptyScriptCall()
        {
            const string scriptFunctionSourceCode = @"
def Empty() :
  return None
";

            var privateScriptEnvironment = ScriptEnvironment.Create();

            var emptyScript = new ScriptFunction <Object> (
                _scriptContext,
                ScriptLanguageType.Python,
                scriptFunctionSourceCode,
                privateScriptEnvironment,
                "Empty"
                );

            var emptyExpression = new ExpressionScript <Object> (
                _scriptContext,
                ScriptLanguageType.Python,
                "None",
                privateScriptEnvironment
                );

            //var nrLoopsArray = new[] {1,1,10,100,1000,10000,100000,1000000};
            var nrLoopsArray = new[] { 1, 1, 10, 100, 1000, 10000 };

            ScriptingHelper.ExecuteAndTime("empty script function", nrLoopsArray, emptyScript.Execute);
            ScriptingHelper.ExecuteAndTime("empty expression script", nrLoopsArray, emptyExpression.Execute);
            ScriptingHelper.ExecuteAndTime("empty expression script (uncompiled)", nrLoopsArray, emptyExpression.ExecuteUncompiled);
        }
コード例 #3
0
        public void ExpressionScript_CreateAndUse()
        {
            const string expressionScriptSourceCode = "doc.Name.Contains('Rec') or doc.Number == 123456";

            var doc = new Document("Receipt");

            // Create a separate script environment for the script expression
            var privateScriptEnvironment = ScriptEnvironment.Create();

            // Import the CLR (e.g. string etc)
            privateScriptEnvironment.ImportClr();
            // Set variable doc to a Document instance
            privateScriptEnvironment.SetVariable("doc", doc);

            // Create a script expression which checks the Document object stored in the variable "doc".
            var checkDocumentExpressionScript =
                new ExpressionScript <bool> (_scriptContext, ScriptLanguageType.Python, expressionScriptSourceCode, privateScriptEnvironment);

            Assert.That(checkDocumentExpressionScript.Execute(), Is.True);
            doc.Name = "Record";
            Assert.That(checkDocumentExpressionScript.Execute(), Is.True);
            doc.Number = 123456;
            Assert.That(checkDocumentExpressionScript.Execute(), Is.True);
            doc.Name = "Report";
            Assert.That(checkDocumentExpressionScript.Execute(), Is.True);
            doc.Number = 21;
            Assert.That(checkDocumentExpressionScript.Execute(), Is.False);
        }
コード例 #4
0
        public void NotImportClr()
        {
            var          scriptEnvironment = ScriptEnvironment.Create();
            const string scriptText        = "'ABcd'.Substring(1,2)";
            var          expressionScript  =
                new ExpressionScript <string> (
                    ScriptContextObjectMother.CreateTestScriptContext("NotImportClr"),
                    ScriptLanguageType.Python,
                    scriptText,
                    scriptEnvironment);

            expressionScript.Execute();
        }
コード例 #5
0
        public void ImportIifHelperFunctions_LazyIIf()
        {
            var scriptEnvironment = ScriptEnvironment.Create();

            scriptEnvironment.ImportIifHelperFunctions();
            scriptEnvironment.SetVariable("x", 100000);
            const string scriptText       = "LazyIIf(x > 1000,lambda:'big',lambda:NonExistingSymbol)";
            var          expressionScript =
                new ExpressionScript <string> (ScriptContextObjectMother.CreateTestScriptContext("ImportIifHelperFunctions"), ScriptLanguageType.Python,
                                               scriptText, scriptEnvironment);

            Assert.That(expressionScript.Execute(), Is.EqualTo("big"));
        }
コード例 #6
0
        public void LongPropertyPathAccess_StableBindingSimple()
        {
            const string scriptFunctionSourceCode = @"
import clr
def PropertyPathAccess(cascade) :
  return cascade.GetChild().GetChild().GetName()
";

            const int numberChildren       = 10;
            var       cascade              = new Cascade(numberChildren);
            var       cascadeStableBinding = new CascadeStableBinding(numberChildren);
            //var cascadeStableBinding = ObjectFactory.Create<CascadeStableBinding> (ParamList.Create (numberChildren));
            var cascadeLocalStableBinding = new CascadeLocalStableBinding(numberChildren);

            var cascadeGetCustomMemberReturnsAttributeProxyFromMap = new CascadeGetCustomMemberReturnsAttributeProxyFromMap(numberChildren);

            cascadeGetCustomMemberReturnsAttributeProxyFromMap.AddAttributeProxy("GetChild", cascade, _scriptContext);
            cascadeGetCustomMemberReturnsAttributeProxyFromMap.AddAttributeProxy("GetName", cascade, _scriptContext);


            var privateScriptEnvironment = ScriptEnvironment.Create();

            privateScriptEnvironment.Import(typeof(Cascade).Assembly.GetName().Name, typeof(Cascade).Namespace, typeof(Cascade).Name);

            var propertyPathAccessScript = new ScriptFunction <Cascade, string> (
                _scriptContext,
                ScriptLanguageType.Python,
                scriptFunctionSourceCode,
                privateScriptEnvironment,
                "PropertyPathAccess"
                );


            privateScriptEnvironment.ImportIifHelperFunctions();
            privateScriptEnvironment.SetVariable("GLOBAL_cascade", cascade);
            var expression = new ExpressionScript <Object> (
                _scriptContext,
                ScriptLanguageType.Python,
                "GLOBAL_cascade.GetChild().GetChild().GetName()",
                privateScriptEnvironment
                );


            //var nrLoopsArray = new[] { 1, 1, 10, 100, 1000, 10000, 100000, 1000000 };
            var nrLoopsArray = new[] { 1, 1, 10, 100, 1000, 10000 };

            ScriptingHelper.ExecuteAndTime("script function", nrLoopsArray, () => propertyPathAccessScript.Execute(cascade));
            ScriptingHelper.ExecuteAndTime("script function (stable binding)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeStableBinding));
            ScriptingHelper.ExecuteAndTime("script function (local stable binding)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeLocalStableBinding));
            ScriptingHelper.ExecuteAndTime("script function (from map)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeGetCustomMemberReturnsAttributeProxyFromMap));
        }
コード例 #7
0
        public void LongPropertyPathAccess_DlrVsClr()
        {
            const string scriptFunctionSourceCode =
                @"
import clr
def PropertyPathAccess(cascade) :
  if cascade.Child.Child.Child.Child.Child.Child.Child.Child.Child.Name == 'C0' :
    return cascade.Child.Child.Child.Child.Child.Child.Child.Name
  return 'FAILED'
";

            const string expressionScriptSourceCode =
                "IIf( GLOBAL_cascade.Child.Child.Child.Child.Child.Child.Child.Child.Child.Name == 'C0',GLOBAL_cascade.Child.Child.Child.Child.Child.Child.Child.Name,'FAILED')";


            var cascade = new Cascade(10);

            var privateScriptEnvironment = ScriptEnvironment.Create();

            privateScriptEnvironment.ImportIifHelperFunctions();
            privateScriptEnvironment.SetVariable("GLOBAL_cascade", cascade);

            privateScriptEnvironment.Import(typeof(Cascade).Assembly.GetName().Name, typeof(Cascade).Namespace, typeof(Cascade).Name);

            var propertyPathAccessScript = new ScriptFunction <Cascade, string> (
                _scriptContext,
                ScriptLanguageType.Python,
                scriptFunctionSourceCode,
                privateScriptEnvironment,
                "PropertyPathAccess"
                );

            var propertyPathAccessExpressionScript = new ExpressionScript <string> (
                _scriptContext, ScriptLanguageType.Python, expressionScriptSourceCode, privateScriptEnvironment
                );

            var nrLoopsArray = new[] { 1, 1, 10, 100, 1000, 10000, 100000, 1000000 };

            //var nrLoopsArray = new[] { 1, 1, 10, 100, 1000, 10000};
            ScriptingHelper.ExecuteAndTime("C# method", nrLoopsArray, delegate
            {
                if (cascade.Child.Child.Child.Child.Child.Child.Child.Child.Child.Name == "C0")
                {
                    return(cascade.Child.Child.Child.Child.Child.Child.Child.Name);
                }
                return("FAILED");
            });
            ScriptingHelper.ExecuteAndTime("script function", nrLoopsArray, () => propertyPathAccessScript.Execute(cascade));
            ScriptingHelper.ExecuteAndTime("expression script", nrLoopsArray, propertyPathAccessExpressionScript.Execute);
        }
コード例 #8
0
        public void Execute()
        {
            const string scriptText = "'Document Name: ' + rmDoc.Name";

            var scriptEnvironment = ScriptEnvironment.Create();
            var document          = new Document("Test Doc");

            scriptEnvironment.SetVariable("rmDoc", document);

            var script = new ExpressionScript <string> (ScriptContextObjectMother.CreateTestScriptContext(),
                                                        ScriptLanguageType.Python, scriptText, scriptEnvironment);

            Assert.That(script.Execute(), Is.EqualTo("Document Name: Test Doc"));
        }
コード例 #9
0
        public void ImportClr()
        {
            var scriptEnvironment = ScriptEnvironment.Create();

            scriptEnvironment.ImportClr();

            const string scriptText       = "'ABcd'.Substring(1,2)";
            var          expressionScript = new ExpressionScript <string> (
                ScriptContextObjectMother.CreateTestScriptContext("ImportClr"),
                ScriptLanguageType.Python,
                scriptText,
                scriptEnvironment);

            Assert.That(expressionScript.Execute(), Is.EqualTo("Bc"));
        }
コード例 #10
0
        public void Ctor()
        {
            ScriptContext            scriptContext      = ScriptContextObjectMother.CreateTestScriptContext();
            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;

            const string scriptText = "'ExpressionScriptCtorTest'";

            var scriptEnvironment = ScriptEnvironment.Create();
            var script            = new ExpressionScript <string> (scriptContext, scriptLanguageType, scriptText, scriptEnvironment);

            Assert.That(script.ScriptContext, Is.EqualTo(scriptContext));
            Assert.That(script.ScriptLanguageType, Is.EqualTo(scriptLanguageType));
            Assert.That(script.ScriptText, Is.EqualTo(scriptText));
            Assert.That(script.Execute(), Is.EqualTo("ExpressionScriptCtorTest"));
        }
コード例 #11
0
        public void Execute_ImportedTypeIntoScriptScope()
        {
            const string scriptText = "Document('New ' + rmDoc.Name)";

            var scriptEnvironment = ScriptEnvironment.Create();

            scriptEnvironment.Import("Remotion.Scripting.UnitTests", "Remotion.Scripting.UnitTests.TestDomain", "Document");
            var document = new Document("Test Doc");

            scriptEnvironment.SetVariable("rmDoc", document);

            var script = new ExpressionScript <Document> (ScriptContextObjectMother.CreateTestScriptContext(),
                                                          ScriptLanguageType.Python, scriptText, scriptEnvironment);
            var result = script.Execute();

            Assert.That(result.Name, Is.EqualTo("New Test Doc"));
        }
コード例 #12
0
        public void Execute_SwitchesAndReleasesScriptContext()
        {
            Assert.That(ScriptContext.Current, Is.Null);

            const string scriptText =
                "ScriptContext.Current";

            ScriptContext scriptContextForScript = ScriptContextObjectMother.CreateTestScriptContext("Execute_SwitchesScriptContext_Script");
            var           scriptEnvironment      = ScriptEnvironment.Create();

            scriptEnvironment.Import("Remotion.Scripting", "Remotion.Scripting", "ScriptContext");
            var script = new ExpressionScript <ScriptContext> (scriptContextForScript, ScriptLanguageType.Python,
                                                               scriptText, scriptEnvironment);

            Assert.That(script.Execute(), Is.SameAs(scriptContextForScript));

            Assert.That(ScriptContext.Current, Is.Null);
        }
        public void CompiledVsUncompiled()
        {
            var          nrLoopsArray = new[] { 1, 1, 1000 };
            const string scriptExpressionSourceCode = "GLOBAL_cascade.Child.Child.Child.Child.Child.Child.Child.Child.Child.Name";

            const int numberChildren = 10;
            var       cascade        = new Cascade(numberChildren);

            var privateScriptEnvironment = ScriptEnvironment.Create();

            privateScriptEnvironment.Import(typeof(TestDomain.Cascade).Assembly.GetName().Name, typeof(TestDomain.Cascade).Namespace, typeof(TestDomain.Cascade).Name);
            privateScriptEnvironment.SetVariable("GLOBAL_cascade", cascade);

            ExpressionScript <string> expressionScript = new ExpressionScript <string> (
                _scriptContext, ScriptLanguageType.Python,
                scriptExpressionSourceCode, privateScriptEnvironment
                );

            // Warm up the DLR
            expressionScript.ExecuteUncompiled();
            ScriptingHelper.ExecuteAndTime("CompiledVsUncompiled (compiled)", nrLoopsArray, () => expressionScript.Execute());
            ScriptingHelper.ExecuteAndTime("CompiledVsUncompiled (uncompiled)", nrLoopsArray, () => expressionScript.ExecuteUncompiled());
        }
コード例 #14
0
        public void Execute_SwitchesAndReleasesScriptContextIfScriptExecutionThrows()
        {
            Assert.That(ScriptContext.Current, Is.Null);

            const string scriptText =
                "RaiseCommandNotSupportedInIronPythonExpressioSoUsingUnkownSymbol";

            ScriptContext scriptContextForScript = ScriptContextObjectMother.CreateTestScriptContext("Execute_SwitchesAndReleasesScriptContextIfScriptExecutionThrows");
            var           scriptEnvironment      = ScriptEnvironment.Create();
            var           script = new ExpressionScript <ScriptContext> (scriptContextForScript, ScriptLanguageType.Python,
                                                                         scriptText, scriptEnvironment);

            try
            {
                script.Execute();
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("name 'RaiseCommandNotSupportedInIronPythonExpressioSoUsingUnkownSymbol' is not defined"));
            }

            Assert.That(ScriptContext.Current, Is.Null);
        }