public void TryGetVariableAsHandle_MultipleCases() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data string[] key = { "test1", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; // case 1 ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // out value; int value = -1; ObjectHandle objHVal = new ObjectHandle(value); // Case 1 Assert.IsTrue(TestScope.TryGetVariableHandle(key[0], out objHVal)); Assert.IsTrue((int)(objHVal.Unwrap()) == tExpectedVal1); // reset value = -1; objHVal = new ObjectHandle(value); // Case 2 Assert.IsFalse(TestScope.TryGetVariableHandle(key[1], out objHVal)); Assert.IsNull(objHVal); }
public void TryGetVariable_multipleCases() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data string[] key = { "test1", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; // case 1 //int tExpectedVal2 = 2222; //global[key[1]] = 2222; // case 2 ScriptScope testScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // out value; int value = -1; // Case 1 Assert.IsTrue(testScope.TryGetVariable(key[0], out value)); Assert.IsTrue(value == tExpectedVal1); // Not null! int tExpectedRtnValueOfNonExistentVar = 0; // reset value = -1; // Case 2 Assert.IsFalse(testScope.TryGetVariable(key[1], out value)); Assert.IsTrue(value == tExpectedRtnValueOfNonExistentVar); }
public void CreateScope_UsingValidScopeDic() { // Create a Scope Dictionary ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data global["One"] = 1; global["Two"] = 2; global["Three"] = 3; // Greate a new Scope and or get default scope... ScriptScope testScope = _testEng.Runtime.CreateScope(new ObjectDictionaryExpando(global)); foreach (string str in testScope.GetVariableNames()) { Assert.AreEqual(global[str], testScope.GetVariable(str)); } // Now verify that changes to the TestScope are reflected in the global scope. string updateTestKey = "Four"; testScope.SetVariable(updateTestKey, 4); Assert.AreEqual(global[updateTestKey], testScope.GetVariable(updateTestKey)); }
public void GetVariableGenericT_ExistingVarGenericTypeCheck() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data string[] key = { "test1", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; // case 1 int tExpectedVal2 = 2222; global[key[1]] = tExpectedVal2; ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // ScriptScope TestNullScope = null; object obj = new object(); // What is ObjectHandle used for? ObjectHandle oResult = new ObjectHandle(obj); int val = TestScope.GetVariable <int>(key[0]); // Check return type and value Assert.IsTrue(val.GetType() == tExpectedVal1.GetType()); Assert.IsTrue(val == tExpectedVal1); }
public void GetVariable_EmptyString() { // Create a Scope Dictionary ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data global["Test1"] = 1111; global["Test2"] = 2222; global["Test3"] = 3333; StringBuilder TestName = new StringBuilder(); // TestName = String.Empty; string tName = TestName.ToString(); // Test value object testVal = 7777; global[tName] = testVal; string msg = string.Format("Make sure our test data is a valid empty string '{0}", tName); Assert.IsTrue(string.IsNullOrEmpty(tName), msg); // Setup up the default scope. ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // Now try to retrieve empty variable. object expVal = TestScope.GetVariable(tName); msg = string.Format("Trying to retrieve the empty string from the local scope"); Assert.IsTrue(expVal.Equals(testVal), msg); }
public void ContainVariable_NameLengthGT256WithNonExistingName() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data // string > 20 string[] key = { "test123456789ABCDEFGHIJ", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; int tExpectedVal2 = 2222; global[key[1]] = tExpectedVal2; int BigValueToTestBeyond = 256; StringBuilder longStrTest = new StringBuilder("test_var_"); for (int i = 0; i < BigValueToTestBeyond + 1; i++) { longStrTest.Append(string.Format("{0}", i)); } string tLongKey = longStrTest.ToString(); // Validate that this key is longer then 256 Assert.IsTrue(tLongKey.Length > BigValueToTestBeyond); // Get default scope with set values ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // Lookup Long key Assert.IsFalse(TestScope.ContainsVariable(tLongKey)); }
public void TryGetVariableGenericT_MultipleCases() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data string[] key = { "test1", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; // case 1 // Get default scope with set values ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // out value; int value = -1; ObjectHandle objHVal = new ObjectHandle(value); // Case 1 //int outV; Assert.IsTrue(TestScope.TryGetVariable <int>(key[0], out value)); Assert.IsTrue(value == tExpectedVal1); // reset value = -1; // Case 2 - Not sure about this Assert.IsFalse(TestScope.TryGetVariable <int>(key[1], out value)); Assert.IsTrue(value == 0); //@TODO - Add Case 3 - HOW CAN I TEST THIS CASE need more info. }
public void Execute_AccessValidVarInScope() { // Setup tests data ScriptScopeDictionary env = new ScriptScopeDictionary(); env["test1"] = -10; ValidateExecute(env, "abs(test1)", 10); }
private void ValidateExecute(ScriptScopeDictionary env, string expression, int expResult) { ScriptSource source = _testEng.CreateScriptSourceFromString(expression, SourceCodeKind.Expression); // Create scope ScriptScope scope = _testEng.CreateScope(new ObjectDictionaryExpando(env)); // Execute source using scope and capture result object testResult = source.Execute(scope); // test result against expected result Assert.AreEqual(expResult, (int)testResult); }
public void CreateScope_InvokeMethodTwiceWithSameGlobals() { ScriptScopeDictionary dict = new ScriptScopeDictionary(); ScriptRuntime runTimeOne = CreateRuntime(); ScriptRuntime runTimeTwo = CreateRuntime(); dict["foo_1"] = 1000; ScriptScope scope1 = runTimeOne.CreateScope(new ObjectDictionaryExpando(dict)); ScriptScope scope2 = runTimeTwo.CreateScope(new ObjectDictionaryExpando(dict)); Assert.IsTrue(scope1.IsSimilarButNotSameAs(scope2)); }
public void GetVariableAsHandle_NullName() { // Create a valid Scope Dictionary ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data global["Test1"] = 1111; // Create the Scope ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // Create a null string to lookup string tNullName = null; ObjectHandle ObjHdl = TestScope.GetVariableHandle(tNullName); }
public void CreateScope_InvokeMethodTwiceWithSameGlobals() { ScriptScopeDictionary dict = new ScriptScopeDictionary(); ScriptRuntime runTimeOne = CreateRuntime(); ScriptRuntime runTimeTwo = CreateRuntime(); dict["foo_1"] = 1000; ScriptScope scope1 = runTimeOne.CreateScope(new ObjectDictionaryExpando (dict)); ScriptScope scope2 = runTimeTwo.CreateScope(new ObjectDictionaryExpando (dict)); Assert.IsTrue( scope1.IsSimilarButNotSameAs(scope2)); }
public void Globals_TryToAccessNamesSet() { ScriptScopeDictionary dict = new ScriptScopeDictionary(); string key = "foo"; dict[key] = 1; _runtime.Globals = _testEng.CreateScope(new ObjectDictionaryExpando(dict)); Assert.AreEqual(1, _runtime.Globals.GetVariable(key)); dict[key] = 2; _runtime.Globals = _testEng.CreateScope(new ObjectDictionaryExpando(dict)); Assert.AreEqual(2, _runtime.Globals.GetVariable(key)); }
public void SetVariable_ScopeBoundToEngineSetExistingName() { ScriptScopeDictionary global = new ScriptScopeDictionary(); string[] key = { "Test1", "test2", "TEST3" }; global[key[0]] = 1111; global[key[1]] = 2222; global[key[2]] = 3333; object tNewValue = 4444; _testEng.CreateScope(new ObjectDictionaryExpando(global)).SetVariable(key[2], tNewValue); Assert.IsTrue(global[key[2]].Equals(tNewValue)); }
public void ContainVariable_EmptyStringValue() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data // string > 20 string[] key = { "test1", "", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; int tExpectedVal2 = 2222; global[key[1]] = tExpectedVal2; // Get default scope with set values ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // Test for empty var! Assert.IsTrue(TestScope.ContainsVariable(key[1])); }
public void CreateScope_LinkBetweenReturnedScopeAndArg() { ScriptScopeDictionary dict = new ScriptScopeDictionary(); ScriptScope attachedScope = _testEng.CreateScope(new ObjectDictionaryExpando(dict)); string key = "how"; string editDict = "through dict", editScope = "through scope"; //edit a) dictionary directly b)through the scope dict[key] = editDict; attachedScope.SetVariable(key, editScope); Assert.AreEqual(dict[key], editScope); dict[key] = editDict; Assert.AreEqual(editDict, attachedScope.GetVariable(key)); }
public void SetVariable_ScopeBoundToEngineSetNewName() { ScriptScopeDictionary global = new ScriptScopeDictionary(); string[] key = { "Test1", "test2", "TEST3" }; global[key[0]] = 1111; global[key[1]] = 2222; global[key[2]] = 3333; string newName = "test4"; object newValue = 4444; _testEng.CreateScope(new ObjectDictionaryExpando(global)).SetVariable(newName, newValue); // Verify new Variable has been created. Assert.IsTrue(global.Contains(newName)); Assert.IsTrue(global[newName].Equals(newValue)); }
public void CreateScope_PassValidScope() { ScriptScopeDictionary dict = new ScriptScopeDictionary(); dict["foo_1"] = 1000; dict["foo_2"] = 2000; ScriptScope linkedScope = _runtime.CreateScope(new ObjectDictionaryExpando(dict)); linkedScope.SetVariable("foo_3", 3000); linkedScope.SetVariable("foo_4", 4000); Assert.IsTrue(linkedScope.IsValid()); Assert.IsTrue(dict["foo_3"].Equals(3000)); Assert.IsTrue(dict["foo_4"].Equals(4000)); Assert.IsTrue(linkedScope.ContainsVariable("foo_1")); Assert.IsTrue(linkedScope.ContainsVariable("foo_2")); }
public void ContainVariable_NameLengthTwentyWithExistingName() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data // string > 20 string[] key = { "test123456789ABCDEFGHIJ", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; int tExpectedVal2 = 2222; global[key[1]] = tExpectedVal2; // Get default scope with set values ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); Assert.IsTrue(TestScope.ContainsVariable(key[0])); }
public void GetVariableAsHandle_LocalEngineExistingName() { // Create a valid Scope Dictionary ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data global["Test1"] = 1111; // Create the Scope ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // Create a null string to lookup string tKey = "Test1"; ObjectHandle ObjHdl = TestScope.GetVariableHandle(tKey); // Verify that we we can access "Test1" string msg = string.Format("The remote lookup scope is {0}", ObjHdl); // Unwrap the value in the ObjectHandle object tObj = ObjHdl.Unwrap(); // Validate they are equal Assert.IsTrue(tObj == global[tKey], msg); }
public void Execute_AccessVarPreDefinedInScope() { ScriptScopeDictionary env = new ScriptScopeDictionary(); env["k"] = (object)1; ScriptSource source = _testEng.CreateScriptSourceFromString(_codeSnippets[CodeType.OneLineAssignmentStatement] + "\nw=k+x", SourceCodeKind.Statements); MyErrorListener errorListen = new MyErrorListener(); CompiledCode compiled = source.Compile(errorListen); Assert.IsTrue(errorListen._message == null); ScriptScope scope = _testEng.CreateScope(new ObjectDictionaryExpando(env)); compiled.Execute(scope); object result = scope.GetVariable("x"); Assert.AreEqual(result, (object)3); result = scope.GetVariable("w"); Assert.AreEqual(result, (object)4); }
public void TryGetVariableGenericT_MultipleCases() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data string[] key = { "test1", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; // case 1 // Get default scope with set values ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // out value; int value = -1; ObjectHandle objHVal = new ObjectHandle(value); // Case 1 //int outV; Assert.IsTrue(TestScope.TryGetVariable<int>(key[0], out value)); Assert.IsTrue(value == tExpectedVal1); // reset value = -1; // Case 2 - Not sure about this Assert.IsFalse(TestScope.TryGetVariable<int>(key[1], out value)); Assert.IsTrue(value == 0); //@TODO - Add Case 3 - HOW CAN I TEST THIS CASE need more info. }
public void GetVariableGenericT_ExistingVarGenericTypeCheck() { ScriptScopeDictionary global = new ScriptScopeDictionary(); // Populate with some test data string[] key = { "test1", "test2", "test3" }; int tExpectedVal1 = 1111; global[key[0]] = tExpectedVal1; // case 1 int tExpectedVal2 = 2222; global[key[1]] = tExpectedVal2; ScriptScope TestScope = _testEng.CreateScope(new ObjectDictionaryExpando(global)); // ScriptScope TestNullScope = null; object obj = new object(); // What is ObjectHandle used for? ObjectHandle oResult = new ObjectHandle(obj); int val = TestScope.GetVariable<int>(key[0]); // Check return type and value Assert.IsTrue(val.GetType() == tExpectedVal1.GetType()); Assert.IsTrue(val == tExpectedVal1); }
public void Globals_TryToAccessNamesSet(){ ScriptScopeDictionary dict = new ScriptScopeDictionary(); string key = "foo"; dict[key] = 1; _runtime.Globals = _testEng.CreateScope(new ObjectDictionaryExpando(dict)); Assert.AreEqual(1, _runtime.Globals.GetVariable( key)); dict[key] = 2; _runtime.Globals = _testEng.CreateScope(new ObjectDictionaryExpando(dict)); Assert.AreEqual(2, _runtime.Globals.GetVariable(key)); }
public void CreateScope_PassValidScope(){ ScriptScopeDictionary dict = new ScriptScopeDictionary(); dict["foo_1"] = 1000; dict["foo_2"] = 2000; ScriptScope linkedScope = _runtime.CreateScope(new ObjectDictionaryExpando(dict)); linkedScope.SetVariable("foo_3", 3000); linkedScope.SetVariable("foo_4", 4000); Assert.IsTrue(linkedScope.IsValid()); Assert.IsTrue(dict["foo_3"].Equals(3000)); Assert.IsTrue(dict["foo_4"].Equals(4000)); Assert.IsTrue(linkedScope.ContainsVariable("foo_1")); Assert.IsTrue(linkedScope.ContainsVariable("foo_2")); }