public void RestoreTest_Simple() { // Create a type var testStructType = registry.CreateType(UTinyId.New(), "TestStructType", UTinyTypeCode.Struct); var testRef = (UTinyType.Reference)testStructType; IMemento initialState = null; // Register for changed events caretaker.OnObjectChanged += (originator, memento) => { initialState = memento; }; // Update to get the initial state; flush changes caretaker.Update(); Assert.NotNull(initialState); { // Make some changes to the created type testStructType.Name = "OtherTestStructType"; Assert.AreEqual(testStructType.Name, "OtherTestStructType"); // revert them testStructType.Restore(initialState); testStructType = testRef.Dereference(context.Registry); Assert.NotNull(testStructType); Assert.AreEqual(testStructType.Name, "TestStructType"); } }
public void TypeInitialDefaultValue() { var registry = new UTinyRegistry(); var type = registry.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); type.CreateField( "TestIntField", (UTinyType.Reference)UTinyType.Int32); type.CreateField( "TestFloatField", (UTinyType.Reference)UTinyType.Float32); var defaultValue = type.DefaultValue as UTinyObject; // Assert that we have some default value object that has been created for us Assert.IsNotNull(defaultValue); // Test the existance and value of the fields Assert.AreEqual(0, defaultValue["TestIntField"]); Assert.AreEqual(0f, defaultValue["TestFloatField"]); }
public void FieldTest() { var registry = new UTinyRegistry(); var @enum = registry.CreateType( UTinyId.New(), "TestEnum", UTinyTypeCode.Enum ); @enum.BaseType = (UTinyType.Reference)UTinyType.Int32; @enum.CreateField("A", (UTinyType.Reference)UTinyType.Int32); var type = registry.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct ); type.CreateField( "TextureReference", (UTinyType.Reference)UTinyType.Texture2DEntity); type.CreateField( "EntityReference", (UTinyType.Reference)UTinyType.EntityReference); type.CreateField( "EnumReference", (UTinyType.Reference)@enum); type.Refresh(); Debug.Log(type); }
public void EnumDefaultValue() { var registry = new UTinyRegistry(); var enumType = registry.CreateType(UTinyId.New(), "TestEnum", UTinyTypeCode.Enum); enumType.BaseType = (UTinyType.Reference)UTinyType.Int32; enumType.CreateField("A", (UTinyType.Reference)UTinyType.Int32); enumType.CreateField("B", (UTinyType.Reference)UTinyType.Int32); enumType.CreateField("C", (UTinyType.Reference)UTinyType.Int32); enumType.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)enumType) { ["A"] = 1, ["B"] = 2, ["C"] = 3, }; var structType = registry.CreateType(UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); structType.CreateField("EnumField", (UTinyType.Reference)enumType); structType.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)structType) { ["EnumField"] = new UTinyEnum.Reference(enumType, "B") }; var instance = new UTinyObject(registry, (UTinyType.Reference)structType); Assert.AreEqual(2, ((UTinyEnum.Reference)instance["EnumField"]).Value); Assert.AreEqual("B", ((UTinyEnum.Reference)instance["EnumField"]).Name); }
public void NestedSourceScope() { var registry = new UTinyRegistry(); var builtInCount = registry.Count; var sourceId = "outer"; var nestedSourceId = "inner"; UTinyType testType, testType2; using (registry.SourceIdentifierScope(sourceId)) { testType = registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Component); using (registry.SourceIdentifierScope(nestedSourceId)) { testType2 = registry.CreateType(UTinyId.New(), "TestType2", UTinyTypeCode.Component); } } var testTypeRef = (UTinyType.Reference)testType; var testType2Ref = (UTinyType.Reference)testType2; Assert.AreEqual(builtInCount + 2, registry.Count); registry.UnregisterAllBySource(sourceId); Assert.AreEqual(builtInCount + 1, registry.Count); Assert.IsNull(testTypeRef.Dereference(registry)); Assert.IsNotNull(testType2Ref.Dereference(registry)); registry.UnregisterAllBySource(nestedSourceId); Assert.AreEqual(builtInCount, registry.Count); Assert.IsNull(testTypeRef.Dereference(registry)); Assert.IsNull(testType2Ref.Dereference(registry)); }
public void DetectTypeChanges() { // Create two new types var testStructType = registry.CreateType(UTinyId.New(), "TestStructType", UTinyTypeCode.Struct); registry.CreateType(UTinyId.New(), "TestComponentType", UTinyTypeCode.Struct); // Update to get the initial state; flush changes caretaker.Update(); { // Make some changes to the data model // NOTE: We can make as many changes as we want with no callbacks being invoked. It is simply a version increment testStructType.CreateField("TestIntField", (UTinyType.Reference)UTinyType.Int32); testStructType.CreateField("TestStringField", (UTinyType.Reference)UTinyType.String); testStructType.Name = "OtherTestStructType"; var count = 0; // Register for changed events caretaker.OnObjectChanged += (originator, memento) => { count++; Assert.AreEqual(testStructType, originator); }; // Invoke update // This will detect any changes that were made between now and the last Update. caretaker.Update(); // We should be notified that one object was changed Assert.AreEqual(1, count); } }
public void ListFieldPrimitiveAssignment() { var registry = new UTinyRegistry(); var type = registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Struct); type.CreateField("TestField", (UTinyType.Reference)UTinyType.Int32, true); var instance = new UTinyObject(registry, (UTinyType.Reference)type) { ["TestField"] = new UTinyList(registry, (UTinyType.Reference)UTinyType.Int32) { 1, 2, 3 } }; instance["TestField"] = new UTinyList(registry, (UTinyType.Reference)UTinyType.Int32) { 3, 6, 7 }; Debug.Log(instance); }
public void ObjectListVersionChange() { var registry = new UTinyRegistry(); var type = registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Struct); type.CreateField("TestField", (UTinyType.Reference)UTinyType.Int32); var list = new UTinyList(registry, (UTinyType.Reference)type) { new UTinyObject(registry, (UTinyType.Reference)type) { ["TestField"] = 1 }, new UTinyObject(registry, (UTinyType.Reference)type) { ["TestField"] = 2 }, new UTinyObject(registry, (UTinyType.Reference)type) { ["TestField"] = 3 } }; var version = list.Version; (list[0] as UTinyObject)["TestField"] = 7; Assert.AreNotEqual(version, list.Version); Debug.Log(list); }
public void FlatJsonEntityRoundTrip() { var registry = new UTinyRegistry(); var type = registry.CreateType( UTinyId.New(), "TestType", UTinyTypeCode.Component ); type.CreateField("TestIntField", (UTinyType.Reference)UTinyType.Int32); type.CreateField("TestStringField", (UTinyType.Reference)UTinyType.String); var entity = registry.CreateEntity( UTinyId.New(), "TestEntity"); var component = entity.AddComponent((UTinyType.Reference)type); component.Refresh(); component["TestIntField"] = 10; component["TestStringField"] = "Test"; using (var json = new MemoryStream()) using (var command = new MemoryStream()) { // Write the data model to a stream as json // mem -> json Serialization.FlatJson.BackEnd.Persist(json, type, entity); json.Position = 0; var reader = new StreamReader(json); { Debug.Log(reader.ReadToEnd()); } json.Position = 0; // Read the data model // json -> commands Serialization.FlatJson.FrontEnd.Accept(json, command); command.Position = 0; // Create a registry to hold accepted objects var output = new UTinyRegistry(); // Process the command // commands -> mem Serialization.CommandStream.FrontEnd.Accept(command, output); } }
public void FlatJsonRoundTrip() { var input = new UTinyRegistry(); var structType = input.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); structType.CreateField("IntField", (UTinyType.Reference)UTinyType.Int32); structType.CreateField("FloatField", (UTinyType.Reference)UTinyType.Int32); structType.CreateField("StringField", (UTinyType.Reference)UTinyType.Int32); var module = input.CreateModule( UTinyId.New(), "TestModule"); module.AddStructReference((UTinyType.Reference)structType); using (var json = new MemoryStream()) using (var command = new MemoryStream()) { // Write the data model to a stream as json // mem -> json Serialization.FlatJson.BackEnd.Persist(json, structType, module); json.Position = 0; var reader = new StreamReader(json); { Debug.Log(reader.ReadToEnd()); } json.Position = 0; // Read the data model // json -> commands Serialization.FlatJson.FrontEnd.Accept(json, command); command.Position = 0; // Create a registry to hold accepted objects var output = new UTinyRegistry(); // Process the command // commands -> mem Serialization.CommandStream.FrontEnd.Accept(command, output); Assert.IsNotNull(output.FindById <UTinyType>(structType.Id)); Assert.IsNotNull(output.FindById <UTinyModule>(module.Id)); } }
public void FlatJsonProjectWrite() { var registry = new UTinyRegistry(); var project = registry.CreateProject( UTinyId.New(), "TestProject"); var json = Serialization.FlatJson.BackEnd.Persist(project); Debug.Log(json); }
public void RestoreTest_UTinyEntity() { var compType = registry.CreateType(UTinyId.New(), "TestComponent", UTinyTypeCode.Component); var compTypeRef = (UTinyType.Reference)compType; var testStructType = registry.CreateType(UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); testStructType.CreateField(UTinyId.New(), "IntField", (UTinyType.Reference)UTinyType.Int32); compType.CreateField(UTinyId.New(), "TestStructField", (UTinyType.Reference)testStructType); var undo = new Dictionary <UTinyId, IMemento>(); caretaker.OnObjectChanged += (originator, memento) => { undo[originator.Id] = memento; }; var entity = registry.CreateEntity(UTinyId.New(), "TestEntity"); var entityRef = (UTinyEntity.Reference)entity; var testCompInstance = entity.AddComponent(compTypeRef); testCompInstance.Refresh(); var obj = new UTinyObject(registry, (UTinyType.Reference)testStructType) { ["IntField"] = 0 }; testCompInstance["TestStructField"] = obj; var item = (UTinyObject)testCompInstance["TestStructField"]; // Update to get the initial state; flush changes caretaker.Update(); item["IntField"] = 123; Assert.AreEqual(123, item["IntField"]); // UNDO entity.Restore(undo[entity.Id]); entity = entityRef.Dereference(context.Registry); Assert.NotNull(entity); testCompInstance = entity.GetComponent(compTypeRef); Assert.NotNull(testCompInstance); item = (UTinyObject)testCompInstance["TestStructField"]; Assert.NotNull(item); // make sure IntField was restored Assert.AreEqual(0, item["IntField"]); }
public void Clear() { var registry = new UTinyRegistry(); var builtInCount = registry.Count; var type = registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Component); var typeRef = (UTinyType.Reference)type; Assert.AreEqual(builtInCount + 1, registry.Count); registry.Clear(); Assert.AreEqual(builtInCount, registry.Count); Assert.IsNull(typeRef.Dereference(registry)); }
public void RestoreTest_Lists_Containers() { // Create two new types var testStructType = registry.CreateType(UTinyId.New(), "TestStructType", UTinyTypeCode.Struct); var testStructTypeRef = (UTinyType.Reference)testStructType; var listField = testStructType.CreateField(UTinyId.New(), "TestListField", (UTinyType.Reference)UTinyType.Float32, true); var undo = new Dictionary <UTinyId, IMemento>(); // Register for changed events caretaker.OnObjectChanged += (originator, memento) => { undo[originator.Id] = memento; }; // Update to get the initial state; flush changes caretaker.Update(); // note: UTinyField proxies version storage onto UTinyType, so there should be only 1 memento Assert.AreEqual(1, undo.Count); { // Make some changes to the created type testStructType.Name = "OtherTestStructType"; Assert.AreEqual(testStructType.Name, "OtherTestStructType"); listField.Name = "RevertMe"; Assert.AreEqual(listField.Name, "RevertMe"); // revert changes var kvp = undo.First(); var obj = registry.FindById <UTinyType>(kvp.Key); Assert.NotNull(obj); Assert.IsTrue(ReferenceEquals(obj, testStructType)); obj.Restore(kvp.Value); obj = testStructTypeRef.Dereference(context.Registry); Assert.NotNull(obj); // the field was detached from the list and re-created Assert.AreEqual(1, obj.Fields.Count); var newListField = obj.Fields[0]; Assert.AreEqual("TestStructType", obj.Name); Assert.AreEqual("TestListField", newListField.Name); Assert.AreEqual(listField.Id, newListField.Id); Assert.AreEqual(listField.DeclaringType.Id, newListField.DeclaringType.Id); } }
public void SetUp() { m_Registry = new UTinyRegistry(); m_TestStruct = m_Registry.CreateType(UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); m_TestStruct.CreateField("Foo", (UTinyType.Reference)UTinyType.String); m_TestStruct.CreateField("Bar", (UTinyType.Reference)UTinyType.Int32); m_TestStructWithList = m_Registry.CreateType(UTinyId.New(), "TestStructWithList", UTinyTypeCode.Struct); m_TestStructWithList.CreateField("Foo", (UTinyType.Reference)UTinyType.String, true); m_TestStructWithList.CreateField("Bar", (UTinyType.Reference)UTinyType.Int32, true); m_TestComponent = m_Registry.CreateType(UTinyId.New(), "TestComponent", UTinyTypeCode.Component); m_TestComponent.CreateField("TestStructField", (UTinyType.Reference)m_TestStruct); }
public void Register() { var registry = new UTinyRegistry(); var builtInCount = registry.Count; var type = registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Component); Assert.AreEqual(builtInCount + 1, registry.Count); registry.Register(type); Assert.AreEqual(builtInCount + 1, registry.Count); registry.Unregister(type); Assert.AreEqual(builtInCount, registry.Count); }
private static bool TryParseReference(object obj, out UTinyId id, out string name) { var dictionary = obj as IDictionary <string, object>; if (dictionary == null) { id = UTinyId.Empty; name = null; return(false); } name = dictionary["Name"] as string; id = ParseId(dictionary["Id"]); return(true); }
public void SourceScope() { var registry = new UTinyRegistry(); var builtInCount = registry.Count; var sourceId = "test"; using (registry.SourceIdentifierScope(sourceId)) { registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Component); } Assert.AreEqual(builtInCount + 1, registry.Count); registry.UnregisterAllBySource(sourceId); Assert.AreEqual(builtInCount, registry.Count); }
public void StructType() { var registry = new UTinyRegistry(); var type = registry.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct ); type.CreateField( "TestField", (UTinyType.Reference)UTinyType.Int32); Assert.AreEqual(type.Fields.Count, 1); }
public void FlatJsonTypeWrite() { var registry = new UTinyRegistry(); var type = registry.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); type.CreateField("IntField", (UTinyType.Reference)UTinyType.Int32); type.CreateField("FloatField", (UTinyType.Reference)UTinyType.Int32); type.CreateField("StringField", (UTinyType.Reference)UTinyType.Int32); var json = Serialization.FlatJson.BackEnd.Persist(type); Debug.Log(json); }
public void NestedTypeInitialDefaultValue() { var registry = new UTinyRegistry(); // Create a struct with a single int field var structType = registry.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); structType.CreateField( "TestIntField", (UTinyType.Reference)UTinyType.Int32); // Default the TestStruct.IntField to 7 structType.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)structType) { ["TestIntField"] = 7 }; // Create a component with a single TestStruct field var componentType = registry.CreateType( UTinyId.New(), "TestComponent", UTinyTypeCode.Component); componentType.CreateField( "TestStructField", (UTinyType.Reference)structType); // Grab the default value for TestComponent var testComponentDefaultValue = componentType.DefaultValue as UTinyObject; Assert.IsNotNull(testComponentDefaultValue); // Grab the TestComponent.TestStructField FIELD defaultValue // NOTE: This is NOT the same as the TestStruct TYPE defaultValue var testComponentTestStructFieldDefaultValue = testComponentDefaultValue["TestStructField"] as UTinyObject; Assert.IsNotNull(testComponentTestStructFieldDefaultValue); Assert.AreNotEqual(testComponentDefaultValue, testComponentTestStructFieldDefaultValue); // This value should have been inherited from the type level but CAN be overriden Assert.AreEqual(7, testComponentTestStructFieldDefaultValue["TestIntField"]); }
public void FlatJsonSceneWrite() { var registry = new UTinyRegistry(); var entityGroup = registry.CreateEntityGroup( UTinyId.New(), "TestEntityGroup"); var entity = registry.CreateEntity( UTinyId.New(), "TestEntity"); entityGroup.AddEntityReference((UTinyEntity.Reference)entity); var json = Serialization.FlatJson.BackEnd.Persist(entityGroup, entity); Debug.Log(json); }
public void DetectComponentChanges() { // Create a type and an entity var componentType = registry.CreateType(UTinyId.New(), "TestComponentType", UTinyTypeCode.Component); var testField = componentType.CreateField("TestField", (UTinyType.Reference)UTinyType.Int32); var entity = registry.CreateEntity(UTinyId.New(), "TestEntity"); var component = entity.AddComponent((UTinyType.Reference)componentType); component.Refresh(); // Update to get the initial state; flush changes caretaker.Update(); // Register for changed events caretaker.OnObjectChanged += (originator, memento) => { Debug.Log(memento); }; { (componentType.DefaultValue as UTinyObject)["TestField"] = 5; componentType.Refresh(); // Invoke update // This will detect any changes that were made between now and the last Update. Debug.Log("-------------------- UPDATE --------------------"); caretaker.Update(); component["TestField"] = 10; // Invoke update // This will detect any changes that were made between now and the last Update. Debug.Log("-------------------- UPDATE --------------------"); caretaker.Update(); testField.FieldType = (UTinyType.Reference)UTinyType.String; component.Refresh(); // Invoke update // This will detect any changes that were made between now and the last Update. Debug.Log("-------------------- UPDATE --------------------"); caretaker.Update(); } }
private static UTinyType.Reference GetType(this IRegistry registry, string name) { if (null == registry) { return(UTinyType.Reference.None); } UTinyId id; if (!s_IDCache.TryGetValue(name, out id)) { s_IDCache[name] = id = UTinyId.Generate(name); } var type = registry.FindById <UTinyType>(id); if (null == type) { return(UTinyType.Reference.None); } return((UTinyType.Reference)type); }
public void ResetObjectToDefaultValues() { var registry = new UTinyRegistry(); // Create a type var type = registry.CreateType( UTinyId.New(), "TestStructType", UTinyTypeCode.Struct); type.CreateField( "TestIntField", (UTinyType.Reference)UTinyType.Int32); type.CreateField( "TestFloatField", (UTinyType.Reference)UTinyType.Float32); // Default the TestStruct.IntField to 7 and FloatField to 0.5f type.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)type) { ["TestIntField"] = 7, ["TestFloatField"] = 0.5f }; var @object = new UTinyObject(registry, (UTinyType.Reference)type); Assert.AreEqual(7, @object["TestIntField"]); Assert.AreEqual(0.5f, @object["TestFloatField"]); @object["TestIntField"] = 1; @object["TestFloatField"] = 7.9f; Assert.AreEqual(1, @object["TestIntField"]); Assert.AreEqual(7.9f, @object["TestFloatField"]); @object.Reset(); Assert.AreEqual(7, @object["TestIntField"]); Assert.AreEqual(0.5f, @object["TestFloatField"]); }
public void StreamingRoundTrip() { var input = new UTinyRegistry(); var type = input.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); type.CreateField("IntField", (UTinyType.Reference)UTinyType.Int32); type.CreateField("FloatField", (UTinyType.Reference)UTinyType.Int32); type.CreateField("StringField", (UTinyType.Reference)UTinyType.Int32); var module = input.CreateModule( UTinyId.New(), "TestModule"); module.AddStructReference((UTinyType.Reference)type); using (var command = new MemoryStream()) { // Write the data model to a stream as json // mem -> command BackEnd.Persist(command, type, module); command.Position = 0; // Create a registry to hold accepted objects var output = new UTinyRegistry(); // Process the command // commands -> mem FrontEnd.Accept(command, output); Assert.IsNotNull(output.FindById <UTinyType>(type.Id)); Assert.IsNotNull(output.FindById <UTinyModule>(module.Id)); } }
public void NameChangeTest() { var registry = new UTinyRegistry(); var type = registry.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct ); var module = registry.CreateModule( UTinyId.New(), "TestModule" ); module.AddStructReference((UTinyType.Reference)type); module.Refresh(); type.Name = "NewStruct"; Debug.Log(module.ToString()); }
public void SetUp() { m_Registry = new UTinyRegistry(); m_EnumType = m_Registry.CreateType( UTinyId.New(), "TestEnum", UTinyTypeCode.Enum); m_EnumType.BaseType = (UTinyType.Reference)UTinyType.Int32; m_EnumType.CreateField("A", (UTinyType.Reference)UTinyType.Int32); m_EnumType.CreateField("B", (UTinyType.Reference)UTinyType.Int32); m_EnumType.CreateField("C", (UTinyType.Reference)UTinyType.Int32); m_EnumType.DefaultValue = new UTinyObject(m_Registry, (UTinyType.Reference)m_EnumType) { // @NOTE We are intentionally starting at 1 to detect 0 case as errors ["A"] = 1, ["B"] = 2, ["C"] = 3 }; // Create a component with a single int field m_ComponentType = m_Registry.CreateType( UTinyId.New(), "TestComponent", UTinyTypeCode.Component); m_ComponentType.CreateField( "TestEnumField", (UTinyType.Reference)m_EnumType); m_Entity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity"); var component = m_Entity.AddComponent((UTinyType.Reference)m_ComponentType); component.Refresh(); }
public void FlatJsonProjectRoundTrip() { var registry = new UTinyRegistry(); var project = registry.CreateProject( UTinyId.New(), "TestProject"); using (var json = new MemoryStream()) using (var command = new MemoryStream()) { // Write the data model to a stream as json // mem -> json Serialization.FlatJson.BackEnd.Persist(json, project); json.Position = 0; var reader = new StreamReader(json); { Debug.Log(reader.ReadToEnd()); } json.Position = 0; // Read the data model // json -> commands Serialization.FlatJson.FrontEnd.Accept(json, command); command.Position = 0; // Create a registry to hold accepted objects var output = new UTinyRegistry(); // Process the command // commands -> mem Serialization.CommandStream.FrontEnd.Accept(command, output); } }
public void DetectEntityChanges() { // Create a type and an entity var componentType = registry.CreateType(UTinyId.New(), "TestStructType", UTinyTypeCode.Component); var entity = registry.CreateEntity(UTinyId.New(), "TestEntity"); // Update to get the initial state; flush changes caretaker.Update(); { // Snapshot the initial version var entityVersion = entity.Version; // Make some changes to the data model // NOTE: We can make as many changes as we want with no callbacks being invoked. It is simply a version increment entity.AddComponent((UTinyType.Reference)componentType); entity.Name = "NewEntityName"; var count = 0; // Register for changed events caretaker.OnObjectChanged += (originator, memento) => { count++; Assert.AreEqual(originator, entity); }; // Invoke update // This will detect any changes that were made between now and the last Update. caretaker.Update(); Assert.AreNotEqual(entityVersion, entity.Version); // We should be notified that one object was changed Assert.AreEqual(1, count); } }