public void ExecuteBasicExpressionTwice_Success()
 {
     using (var expression = new CSharpScript("1 + 1"))
     {
         Assert.AreEqual(2, expression.Execute());
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void TwoBasicExpressionsInOneObject_Success()
 {
     using (var expression = new CSharpScript())
     {
         Assert.AreEqual(0, expression.AddExpression("1 + 1"));
         Assert.AreEqual(1, expression.AddExpression("2 + 2"));
         Assert.AreEqual(2, expression.Execute());
         Assert.AreEqual(4, expression.Execute(1));
     }
 }
 public void AddSameExpressionTwiceAssumeDuplication_Failure()
 {
     using (var expression = new CSharpScript())
     {
         Assert.AreEqual(0, expression.AddExpression("1 + 1"));
         Assert.AreEqual(0, expression.AddExpression(" 1 + 1 "));
         Assert.AreEqual(2, expression.Execute());
         expression.Execute(1); // This will throw the final exception expected by the test
     }
 }
 public void ExpressionWithOneParameterForceRecompilation_Success()
 {
     using (var expression = new CSharpScript("1 + a"))
     {
         expression.AddObjectInScope("a", 1);
         Assert.AreEqual(2, expression.Execute());
         expression.AddObjectInScope("b", 2);
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void ExpressionUsingGlobalToKeepStateAcrossExecutions_Success()
 {
     using (var expression = new CSharpScript())
     {
         Assert.AreEqual(0, expression.AddVoidReturnCodeSnippet("global.a = 0"));
         Assert.AreEqual(1, expression.AddExpression("global.a++"));
         Assert.AreEqual(null, expression.Execute()); // setup the global, runs first expression
         Assert.AreEqual(0, expression.Execute(1));
         Assert.AreEqual(1, expression.Execute(1));
     }
 }
 public void ExpressionReferencingMutableExpandoObject_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         IDictionary <string, object> obj = new ExpandoObject();
         obj.Add("int_Field", 2);
         expression.AddObjectInScope("obj", obj);
         Assert.AreEqual(3, expression.Execute());
         dynamic dynObj = obj;
         dynObj.int_Field = 3;
         Assert.AreEqual(4, expression.Execute());
     }
 }
 public void ExpressionReferencingMutableLocalObject_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         var obj = new TestClass {
             int_Field = 2
         };
         expression.AddObjectInScope("obj", obj);
         Assert.AreEqual(3, expression.Execute());
         obj.int_Field = 5;
         Assert.AreEqual(6, expression.Execute());
     }
 }
 public void ExpressionUsingGlobalPerformance_Success()
 {
     using (var expression = new CSharpScript())
     {
         Assert.AreEqual(0, expression.AddVoidReturnCodeSnippet("global.a = 0"));
         Assert.AreEqual(1, expression.AddExpression("global.a++"));
         Assert.AreEqual(null, expression.Execute()); // setup the global
         var initialTicks = Environment.TickCount;
         for (uint i = 0; i < 1000000; i++)
         {
             Assert.AreEqual(i, expression.Execute(1));
         }
         Assert.Less(Environment.TickCount - initialTicks, 2000); // 1 million executions in less than 2 seconds
     }
 }
 public void RunExpressionTwiceReferencingReplacedLocalObject_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         var obj = new TestClass {
             int_Field = 2
         };
         expression.AddObjectInScope("obj", obj);
         Assert.AreEqual(3, expression.Execute());
         var obj2 = new TestClass {
             int_Field = 3
         };
         expression.ReplaceObjectInScope("obj", obj2);
         Assert.AreEqual(4, expression.Execute());
     }
 }
