public void TestMethod1() { // <div style="1"> // <div style="2"></div> // </div> var rootBlueprint = ComponentBlueprint.From <DivComponent, DivComponentProps>(new DivComponentProps { Style = "1", Children = new Blueprint[] { ComponentBlueprint.From <DivComponent, DivComponentProps>(new DivComponentProps { Style = "2" }), }, }); var document = new TestHtmlDocument(); var renderer = new DomRenderer(document); var htmlElement = renderer.Mount(rootBlueprint).RootNodes[0]; var expected = "<div style=\"1\"><div style=\"2\"></div></div>"; Assert.AreEqual(htmlElement.ToString(), expected); }
public void TestRedrawOnChangeProps() { // <PropsChangeTestComponent Id="1"> // </PropsChangeTestComponent> const string id = "1"; var rootBlueprint = ComponentBlueprint.From <PropsChangeTestComponent, PropsChangeTestComponentProps>(new PropsChangeTestComponentProps { Id = id, }); var document = new TestHtmlDocument(); var renderer = new DomRenderer(document); var renderNode = renderer.Mount(rootBlueprint); var expected = "<div id=\"1\"></div>"; Assert.AreEqual(renderNode.RootNodes[0].ToString(), expected); var childrenString = ""; for (var i = 0; i < 10; i += 1) { PropsChangeTestComponent.IncreaseLength(id); childrenString += $"<div id=\"1-{i + 1}\"></div>"; expected = $"<div id=\"1\">{childrenString}</div>"; Assert.AreEqual(renderNode.RootNodes[0].ToString(), expected); } }
public PartBlueprint GetBlueprint() { PartBlueprint bp = new PartBlueprint(); bp.partName = partName; bp.position = transform.localPosition; //All parts should be direct children of "body". If that weren't the standard, things could get weird considering multiple connections per part etc. bp.rotation = transform.localEulerAngles; bp.health = health; bp.heat = currentHeat; //if supported, save each component's extra info int i = 0; foreach (PartComponent component in components) { ComponentBlueprint cbp = component.GetBlueprint(); if (cbp == null) { continue; } cbp.componentIndex = i; //keep track of the index of this component to apply it correctly when loading. bp.componentDatas.Add(cbp); i++; } return(bp); }
public override ComponentBlueprint Render() { var style = State.IsClicked ? $"{Props.Style} clicked" : Props.Style; return(ComponentBlueprint.From <DivComponent, DivComponentProps>(new DivComponentProps { Style = style, Children = Props.Children, })); }
Blueprint ParseBlueprint(string name, JsonData blueprintData) { var components = new ComponentBlueprint[blueprintData.Count]; int i = 0; foreach (KeyValuePair <string, JsonData> n in blueprintData) { components[i] = ParseComponentBlueprint(n.Key, n.Value); i++; } return(new Blueprint(name, components)); }
public RenderNode Mount(Blueprint blueprint) { return(blueprint switch { ComponentBlueprint componentBlueprint => IsPlatformSpecificComponent(componentBlueprint) ? MountPlatformSpecificComponent(componentBlueprint) : MountUserDefinedComponent(componentBlueprint), ValueBlueprint valueBlueprint => MountValueBlueprint(valueBlueprint), _ => throw new Exception($"Unknown Blueprint Type {blueprint.GetType()}"), });
ComponentBlueprint ParseComponentBlueprint(string name, JsonData componentData) { var componentBlueprint = new ComponentBlueprint(name); var members = new SerializableMember[componentData.Count]; int i = 0; foreach (KeyValuePair <string, JsonData> n in componentData) { members[i] = new JsonSerializableMember(n.Key, n.Value); i++; } componentBlueprint.Members = members; return(componentBlueprint); }
public override ComponentBlueprint Render() { var children = new List <ComponentBlueprint>(); for (var i = 0; i < this.State.Length; i += 1) { children.Add(ComponentBlueprint.From <DivComponent, DivComponentProps>(new DivComponentProps { Id = $"{Props.Id}-{i + 1}", })); } return(ComponentBlueprint.From <DivComponent, DivComponentProps>(new DivComponentProps { Id = Props.Id, Children = children.ToArray(), })); }
public void TestTextNodeChild() { // <div>"abc"</div> var rootBlueprint = ComponentBlueprint.From <DivComponent, DivComponentProps>(new DivComponentProps { Children = new Blueprint[] { new ValueBlueprint("abc"), }, }); var document = new TestHtmlDocument(); var renderer = new DomRenderer(document); var htmlElement = renderer.Mount(rootBlueprint).RootNodes[0]; var expected = @"<div>abc</div>"; Assert.AreEqual(htmlElement.ToString(), expected); }
public void TestRedrawOnChangeState() { // <MyComponent style="1" id="first"> // <MyComponent style="2" id="second"></MyComponent> // </MyComponent> // <div style="1"> // <div style="2"></div> // </div> var rootBlueprint = ComponentBlueprint.From <MyComponent, MyComponentProps>(new MyComponentProps { Style = "1", Id = "first", Children = new Blueprint[] { ComponentBlueprint.From <MyComponent, MyComponentProps>(new MyComponentProps { Style = "2", Id = "second", }), }, }); var document = new TestHtmlDocument(); var renderer = new DomRenderer(document); var renderNode = renderer.Mount(rootBlueprint); var expected = "<div style=\"1\"><div style=\"2\"></div></div>"; Assert.AreEqual(renderNode.RootNodes[0].ToString(), expected); MyComponent.Click("first"); expected = "<div style=\"1 clicked\"><div style=\"2\"></div></div>"; Assert.AreEqual(renderNode.RootNodes[0].ToString(), expected); }
void when_throwing() { before = () => { var componentNames = new [] { "Health", "Position", "View" }; var metaData = new PoolMetaData("My Pool", componentNames, null); _pool = new Pool(componentNames.Length, 42, metaData); _entity = createEntity(); }; it["creates exception with hint separated by newLine"] = () => { // given const string msg = "Message"; const string hint = "Hint"; var ex = new EntitasException(msg, hint); // then ex.Message.should_be(msg + "\n" + hint); }; it["ignores hint when null"] = () => { // given const string msg = "Message"; string hint = null; var ex = new EntitasException(msg, hint); // then ex.Message.should_be(msg); }; context["Entity"] = () => { context["when not enabled"] = () => { before = () => { _pool.DestroyEntity(_entity); }; it["add a component"] = () => printErrorMessage(() => _entity.AddComponentA()); it["remove a component"] = () => printErrorMessage(() => _entity.RemoveComponentA()); it["replace a component"] = () => printErrorMessage(() => _entity.ReplaceComponentA(Component.A)); }; context["when enabled"] = () => { it["add a component twice"] = () => printErrorMessage(() => { _entity.AddComponentA(); _entity.AddComponentA(); }); it["remove a component that doesn't exist"] = () => printErrorMessage(() => { _entity.RemoveComponentA(); }); it["get a component that doesn't exist"] = () => printErrorMessage(() => { _entity.GetComponentA(); }); it["retain an entity twice"] = () => printErrorMessage(() => { var owner = new object(); _entity.Retain(owner); _entity.Retain(owner); }); it["release an entity with wrong owner"] = () => printErrorMessage(() => { var owner = new object(); _entity.Release(owner); }); }; }; context["Group"] = () => { it["get single entity when multiple exist"] = () => printErrorMessage(() => { createEntityA(); createEntityA(); var matcher = createMatcherA(); matcher.componentNames = _pool.metaData.componentNames; var group = _pool.GetGroup(matcher); group.GetSingleEntity(); }); }; context["GroupObserver"] = () => { it["unbalanced goups"] = () => printErrorMessage(() => { var g1 = new Group(Matcher.AllOf(CID.ComponentA)); var g2 = new Group(Matcher.AllOf(CID.ComponentB)); var e1 = GroupEventType.OnEntityAdded; new GroupObserver(new [] { g1, g2 }, new [] { e1 }); }); }; context["Pool"] = () => { it["wrong PoolMetaData componentNames count"] = () => printErrorMessage(() => { var componentNames = new [] { "Health", "Position", "View" }; var metaData = new PoolMetaData("My Pool", componentNames, null); new Pool(1, 0, metaData); }); it["destroy entity which is not in pool"] = () => printErrorMessage(() => { _pool.DestroyEntity(new Entity(0, null)); }); it["destroy retained entities"] = () => printErrorMessage(() => { createEntity().Retain(this); _pool.DestroyAllEntities(); }); it["releases entity before destroy"] = () => printErrorMessage(() => { _entity.Release(_pool); }); it["unknown entityIndex"] = () => printErrorMessage(() => { _pool.GetEntityIndex("unknown"); }); it["duplicate entityIndex"] = () => printErrorMessage(() => { var index = new PrimaryEntityIndex <string>(getGroupA(), null); _pool.AddEntityIndex("duplicate", index); _pool.AddEntityIndex("duplicate", index); }); }; context["CollectionExtension"] = () => { it["get single entity when more than one exist"] = () => printErrorMessage(() => { new Entity[2].SingleEntity(); }); }; context["ComponentBlueprint"] = () => { it["type doesn't implement IComponent"] = () => printErrorMessage(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "string"; componentBlueprint.CreateComponent(_entity); }); it["type doesn't exist"] = () => printErrorMessage(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "UnknownType"; componentBlueprint.CreateComponent(_entity); }); it["invalid field name"] = () => printErrorMessage(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.index = 0; componentBlueprint.fullTypeName = typeof(NameAgeComponent).FullName; componentBlueprint.members = new [] { new SerializableMember("xxx", "publicFieldValue"), new SerializableMember("publicProperty", "publicPropertyValue") }; componentBlueprint.CreateComponent(_entity); }); }; context["EntityIndex"] = () => { it["no entity with key"] = () => printErrorMessage(() => { createPrimaryIndex().GetEntity("unknownKey"); }); it["multiple entities for primary key"] = () => printErrorMessage(() => { createPrimaryIndex(); var nameAge = createNameAge(); _pool.CreateEntity().AddComponent(CID.ComponentA, nameAge); _pool.CreateEntity().AddComponent(CID.ComponentA, nameAge); }); }; }
void when_creating() { IContext <TestEntity> ctx = null; TestEntity entity = null; before = () => { ctx = new MyTestContext(); entity = ctx.CreateEntity(); }; context["ComponentBlueprint"] = () => { it["creates a component blueprint from a component without members"] = () => { var component = new ComponentA(); const int index = 42; var componentBlueprint = new ComponentBlueprint(index, component); componentBlueprint.index.should_be(index); componentBlueprint.fullTypeName.should_be(component.GetType().FullName); componentBlueprint.members.Length.should_be(0); }; it["throws when unknown type"] = expect <ComponentBlueprintException>(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "UnknownType"; componentBlueprint.CreateComponent(null); }); it["throws when type doesn't implement IComponent"] = expect <ComponentBlueprintException>(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "string"; componentBlueprint.CreateComponent(null); }); it["creates a component blueprint from a component with members"] = () => { var component = new NameAgeComponent(); component.name = "Max"; component.age = 42; const int index = 24; var componentBlueprint = new ComponentBlueprint(index, component); componentBlueprint.index.should_be(index); componentBlueprint.fullTypeName.should_be(component.GetType().FullName); componentBlueprint.members.Length.should_be(2); componentBlueprint.members[0].name.should_be("name"); componentBlueprint.members[0].value.should_be(component.name); componentBlueprint.members[1].name.should_be("age"); componentBlueprint.members[1].value.should_be(component.age); }; it["creates a component and sets members values"] = () => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = typeof(ComponentWithFieldsAndProperties).FullName; componentBlueprint.index = CID.ComponentB; componentBlueprint.members = new [] { new SerializableMember("publicField", "publicFieldValue"), new SerializableMember("publicProperty", "publicPropertyValue") }; var component = (ComponentWithFieldsAndProperties)componentBlueprint.CreateComponent(entity); component.publicField.should_be("publicFieldValue"); component.publicProperty.should_be("publicPropertyValue"); }; it["ignores invalid member name"] = () => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.index = 0; componentBlueprint.fullTypeName = typeof(NameAgeComponent).FullName; componentBlueprint.members = new [] { new SerializableMember("xxx", "publicFieldValue"), new SerializableMember("publicProperty", "publicPropertyValue") }; componentBlueprint.CreateComponent(entity); }; }; context["Blueprint"] = () => { it["creates an empty blueprint from a null entity"] = () => { var blueprint = new Blueprint("My Context", "Hero", null); blueprint.contextIdentifier.should_be("My Context"); blueprint.name.should_be("Hero"); blueprint.components.Length.should_be(0); }; it["creates a blueprint from an entity"] = () => { entity.AddComponentA(); var component = new NameAgeComponent(); component.name = "Max"; component.age = 42; entity.AddComponent(CID.ComponentB, component); var blueprint = new Blueprint("My Context", "Hero", entity); blueprint.contextIdentifier.should_be("My Context"); blueprint.name.should_be("Hero"); blueprint.components.Length.should_be(2); blueprint.components[0].index.should_be(CID.ComponentA); blueprint.components[0].fullTypeName.should_be(Component.A.GetType().FullName); blueprint.components[1].index.should_be(CID.ComponentB); blueprint.components[1].fullTypeName.should_be(component.GetType().FullName); }; context["when applying blueprint"] = () => { Blueprint blueprint = null; before = () => { var component1 = new ComponentBlueprint(); component1.index = CID.ComponentA; component1.fullTypeName = typeof(ComponentA).FullName; component1.members = new SerializableMember[0]; var component2 = new ComponentBlueprint(); component2.index = CID.ComponentB; component2.fullTypeName = typeof(NameAgeComponent).FullName; component2.members = new [] { new SerializableMember("name", "Max"), new SerializableMember("age", 42) }; blueprint = new Blueprint(); blueprint.name = "Hero"; blueprint.components = new [] { component1, component2 }; }; it["applies blueprint to entity"] = () => { entity.ApplyBlueprint(blueprint); entity.GetComponents().Length.should_be(2); entity.GetComponent(CID.ComponentA).GetType().should_be(typeof(ComponentA)); var nameAgeComponent = (NameAgeComponent)entity.GetComponent(CID.ComponentB); nameAgeComponent.GetType().should_be(typeof(NameAgeComponent)); nameAgeComponent.name.should_be("Max"); nameAgeComponent.age.should_be(42); }; it["throws when entity already has a component which should be added from blueprint"] = expect <EntityAlreadyHasComponentException>(() => { entity.AddComponentA(); entity.ApplyBlueprint(blueprint); }); it["can overwrite existing components"] = () => { var nameAgeComponent = new NameAgeComponent(); nameAgeComponent.name = "Jack"; nameAgeComponent.age = 24; entity.AddComponent(CID.ComponentB, nameAgeComponent); entity.ApplyBlueprint(blueprint, true); }; it["uses component from componentPool"] = () => { var component = new ComponentBlueprint(); component.index = CID.ComponentA; component.fullTypeName = typeof(ComponentA).FullName; component.members = new SerializableMember[0]; blueprint = new Blueprint(); blueprint.name = "Hero"; blueprint.components = new [] { component }; var componentA = entity.CreateComponent <ComponentA>(CID.ComponentA); entity.AddComponent(CID.ComponentA, componentA); entity.RemoveComponentA(); entity.ApplyBlueprint(blueprint); entity.GetComponentA().should_be_same(componentA); }; }; }; }
public virtual void ApplyBlueprint(ComponentBlueprint bp) { return; }
void when_throwing() { MyTestContext ctx = null; TestEntity entity = null; before = () => { var componentNames = new [] { "Health", "Position", "View" }; var contextInfo = new ContextInfo("My Context", componentNames, null); ctx = new MyTestContext(componentNames.Length, 42, contextInfo); entity = ctx.CreateEntity(); }; context["Entity"] = () => { context["when not enabled"] = () => { before = () => { entity.Destroy(); }; it["add a component"] = () => printErrorMessage(() => entity.AddComponentA()); it["remove a component"] = () => printErrorMessage(() => entity.RemoveComponentA()); it["replace a component"] = () => printErrorMessage(() => entity.ReplaceComponentA(Component.A)); }; context["when enabled"] = () => { it["add a component twice"] = () => printErrorMessage(() => { entity.AddComponentA(); entity.AddComponentA(); }); it["remove a component that doesn't exist"] = () => printErrorMessage(() => { entity.RemoveComponentA(); }); it["get a component that doesn't exist"] = () => printErrorMessage(() => { entity.GetComponentA(); }); it["retain an entity twice"] = () => printErrorMessage(() => { var owner = new object(); entity.Retain(owner); entity.Retain(owner); }); it["release an entity with wrong owner"] = () => printErrorMessage(() => { var owner = new object(); entity.Release(owner); }); }; }; context["Group"] = () => { it["get single entity when multiple exist"] = () => printErrorMessage(() => { ctx.CreateEntity().AddComponentA(); ctx.CreateEntity().AddComponentA(); var matcher = (Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA); matcher.componentNames = ctx.contextInfo.componentNames; var group = ctx.GetGroup(matcher); group.GetSingleEntity(); }); }; context["Collector<TestEntity>"] = () => { it["unbalanced goups"] = () => printErrorMessage(() => { var g1 = new Group <TestEntity>(Matcher <TestEntity> .AllOf(CID.ComponentA)); var g2 = new Group <TestEntity>(Matcher <TestEntity> .AllOf(CID.ComponentB)); var e1 = GroupEvent.Added; new Collector <TestEntity>(new [] { g1, g2 }, new [] { e1 }); }); }; context["Context"] = () => { it["wrong ContextInfo componentNames count"] = () => printErrorMessage(() => { var componentNames = new [] { "Health", "Position", "View" }; var contextInfo = new ContextInfo("My Context", componentNames, null); new MyTestContext(1, 0, contextInfo); }); it["destroy retained entities"] = () => printErrorMessage(() => { var e = ctx.CreateEntity(); e.Retain(this); e.Retain(new object()); e = ctx.CreateEntity(); e.Retain(this); e.Retain(new object()); ctx.DestroyAllEntities(); }); it["releases entity before destroy"] = () => printErrorMessage(() => { entity.Release(ctx); }); it["unknown entityIndex"] = () => printErrorMessage(() => { ctx.GetEntityIndex("unknown"); }); it["duplicate entityIndex"] = () => printErrorMessage(() => { var groupA = ctx.GetGroup((Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA)); var index = new PrimaryEntityIndex <TestEntity, string>("TestIndex", groupA, (arg1, arg2) => string.Empty); ctx.AddEntityIndex(index); ctx.AddEntityIndex(index); }); }; context["CollectionExtension"] = () => { it["get single entity when more than one exist"] = () => printErrorMessage(() => { new IEntity[2].SingleEntity(); }); }; context["ComponentBlueprint"] = () => { it["type doesn't implement IComponent"] = () => printErrorMessage(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "string"; componentBlueprint.CreateComponent(entity); }); it["type doesn't exist"] = () => printErrorMessage(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.fullTypeName = "UnknownType"; componentBlueprint.CreateComponent(entity); }); it["invalid field name"] = () => printErrorMessage(() => { var componentBlueprint = new ComponentBlueprint(); componentBlueprint.index = 0; componentBlueprint.fullTypeName = typeof(NameAgeComponent).FullName; componentBlueprint.members = new [] { new SerializableMember("xxx", "publicFieldValue"), new SerializableMember("publicProperty", "publicPropertyValue") }; componentBlueprint.CreateComponent(entity); }); }; context["EntityIndex"] = () => { it["no entity with key"] = () => printErrorMessage(() => { var groupA = ctx.GetGroup((Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA)); var index = new PrimaryEntityIndex <TestEntity, string>("TestIndex", groupA, (e, c) => ((NameAgeComponent)c).name); index.GetEntity("unknownKey"); }); it["multiple entities for primary key"] = () => printErrorMessage(() => { var groupA = ctx.GetGroup((Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA)); new PrimaryEntityIndex <TestEntity, string>("TestIndex", groupA, (e, c) => ((NameAgeComponent)c).name); var nameAge = new NameAgeComponent(); nameAge.name = "Max"; nameAge.age = 42; ctx.CreateEntity().AddComponent(CID.ComponentA, nameAge); ctx.CreateEntity().AddComponent(CID.ComponentA, nameAge); }); }; }