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 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 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 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 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 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 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 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());
     }
 }
 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 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 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));
     }
 }