Example #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));
        }
        public void Execute_SwitchesAndReleasesScriptContextIfScriptExecutionThrows()
        {
            Assert.That(ScriptContext.Current, Is.Null);

            const string scriptText = @"
import clr
clr.AddReferenceByPartialName('Remotion.Scripting')
from Remotion.Scripting import *
def Test() :
  raise Exception('IntentionallyRaisedIronPythonException') 
";

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

            try
            {
                script.Execute();
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("IntentionallyRaisedIronPythonException"));
            }

            Assert.That(ScriptContext.Current, Is.Null);
        }
        public void SimplePropertyAccess_GetCustomMember_FixedAttributeProxy()
        {
            const string scriptFunctionSourceCode = @"
import clr
def PropertyPathAccess(cascade) :
  return cascade.GetName()
";

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

            var attributeNameProxy = _scriptContext.GetAttributeProxy(cascade, "GetName");
            var cascadeGetCustomMemberReturnsFixedAttributeProxy = new CascadeGetCustomMemberReturnsFixedAttributeProxy(numberChildren, attributeNameProxy);

            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"
                );

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

            ScriptingHelper.ExecuteAndTime("script function", nrLoopsArray, () => propertyPathAccessScript.Execute(cascade));
            ScriptingHelper.ExecuteAndTime("script function (FixedAttributeProxy)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeGetCustomMemberReturnsFixedAttributeProxy));
        }
        public void SimplePropertyAccess_GetCustomMember2()
        {
            const string scriptFunctionSourceCode = @"
import clr
def PropertyPathAccess(cascade) :
  return cascade.Child.Child.Child.Child.Child.Child.Child.Child.Child.Name
";

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

            var privateScriptEnvironment = ScriptEnvironment.Create();

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

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

            var nrLoopsArray = new[] { 1, 1, 100000 };

            ScriptingHelper.ExecuteAndTime("SimplePropertyAccess_GetCustomMember2 (No StableBinding)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascade));
            ScriptingHelper.ExecuteAndTime("SimplePropertyAccess_GetCustomMember2 (StableBinding from Mixin)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeStableBindingFromMixin));
            ScriptingHelper.ExecuteAndTime("SimplePropertyAccess_GetCustomMember2 (StableBinding)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeStableBinding));
        }
        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);
        }
        public void LongPropertyPathAccess_StableBinding()
        {
            const string scriptFunctionSourceCode =
                @"
import clr
def PropertyPathAccess(cascade) :
  if cascade.GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetName() == 'C0' :
    return cascade.GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetName()
  return 'FAILED'
";

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

            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"
                );

            //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));
        }
        public void SimplePropertyAccess_GetCustomMember()
        {
            const string scriptFunctionSourceCode = @"
import clr
def PropertyPathAccess(cascade) :
  return cascade.Child.Child.Child.Child.Child.Child.Child.Child.Child.Name
";

            const int numberChildren = 10;
            var       cascadeWithoutStableBinding = new Cascade(numberChildren);
            var       cascadeStableBinding        = new CascadeStableBinding(numberChildren);

            var privateScriptEnvironment = ScriptEnvironment.Create();

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

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

            //var nrLoopsArray = new[] { 1, 1, 10000 };
            var nrLoopsArray = new[] { 1, 1, 100000 };

            // Warm up
            ScriptingHelper.ExecuteAndTime(nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeStableBinding)).Last();

            double timingStableBinding        = ScriptingHelper.ExecuteAndTime(nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeStableBinding)).Last();
            double timingWithoutStableBinding = ScriptingHelper.ExecuteAndTime(nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeWithoutStableBinding)).Last();

            //To.ConsoleLine.e (() => timingStableBinding).e (() => timingWithoutStableBinding);
            //To.ConsoleLine.e ("timingStableBinding / timingWithoutStableBinding = ", timingStableBinding / timingWithoutStableBinding);

            Assert.That(timingStableBinding / timingWithoutStableBinding, Is.LessThan(7.0));
        }
        public void OnlyKnownInterface_PublicProperty()
        {
            const string scriptFunctionSourceCode = @"
import clr

def Dir(x) :
  return dir(x)

def PropertyPathAccess(cascade) :
  # return cascade.Child.Child.Child.Child.Child.Child.Child.Child.Child.Name
  return cascade.Child.Name
";

            const int numberChildren       = 10;
            var       cascadeStableBinding = new StableBindingProxyProviderPerformanceTests.CascadeStableBinding(numberChildren);

            var privateScriptEnvironment = ScriptEnvironment.Create();

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

            //var dirScript = new ScriptFunction<object, object> (
            //  _scriptContext, ScriptLanguageType.Python,
            //  scriptFunctionSourceCode, privateScriptEnvironment, "Dir"
            //);
            //To.ConsoleLine.e (dirScript.Execute (cascadeStableBinding)).nl ().e (dirScript.Execute (cascadeStableBinding.Child)).nl ().e (dirScript.Execute (cascadeStableBinding.Child.Child));

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


            propertyPathAccessScript.Execute(cascadeStableBinding);
        }
