public void ShouldMaintainContextEvenWhenNestingCalls() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); obj.Prototype.PrintName = (Func <String>)(() => { return(Proto.CurrentContext.Name); }); obj.Prototype.PrintAge = (Func <String>)(() => { return(Proto.CurrentContext.Age); }); obj.Name = "OBJ_1"; obj.Age = "32"; obj2.Name = "OBJ_2"; obj2.Age = "12"; obj.PrintAll = (Func <String>)(() => { return(String.Format("{0}|{1}|{2}|{3}", Proto.CurrentContext.PrintName(), obj2.PrintName(), Proto.CurrentContext.PrintAge(), obj2.PrintAge())); }); Assert.Equal("OBJ_1|OBJ_2|32|12", obj.PrintAll()); }
public void ShouldMaintainCorrectContextEvenWithMultipleThreads() { dynamic obj = new ProtoObject(); obj.Prototype.Name = ""; obj.Prototype.Say = (Func <String>)(() => { dynamic @this = Proto.CurrentContext; return(String.Format("Say: {0}", @this.Name)); }); var objects = new List <dynamic>(); //Using 1000 here to ensure at least 2 threads are used for (int i = 0; i < 1000; i++) { dynamic newObj = new ProtoObject(); newObj.Name = i; objects.Add(newObj); } Parallel.ForEach(objects, target => { var expected = "Say: " + target.Name; Assert.Equal(expected, target.Say()); }); }
public void ShouldMaintainContextEvenWhenExceptionsAreThrownFromInnerCalls() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); obj.Prototype.PrintName = (Func <String>)(() => { return(Proto.CurrentContext.Name); }); obj.Prototype.Throws = (Action)(() => { throw new InvalidOperationException(String.Format("{0}: Threw an exception", Proto.CurrentContext.Name)); }); obj.Prototype.CheckValues = (Action)(() => { var name = Proto.CurrentContext.PrintName(); try { Proto.CurrentContext.Throws(); Assert.True(false, "Previous line should prevent this from executing"); } catch (Exception ex) { Assert.Equal(Proto.CurrentContext.Name + ": Threw an exception", ex.Message); } Assert.Equal(name, Proto.CurrentContext.Name); }); obj.Name = "OBJ_1"; obj2.Name = "OBJ_2"; obj.CheckValues(); obj2.CheckValues(); }
public void PrototypeMembersShouldBeAvailableToAllObjects() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); obj.Prototype.Name = "Josh"; Assert.Equal("Josh", obj.Name); Assert.Equal("Josh", obj2.Name); }
public void ShouldClearCurrentContextEvenIfExceptionIsThrownDuringInvocation() { dynamic obj = new ProtoObject(); obj.Prototype.Throw = (Action)(() => { throw new Exception("It Blew Up!"); }); Assert.Null(Proto.CurrentContext); Assert.Throws <Exception>(() => obj.Throw()); Assert.Null(Proto.CurrentContext); }
public void ShouldBeAbleToOverwriteMembers() { dynamic obj = new ProtoObject(); obj.Name = "Josh"; Assert.Equal("Josh", obj.Name); obj.Name = 56; Assert.Equal(56, obj.Name); }
public void InstanceMembersShouldOverridePrototypeMembers() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); dynamic obj3 = new ProtoObject(); obj.Prototype.Name = "Josh"; obj2.Name = "Dan"; obj3.Name = "Kathy"; Assert.Equal("Josh", obj.Name); Assert.Equal("Dan", obj2.Name); Assert.Equal("Kathy", obj3.Name); }
} //全局变量 public Script() { Global = new ScriptGlobal(); TypeObject = new ScriptTypeObject("Object"); TypeObjectValue = new ScriptValue(TypeObject); Global.SetValue(TypeObject.TypeName, TypeObjectValue); TypeBoolean = new ScriptBasicType("Bool", TypeObjectValue); TypeBooleanValue = new ScriptValue(TypeBoolean); Global.SetValue(TypeBoolean.TypeName, TypeBooleanValue); TypeNumber = new ScriptBasicType("Number", TypeObjectValue); TypeNumberValue = new ScriptValue(TypeNumber); Global.SetValue(TypeNumber.TypeName, TypeNumberValue); TypeString = new ScriptBasicType("String", TypeObjectValue); TypeStringValue = new ScriptValue(TypeString); Global.SetValue(TypeString.TypeName, TypeStringValue); TypeArray = new ScriptBasicType("Array", TypeObjectValue); TypeArrayValue = new ScriptValue(TypeArray); Global.SetValue(TypeArray.TypeName, TypeArrayValue); TypeMap = new ScriptBasicType("Map", TypeObjectValue); TypeMapValue = new ScriptValue(TypeMap); Global.SetValue(TypeMap.TypeName, TypeMapValue); TypeFunction = new ScriptBasicType("Function", TypeObjectValue); TypeFunctionValue = new ScriptValue(TypeFunction); Global.SetValue(TypeFunction.TypeName, TypeFunctionValue); Global.SetValue(GLOBAL_NAME, new ScriptValue(Global)); Global.SetValue(GLOBAL_SCRIPT, ScriptValue.CreateValue(this)); Global.SetValue(GLOBAL_VERSION, ScriptValue.CreateValue(typeof(Version))); ProtoObject.Load(this, TypeObject); ProtoBoolean.Load(this, TypeBoolean); ProtoNumber.Load(this, TypeNumber); ProtoString.Load(this, TypeString); ProtoArray.Load(this, TypeArray); ProtoMap.Load(this, TypeMap); ProtoFunction.Load(this, TypeFunction); TypeManager.PushAssembly(typeof(object).Assembly); //mscorlib.dll TypeManager.PushAssembly(typeof(System.Net.Sockets.Socket).Assembly); //System.dll TypeManager.PushAssembly(GetType().Assembly); //当前所在的程序集 LoadLibrary(); }
public void PrototypeMembersShouldBeAvailableToDerivedTypes() { dynamic obj = new ProtoObject(); obj.Prototype.Name = "Josh"; dynamic person = new Person(); dynamic emp = new Employee(); dynamic sales = new Salesman(); dynamic mgr = new Manager(); Assert.Equal("Josh", person.Name); Assert.Equal("Josh", emp.Name); Assert.Equal("Josh", sales.Name); Assert.Equal("Josh", mgr.Name); }
public void ShouldBeAbleToAddMembersDynamically() { dynamic obj = new ProtoObject(); obj.Name = "Josh"; Assert.Equal("Josh", obj.Name); obj.Age = 34; Assert.Equal(34, obj.Age); obj.Is21 = (Func <Boolean>)(() => obj.Age >= 21); Assert.True(obj.Is21()); }
public void ShouldBeAbleToAccessInstanceFromCurrentContext() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); dynamic obj3 = new ProtoObject(); obj.Prototype.Name = "Josh"; obj.Prototype.SayHello = (Func <String>)(() => { dynamic @this = Proto.CurrentContext; return(String.Format("Hello, {0}!", @this.Name)); }); obj2.Name = "Dan"; obj3.Name = "Kathy"; Assert.Equal("Hello, Josh!", obj.SayHello()); Assert.Equal("Hello, Dan!", obj2.SayHello()); Assert.Equal("Hello, Kathy!", obj3.SayHello()); }
public Script() { Global = new ScriptGlobal(); TypeObject = new ScriptTypeObject("Object"); TypeObjectValue = new ScriptValue(TypeObject); Global.SetValue(TypeObject.TypeName, TypeObjectValue); AddPrimitivePrototype("Bool", ref m_TypeBool, ref m_TypeValueBool); AddPrimitivePrototype("Number", ref m_TypeNumber, ref m_TypeValueNumber); AddPrimitivePrototype("String", ref m_TypeString, ref m_TypeValueString); AddPrimitivePrototype("Function", ref m_TypeFunction, ref m_TypeValueFunction); AddBasicPrototype(m_TypeArray = new ScriptTypeBasicArray(this, "Array", TypeObjectValue), ref m_TypeValueArray); AddBasicPrototype(m_TypeMap = new ScriptTypeBasicMap(this, "Map", TypeObjectValue), ref m_TypeValueMap); AddBasicPrototype(m_TypeStringBuilder = new ScriptTypeBasicStringBuilder(this, "StringBuilder", TypeObjectValue), ref m_TypeValueStringBuilder); Global.SetValue(GLOBAL_NAME, new ScriptValue(Global)); Global.SetValue(GLOBAL_SCRIPT, ScriptValue.CreateValue(this)); Global.SetValue(GLOBAL_VERSION, ScriptValue.CreateValue(typeof(Version))); ProtoObject.Load(this, TypeObject); ProtoBoolean.Load(this, TypeBoolean); ProtoNumber.Load(this, TypeNumber); ProtoString.Load(this, TypeString); ProtoArray.Load(this, TypeArray); ProtoMap.Load(this, TypeMap); ProtoFunction.Load(this, TypeFunction); ProtoStringBuilder.Load(this, TypeStringBuilder); TypeManager.PushAssembly(typeof(object).Assembly); //mscorlib.dll TypeManager.PushAssembly(typeof(System.Net.Sockets.Socket).Assembly); //System.dll TypeManager.PushAssembly(GetType().Assembly); //当前所在的程序集 LibraryBasis.Load(this); LibraryJson.Load(this); LibraryMath.Load(this); LibraryUserdata.Load(this); LibraryIO.Load(this); }
public void ShouldBeAbleToCreateInstanceOfProtoObject() { var obj = new ProtoObject(); }
public void ShouldClearCurrentContextEvenIfExceptionIsThrownDuringInvocation() { dynamic obj = new ProtoObject(); obj.Prototype.Throw = (Action)(() => { throw new Exception("It Blew Up!"); }); Assert.Null(Proto.CurrentContext); Assert.Throws<Exception>(() => obj.Throw()); Assert.Null(Proto.CurrentContext); }
public void ShouldMaintainContextEvenWhenExceptionsAreThrownFromInnerCalls() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); obj.Prototype.PrintName = (Func<String>)(() => { return Proto.CurrentContext.Name; }); obj.Prototype.Throws = (Action)(() => { throw new InvalidOperationException(String.Format("{0}: Threw an exception", Proto.CurrentContext.Name)); }); obj.Prototype.CheckValues = (Action)(() => { var name = Proto.CurrentContext.PrintName(); try { Proto.CurrentContext.Throws(); Assert.True(false, "Previous line should prevent this from executing"); } catch(Exception ex) { Assert.Equal(Proto.CurrentContext.Name + ": Threw an exception", ex.Message); } Assert.Equal(name, Proto.CurrentContext.Name); }); obj.Name = "OBJ_1"; obj2.Name = "OBJ_2"; obj.CheckValues(); obj2.CheckValues(); }
public void ShouldReturnUndefinedForMembersThatDoNotExist() { dynamic obj = new ProtoObject(); Assert.Equal(Proto.Undefined, obj.NoWhereElse); }
public void ShouldMaintainContextEvenWhenNestingCalls() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); obj.Prototype.PrintName = (Func<String>)(() => { return Proto.CurrentContext.Name; }); obj.Prototype.PrintAge = (Func<String>)(() => { return Proto.CurrentContext.Age; }); obj.Name = "OBJ_1"; obj.Age = "32"; obj2.Name = "OBJ_2"; obj2.Age = "12"; obj.PrintAll = (Func<String>)(() => { return String.Format("{0}|{1}|{2}|{3}", Proto.CurrentContext.PrintName(), obj2.PrintName(), Proto.CurrentContext.PrintAge(), obj2.PrintAge()); }); Assert.Equal("OBJ_1|OBJ_2|32|12", obj.PrintAll()); }
public void ShouldMaintainCorrectContextEvenWithMultipleThreads() { dynamic obj = new ProtoObject(); obj.Prototype.Name = ""; obj.Prototype.Say = (Func<String>)(() => { dynamic @this = Proto.CurrentContext; return String.Format("Say: {0}", @this.Name); }); var objects = new List<dynamic>(); //Using 1000 here to ensure at least 2 threads are used for (int i = 0; i < 1000; i++) { dynamic newObj = new ProtoObject(); newObj.Name = i; objects.Add(newObj); } Parallel.ForEach(objects, target => { var expected = "Say: " + target.Name; Assert.Equal(expected, target.Say()); }); }
public void ShouldBeAbleToAddMembersDynamically() { dynamic obj = new ProtoObject(); obj.Name = "Josh"; Assert.Equal("Josh", obj.Name); obj.Age = 34; Assert.Equal(34, obj.Age); obj.Is21 = (Func<Boolean>)(() => obj.Age >= 21); Assert.True(obj.Is21()); }
public void ShouldBeAbleToAccessInstanceFromCurrentContext() { dynamic obj = new ProtoObject(); dynamic obj2 = new ProtoObject(); dynamic obj3 = new ProtoObject(); obj.Prototype.Name = "Josh"; obj.Prototype.SayHello = (Func<String>)(() => { dynamic @this = Proto.CurrentContext; return String.Format("Hello, {0}!", @this.Name); }); obj2.Name = "Dan"; obj3.Name = "Kathy"; Assert.Equal("Hello, Josh!", obj.SayHello()); Assert.Equal("Hello, Dan!", obj2.SayHello()); Assert.Equal("Hello, Kathy!", obj3.SayHello()); }