예제 #10
0
        static void Main(string[] args)
        {
            var expression = new CSharpScript("1 + a.a")
            {
                ExecuteInSeparateAppDomain = true
            };

            // This snippet shows how a class on a assembly in the GAC can be used
            using (expression)
            {
                expression.AddObjectInScope("a", new TestClassInGACAssembly());
                Console.WriteLine(expression.Execute());
            }

            // This snippet shows how building a SovosExpando, in an assembly installed in GAC works fine.
            // You can call all methods dynamically from the C# script
            using (expression = new CSharpScript()
            {
                ExecuteInSeparateAppDomain = true
            })
            {
                var expando = SovosExpandoBuilder.Build();
                expression.AddObjectInScope("sovosExpando", expando);
                expression.AddCodeSnippet(
                    @"var v = sovosExpando.Test; // We read here a property that gets created on-the-fly
            sovosExpando.Test = ""Hola Mundo""; // We enter a value here that will be cleared by ResetTest() call
            sovosExpando.ResetTest(); // We invoke a dynamically created method here
            sovosExpando.Test = v + sovosExpando.Test + ""Hello World""; // We use here the property read on-the-fly and stored in v
            return sovosExpando.Test");
                Console.WriteLine(expression.Execute());
            }
        }
예제 #11
0
        public static void RunScript(CSharpScript s)
        {
            Exception ex = null;

            s.ScriptExecutionFinished += (sender, e) => { ex = e.Value; };
            s.Execute();
            Assert.IsNull(ex);
        }
 public void VoidReturningCodeSnippet_Success()
 {
     using (var expression = new CSharpScript())
     {
         Assert.AreEqual(0, expression.AddVoidReturnCodeSnippet("var i = 1; Console.WriteLine(i)"));
         Assert.AreEqual(null, expression.Execute());
     }
 }
 public void CreateTwoObjectsMaintainingDifferentState_Success()
 {
     using (var script = new CSharpScript())
     {
         script.AddMember(
             @"public void Test(int a) {  
       global.a = a;                            
     }");
         script.AddExpression("global.a");
         var obj_a = script.CreateScriptObject();
         var obj_b = script.CreateScriptObject();
         Assert.AreEqual(null, script.Invoke(obj_a, "Test", new object[] { 5 }));
         Assert.AreEqual(null, script.Invoke(obj_b, "Test", new object[] { 10 }));
         Assert.AreEqual(5, script.Execute(obj_a));
         Assert.AreEqual(10, script.Execute(obj_b));
     }
 }
 public void TwoBasicExpressions_Success()
 {
     using (var expression1 = new CSharpScript("1 + 1"))
     {
         using (var expression2 = new CSharpScript("2 + 2"))
             Assert.AreEqual(6, (int)expression1.Execute() + (int)expression2.Execute());
     }
 }
 public void ExpressionCountPressure_Success()
 {
     for (var i = 1; i < 50; i++)
     {
         using (var expression = new CSharpScript(String.Format("{0} + 1", i)))
             Assert.AreEqual(1 + i, expression.Execute());
     }
 }
 public void BasicExpressionSeparateAppDomain_Success()
 {
     using (var expression = new CSharpScript("1 + 1"))
     {
         expression.ExecuteInSeparateAppDomain = true;
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void CodeSnippet_Success()
 {
     using (var expression = new CSharpScript())
     {
         Assert.AreEqual(0, expression.AddCodeSnippet("var i = 1; return 1 + i"));
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void BasicExpressionSeparateAppDomainShareIntegerObject_Success()
 {
     using (var expression = new CSharpScript("1 + a"))
     {
         expression.ExecuteInSeparateAppDomain = true;
         expression.AddObjectInScope("a", 1);
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void ExpressionWithSameParameterTwice_Failure()
 {
     using (var expression = new CSharpScript("1 + a + a"))
     {
         expression.AddObjectInScope("a", 1);
         expression.AddObjectInScope("a", 3);
         Assert.AreEqual(5, expression.Execute());
     }
 }
 public void ExpressionWithTwoParameters_Success()
 {
     using (var expression = new CSharpScript("1 + a + c"))
     {
         expression.AddObjectInScope("a", 1);
         expression.AddObjectInScope("c", 3);
         Assert.AreEqual(5, expression.Execute());
     }
 }
 public void AddSameExpressionTwice_Success()
 {
     using (var expression = new CSharpScript())
     {
         Assert.AreEqual(0, expression.AddExpression("1 + 1"));
         Assert.AreEqual(0, expression.AddExpression("1 + 1"));
         Assert.AreEqual(2, expression.Execute());
     }
 }
 public void UseExpandoObjectWithDynamicMethod_Success()
 {
     using (var expression = new CSharpScript("\"a\" + a.Test(\"a\")"))
     {
         dynamic expando = new ExpandoObject();
         expando.Test = new Func <string, string>(str => "Hello" + str);
         expression.AddObjectInScope("a", expando);
         Assert.AreEqual("aHelloa", expression.Execute());
     }
 }
 public void ReplacedNonExistingLocalObject_Failure()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         var obj = new TestClass {
             int_Field = 2
         };
         expression.ReplaceObjectInScope("obj", obj);
         Assert.AreEqual(4, expression.Execute());
     }
 }
 public void CreateTwoObjectsWithObjectInScopeResettingIt_Success()
 {
     using (var script = new CSharpScript())
     {
         script.AddMember(
             @"public void Test(int a) {  
       global.a = a;                            
     }");
         script.AddExpression("global.a + i");
         script.AddObjectInScope("i", 1);
         var obj_a = script.CreateScriptObject();
         var obj_b = script.CreateScriptObject();
         Assert.AreEqual(null, script.Invoke(obj_a, "Test", new object[] { 5 }));
         Assert.AreEqual(null, script.Invoke(obj_b, "Test", new object[] { 10 }));
         Assert.AreEqual(6, script.Execute(obj_a));
         Assert.AreEqual(11, script.Execute(obj_b));
         script.ReplaceObjectInScope(obj_a, "i", 2);
         Assert.AreEqual(7, script.Execute(obj_a));
         Assert.AreEqual(11, script.Execute(obj_b));
     }
 }
 public void CallInjectedFunction_Success()
 {
     using (var expression = new CSharpScript())
     {
         expression.AddMember(
             @"private int AddNumbers(int a, int b)
   {
     return a + b; 
   }");
         Assert.AreEqual(0, expression.AddExpression("AddNumbers(1, 2)"));
         Assert.AreEqual(3, expression.Execute());
     }
 }
 public void InvokeVoidMethodWithParamsModifyGlobals_Success()
 {
     using (var expression = new CSharpScript())
     {
         expression.AddMember(
             @"public void Test(int a) {  
       global.a = a;                            
     }");
         expression.AddExpression("global.a");
         Assert.AreEqual(null, expression.Invoke("Test", new object[] { 5 }));
         Assert.AreEqual(5, expression.Execute());
     }
 }
 public void ShortPerformanceTest_Success()
 {
     using (var expression = new CSharpScript("1 + obj.int_Field"))
     {
         var obj = new TestClass();
         expression.AddObjectInScope("obj", obj);
         var initialTicks = Environment.TickCount;
         for (obj.int_Field = 1; obj.int_Field < 1000000; obj.int_Field++)
         {
             Assert.AreEqual(1 + obj.int_Field, expression.Execute());
         }
         Assert.Less(Environment.TickCount - initialTicks, 2000); // 1MM iterations should run in less than 1 second
     }
 }
 public void OneThousendExpressionsInOneObject_Success()
 {
     using (var expression = new CSharpScript())
     {
         for (uint i = 1; i < 1000; i++)
         {
             Assert.AreEqual(i - 1, expression.AddExpression(String.Format("{0} + 1", i)));
         }
         for (uint i = 1; i < 1000; i++)
         {
             Assert.AreEqual(i + 1, expression.Execute(i - 1));
         }
     }
 }
 public void ClassSnippet_Success()
 {
     using (var expression = new CSharpScript())
     {
         expression.AddMember(
             @"private class Tester {
     public static int Test() {
       return 10;
     }
   }");
         Assert.AreEqual(0, expression.AddCodeSnippet("var i = 1; return Tester.Test() + i"));
         Assert.AreEqual(11, expression.Execute());
     }
 }
 public void InvokeMethodWithParamsModifyGlobalsMultiAppDomain_Success()
 {
     using (var expression = new CSharpScript())
     {
         expression.ExecuteInSeparateAppDomain = true;
         expression.AddMember(
             @"public int Test(int a) {  
       global.a = 20;     
       return a;            
     }");
         expression.AddExpression("global.a");
         Assert.AreEqual(5, expression.Invoke("Test", new object[] { 5 }));
         Assert.AreEqual(20, expression.Execute());
     }
 }