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 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 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); }
public void ScriptFunction_CreateAndUseScripts_InSameScriptEnvironment() { // Import CLR (for CLR string functionality) _scriptEnvironment.ImportClr(); // Import the Document class into the shared script environment (otherwise we would not be able to call the Document ctors within // the script). _scriptEnvironment.Import("Remotion.Scripting.UnitTests", "Remotion.Scripting.UnitTests.TestDomain", "Document"); const string scriptFunctionSourceCode0 = @" def CreateScriptDocSuccessor() : return Document(scriptDoc.Name,scriptDoc.Number+1) "; const string scriptFunctionSourceCode1 = @" def ModifyDocument(newNamePostfix, addToNumber) : scriptDoc.Name += newNamePostfix scriptDoc.Number += addToNumber return scriptDoc "; // Create a script function which creates a new Document. var createScriptDocSuccessorScript = new ScriptFunction <Document> ( _scriptContext, ScriptLanguageType.Python, scriptFunctionSourceCode0, _scriptEnvironment, "CreateScriptDocSuccessor"); // Create a script function which modifies the passed Document. var modifyDocumentScript = new ScriptFunction <string, int, Document> ( _scriptContext, ScriptLanguageType.Python, scriptFunctionSourceCode1, _scriptEnvironment, "ModifyDocument"); var doc = new Document("invoice", 1); Assert.That(doc.Name, Is.EqualTo("invoice")); Assert.That(doc.Number, Is.EqualTo(1)); _scriptEnvironment.SetVariable("scriptDoc", doc); Document successorDoc = createScriptDocSuccessorScript.Execute(); Document scriptDoc = modifyDocumentScript.Execute(" - processed", 1000); Assert.That(successorDoc.Name, Is.EqualTo("invoice")); Assert.That(successorDoc.Number, Is.EqualTo(2)); Assert.That(scriptDoc.Name, Is.EqualTo("invoice - processed")); Assert.That(scriptDoc.Number, Is.EqualTo(1001)); }
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")); }
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")); }
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 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 ScriptFunction_CreateAndUseScript_WithPrivateScriptEnvironment() { const string scriptFunctionSourceCode = @" import clr def CheckDocument(doc) : return doc.Name.Contains('Rec') or doc.Number == 123456 "; var privateScriptEnvironment = ScriptEnvironment.Create(); // Create a script function called CheckDocument which takes a Document and returns a bool. var checkDocumentScript = new ScriptFunction <Document, bool> ( ScriptContext.GetScriptContext("rubicon.eu.YourModuleName.ScriptingIntegrationTests"), ScriptLanguageType.Python, scriptFunctionSourceCode, privateScriptEnvironment, "CheckDocument"); Assert.That(checkDocumentScript.Execute(new Document("Receipt", 123456)), Is.True); Assert.That(checkDocumentScript.Execute(new Document("XXX", 123456)), Is.True); Assert.That(checkDocumentScript.Execute(new Document("Rec", 0)), Is.True); Assert.That(checkDocumentScript.Execute(new Document("XXX", 0)), Is.False); }
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); }
/// <summary> /// Execute function with or without error MessageBox /// </summary> /// <param name="par"></param> /// <returns>true=ok;false=nok</returns> public Boolean Execute(object[] par) { try { _function.Execute(par); return(true); } catch (Exception e) { if (_isErrorOutput) { MessageBox.Show($"Have you updated the Scripts (File, Update Scripts)??\r\n\r\n{e.ToString()}", $"Error run Script '{_function.FullName}()'"); } return(false); } }
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 override void Execute(params object[] executeArgs) { if (commandArgs.Length > 0) { if (!commandArgs[0].StartsWith("%") && !commandArgs[0].StartsWith("$")) { ScriptFunction func = Context.GetFunction(commandArgs[0]); if (func != null) { func.Execute(executeArgs); } else { GameManager.Instance.log.LogMessage(string.Format("Function `{0}` didn't exist!", commandArgs[0]), LogMessage.LogType.Error); } } } }
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")); }
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 ScriptFunction_CreateAndUse() { const string scriptFunctionSourceCode = @" import clr clr.AddReferenceByPartialName('Remotion.Scripting.UnitTests') from Remotion.Scripting.UnitTests.TestDomain import Document def CreateDocument() : return Document('Here is your document, sir.') "; // Create a script function called "CreateDocument" which returns a Document object. var createDocumentScript = new ScriptFunction <Document> ( _scriptContext, ScriptLanguageType.Python, scriptFunctionSourceCode, _scriptEnvironment, "CreateDocument"); Document resultDocument = createDocumentScript.Execute(); Assert.That(resultDocument.Name, Is.EqualTo("Here is your document, sir.")); }
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)); }
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)); }