Example #9
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);
        }
        public void SetVariable()
        {
            var scriptEnvironment = ScriptEnvironment.Create();
            var doc = new Document("ScriptEnvironmentTest_SetVariable");

            scriptEnvironment.SetVariable("ScriptEnvironmentTest_SetVariable", doc);

            Assert.That(scriptEnvironment.ScriptScope.GetVariable("ScriptEnvironmentTest_SetVariable"), Is.SameAs(doc));
        }
        public void Import()
        {
            var scriptEnvironment = ScriptEnvironment.Create();

            scriptEnvironment.Import("Remotion.Scripting", "Remotion.Scripting", "ScriptEnvironment", "ScriptContext");

            Assert.That(scriptEnvironment.ScriptScope.GetVariable("ScriptEnvironment"), Is.Not.Null);
            Assert.That(scriptEnvironment.ScriptScope.GetVariable("ScriptContext"), Is.Not.Null);
        }
        public void Import_StrongName()
        {
            var scriptEnvironment = ScriptEnvironment.Create();

            scriptEnvironment.Import(typeof(ScriptEnvironment).Assembly.FullName, "Remotion.Scripting", "ScriptEnvironment", "ScriptContext");

            Assert.That(scriptEnvironment.ScriptScope.GetVariable("ScriptEnvironment"), Is.Not.Null);
            Assert.That(scriptEnvironment.ScriptScope.GetVariable("ScriptContext"), Is.Not.Null);
        }
        public void Create()
        {
            var scriptEnvironment = ScriptEnvironment.Create();

            Assert.That(scriptEnvironment.ScriptScope, Is.Not.Null);

            var scriptEnvironment2 = ScriptEnvironment.Create();

            Assert.That(scriptEnvironment2.ScriptScope, Is.Not.Null);

            Assert.That(scriptEnvironment.ScriptScope, Is.Not.SameAs(scriptEnvironment2.ScriptScope));
        }
Example #14
0
        public void Ctor_NumberTemplateArguments_1()
        {
            const string scriptText =
                @"def Test(s) :
  return 'Test: ' + s";

            //ScriptScope scriptScope = ScriptingHelper.CreateScriptScope (ScriptLanguageType.Python);
            var scriptScope = ScriptEnvironment.Create();
            var script      = new ScriptFunction <string, string> (ScriptContextObjectMother.CreateTestScriptContext(), ScriptLanguageType.Python, scriptText, scriptScope, "Test");

            Assert.That(script.Execute("works"), Is.EqualTo("Test: works"));
        }
Example #15
0
        public void Ctor_NumberTemplateArguments_9()
        {
            const string scriptText =
                @"def Test(s1,s2,s3,s4,s5,s6,s7,s8,s9) :
  return 'Test: '+s1+s2+s3+s4+s5+s6+s7+s8+s9";

            var scriptEnvironment = ScriptEnvironment.Create();
            var script            = new ScriptFunction <string, string, string, string, string, string, string, string, string, string> (
                ScriptContextObjectMother.CreateTestScriptContext(), ScriptLanguageType.Python, scriptText, scriptEnvironment, "Test");

            Assert.That(script.Execute("1", "2", "3", "4", "5", "6", "7", "8", "9"), Is.EqualTo("Test: 123456789"));
        }
Example #16
0
        public void Ctor_NumberTemplateArguments_2()
        {
            const string scriptText =
                @"def Test(s0,s1) :
  return 'Test: ' + s0 + ' ' + s1";

            var scriptEnvironment = ScriptEnvironment.Create();
            var script            = new ScriptFunction <string, string, string> (ScriptContextObjectMother.CreateTestScriptContext(),
                                                                                 ScriptLanguageType.Python, scriptText, scriptEnvironment, "Test");

            Assert.That(script.Execute("really", "works"), Is.EqualTo("Test: really works"));
        }
        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();
        }
        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"));
        }
        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));
        }
        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"));
        }
        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);
        }
        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"));
        }
        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"));
        }
Example #24
0
        public void ScriptExecute_ImportMultipleIntoScriptScope()
        {
            const string scriptText = @"
import clr
clr.AddReferenceByPartialName('Remotion.Scripting.UnitTests')
from Remotion.Scripting.UnitTests import *
def Test() :
  return TestDomain.Document('Knows Document')
";

            ScriptContext scriptContextForScript = ScriptContextObjectMother.CreateTestScriptContext("Execute_ImportIntoScriptScope");
            var           scriptEnvironment      = ScriptEnvironment.Create();
            var           script         = new ScriptFunction <Document> (scriptContextForScript, ScriptLanguageType.Python, scriptText, scriptEnvironment, "Test");
            Document      resultDocument = script.Execute();

            Assert.That(resultDocument.Name, Is.EqualTo("Knows Document"));
        }
        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"));
        }
        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 GetVariable()
        {
            var          scriptEnvironment = ScriptEnvironment.Create();
            const string variableName      = "ScriptEnvironmentTest_GetVariable";

            var variableInvalid = scriptEnvironment.GetVariable <Document> (variableName);

            Assert.That(variableInvalid.IsValid, Is.False);

            var doc = new Document("GetVariable");

            scriptEnvironment.SetVariable(variableName, doc);

            var variableValid = scriptEnvironment.GetVariable <Document> (variableName);

            Assert.That(variableValid.IsValid, Is.True);
            Assert.That(variableValid.Value, Is.SameAs(doc));
        }
Example #28
0
        public void Ctor()
        {
            ScriptContext            scriptContext      = ScriptContextObjectMother.CreateTestScriptContext();
            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;
            const string             scriptFunctionName = "Test";

            const string scriptText =
                @"def Test() :
  return 'CtorTest'";

            var scriptEnvironment = ScriptEnvironment.Create();

            var script = new ScriptFunction <string> (scriptContext, scriptLanguageType, scriptText, scriptEnvironment, scriptFunctionName);

            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("CtorTest"));
        }
Example #29
0
        public void Execute_SwitchesAndReleasesScriptContext()
        {
            Assert.That(ScriptContext.Current, Is.Null);

            const string scriptText = @"
import clr
clr.AddReferenceByPartialName('Remotion.Scripting')
from Remotion.Scripting import *
def Test() :
  return ScriptContext.Current
";

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

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

            Assert.That(ScriptContext.Current, Is.Null);
        }
        public void SimplePropertyAccess_GetCustomMember_AttributeProxyFromMap()
        {
            const string scriptFunctionSourceCode =
                @"
import clr
def PropertyPathAccess(cascade) :
  if cascade.GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetName() == 'C0' :
    return cascade.GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetChild().GetName()
  return 'FAILED'
";

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

            var cascadeGetCustomMemberReturnsAttributeProxyFromMap = new CascadeGetCustomMemberReturnsAttributeProxyFromMap(numberChildren);

            cascadeGetCustomMemberReturnsAttributeProxyFromMap.AddAttributeProxy("GetName", cascade, _scriptContext);
            cascadeGetCustomMemberReturnsAttributeProxyFromMap.AddAttributeProxy("GetChild", 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"
                );

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

            ScriptingHelper.ExecuteAndTime("script function", nrLoopsArray, () => propertyPathAccessScript.Execute(cascade));
            ScriptingHelper.ExecuteAndTime("script function (AttributeProxyFromMap)", nrLoopsArray, () => propertyPathAccessScript.Execute(cascadeGetCustomMemberReturnsAttributeProxyFromMap));
        }