void when_created() { it["has values"] = () => { var componentNames = new [] { "Health", "Position", "View" }; var componentTypes = new [] { typeof(ComponentA), typeof(ComponentB), typeof(ComponentC) }; const string contextName = "My Context"; var data = new ContextInfo(contextName, componentNames, componentTypes); data.name.should_be(contextName); data.componentNames.should_be_same(componentNames); data.componentTypes.should_be_same(componentTypes); }; }
public Context(int totalComponents, int startCreationIndex, ContextInfo contextInfo) { _totalComponents = totalComponents; _creationIndex = startCreationIndex; if(contextInfo != null) { _contextInfo = contextInfo; if(contextInfo.componentNames.Length != totalComponents) { throw new ContextInfoException(this, contextInfo); } } else { // If Contexts.CreateContext() was used to create the context, // we will never end up here. // This is a fallback when the context is created manually. var componentNames = new string[totalComponents]; const string prefix = "Index "; for (int i = 0; i < componentNames.Length; i++) { componentNames[i] = prefix + i; } _contextInfo = new ContextInfo( "Unnamed Context", componentNames, null ); } _groupsForIndex = new List<Group>[totalComponents]; _componentPools = new Stack<IComponent>[totalComponents]; _entityIndices = new Dictionary<string, IEntityIndex>(); // Cache delegates to avoid gc allocations _cachedEntityChanged = updateGroupsComponentAddedOrRemoved; _cachedComponentReplaced = updateGroupsComponentReplaced; _cachedEntityReleased = onEntityReleased; }
void when_created() { Entity e = null; before = () => { e = this.CreateEntity(); }; context["initial state"] = () => { it["has default ContextInfo"] = () => { e.contextInfo.name.should_be("No Context"); e.contextInfo.componentNames.Length.should_be(CID.TotalComponents); e.contextInfo.componentTypes.should_be_null(); for(int i = 0; i < e.contextInfo.componentNames.Length; i++) { e.contextInfo.componentNames[i].should_be(i.ToString()); } }; it["has custom ContextInfo when set"] = () => { var contextInfo = new ContextInfo(null, null, null); e = new Entity(0, null, contextInfo); e.contextInfo.should_be_same(contextInfo); }; it["throws when attempting to get component at index which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.GetComponentA(); }); it["gets total components count when empty"] = () => { e.totalComponents.should_be(CID.TotalComponents); }; it["gets empty array of components when no components were added"] = () => { e.GetComponents().should_be_empty(); }; it["gets empty array of component indices when no components were added"] = () => { e.GetComponentIndices().should_be_empty(); }; it["doesn't have component at index when no component was added"] = () => { e.HasComponentA().should_be_false(); }; it["doesn't have components at indices when no components were added"] = () => { e.HasComponents(_indicesA).should_be_false(); }; it["doesn't have any components at indices when no components were added"] = () => { e.HasAnyComponent(_indicesA).should_be_false(); }; it["returns entity when adding a component"] = () => { e.AddComponent(0, null).should_be_same(e); }; it["adds a component"] = () => { e.AddComponentA(); assertHasComponentA(e); }; it["throws when attempting to remove a component at index which hasn't been added"] = expect<EntityDoesNotHaveComponentException>(() => { e.RemoveComponentA(); }); it["replacing a non existing component adds component"] = () => { e.ReplaceComponentA(Component.A); assertHasComponentA(e); }; }; context["when component added"] = () => { before = () => { e.AddComponentA(); }; it["throws when adding a component at the same index twice"] = expect<EntityAlreadyHasComponentException>(() => { e.AddComponentA(); }); it["returns entity when removing a component"] = () => { e.RemoveComponent(CID.ComponentA).should_be_same(e); }; it["removes a component at index"] = () => { e.RemoveComponentA(); assertHasNotComponentA(e); }; it["returns entity when replacing a component"] = () => { e.ReplaceComponent(CID.ComponentA, null).should_be_same(e); }; it["replaces existing component"] = () => { var newComponentA = new ComponentA(); e.ReplaceComponentA(newComponentA); assertHasComponentA(e, newComponentA); }; it["doesn't have components at indices when not all components were added"] = () => { e.HasComponents(_indicesAB).should_be_false(); }; it["has any components at indices when any component was added"] = () => { e.HasAnyComponent(_indicesAB).should_be_true(); }; context["when adding another component"] = () => { before = () => { e.AddComponentB(); }; it["gets all components"] = () => { var components = e.GetComponents(); components.Length.should_be(2); components.should_contain(Component.A); components.should_contain(Component.B); }; it["gets all component indices"] = () => { var componentIndices = e.GetComponentIndices(); componentIndices.Length.should_be(2); componentIndices.should_contain(CID.ComponentA); componentIndices.should_contain(CID.ComponentB); }; it["has other component"] = () => { e.HasComponentB().should_be_true(); }; it["has components at indices when all components were added"] = () => { e.HasComponents(_indicesAB).should_be_true(); }; it["removes all components"] = () => { e.RemoveAllComponents(); e.HasComponentA().should_be_false(); e.HasComponentB().should_be_false(); e.GetComponents().should_be_empty(); e.GetComponentIndices().should_be_empty(); }; it["can ToString and removes *Component suffix"] = () => { e.AddComponent(0, new SomeComponent()); e.Retain(this); e.ToString().should_be("Entity_0(*1)(Some, ComponentA, ComponentB)"); }; it["uses component.ToString()"] = () => { e.AddComponent(0, new NameAgeComponent { name = "Max", age = 42 }); e.ToString().should_be("Entity_0(*0)(NameAge(Max, 42), ComponentA, ComponentB)"); }; it["uses short component name without namespace if ToString is not implemented"] = () => { e.AddComponent(0, new NamespaceComponent()); e.ToString().should_be("Entity_0(*0)(Namespace, ComponentA, ComponentB)"); }; }; }; context["componentPool"] = () => { it["gets component context"] = () => { var componentPool = e.GetComponentPool(CID.ComponentA); componentPool.Count.should_be(0); }; it["gets same component context instance"] = () => { e.GetComponentPool(CID.ComponentA).should_be_same(e.GetComponentPool(CID.ComponentA)); }; it["pushes component to componentPool when removed"] = () => { e.AddComponentA(); var component = e.GetComponentA(); e.RemoveComponentA(); var componentPool = e.GetComponentPool(CID.ComponentA); componentPool.Count.should_be(1); componentPool.Pop().should_be_same(component); }; it["creates new component when componentPool is empty"] = () => { var type = typeof(NameAgeComponent); var component = e.CreateComponent(1, type); component.GetType().should_be(type); var nameAgeComponent = ((NameAgeComponent)component); nameAgeComponent.name.should_be_null(); nameAgeComponent.age.should_be(0); }; it["gets pooled component when componentPool is not empty"] = () => { var component = new NameAgeComponent(); e.AddComponent(1, component); e.RemoveComponent(1); var newComponent = (NameAgeComponent)e.CreateComponent(1, typeof(NameAgeComponent)); newComponent.should_be_same(component); }; }; context["events"] = () => { int didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnComponentAdded when adding a component"] = () => { e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.AddComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing a component"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.RemoveComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved before pushing component to context"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { var newComponent = entity.CreateComponent(index, component.GetType()); component.should_not_be_same(newComponent); }; e.RemoveComponentA(); }; it["dispatches OnComponentReplaced when replacing a component"] = () => { e.AddComponentA(); var newComponentA = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); previousComponent.should_be_same(Component.A); newComponent.should_be_same(newComponentA); }; e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with different component"] = () => { var prevComp = new ComponentA(); var newComp = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; e.AddComponent(CID.ComponentA, prevComp); e.ReplaceComponent(CID.ComponentA, newComp); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with same component"] = () => { e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(Component.A); newComponent.should_be_same(Component.A); }; e.AddComponentA(); e.ReplaceComponentA(Component.A); didDispatch.should_be(1); }; it["doesn't dispatch anything when replacing a non existing component with null"] = () => { e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.ReplaceComponentA(null); }; it["dispatches OnComponentAdded when attempting to replace a component which hasn't been added"] = () => { var newComponentA = new ComponentA(); e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(newComponentA); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when replacing a component with null"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; component.should_be_same(Component.A); }; e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; e.ReplaceComponentA(null); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing all components"] = () => { e.AddComponentA(); e.AddComponentB(); e.OnComponentRemoved += (entity, index, component) => didDispatch += 1; e.RemoveAllComponents(); didDispatch.should_be(2); }; }; context["reference counting"] = () => { it["retains entity"] = () => { e.retainCount.should_be(0); e.Retain(this); e.retainCount.should_be(1); }; it["releases entity"] = () => { e.Retain(this); e.Release(this); e.retainCount.should_be(0); }; it["throws when releasing more than it has been retained"] = expect<EntityIsNotRetainedByOwnerException>(() => { e.Retain(this); e.Release(this); e.Release(this); }); it["throws when retaining twice with same owner"] = expect<EntityIsAlreadyRetainedByOwnerException>(() => { var owner1 = new object(); e.Retain(owner1); e.Retain(owner1); }); it["throws when releasing with unknown owner"] = expect<EntityIsNotRetainedByOwnerException>(() => { var owner = new object(); var unknownOwner = new object(); e.Retain(owner); e.Release(unknownOwner); }); it["throws when releasing with owner which doesn't retain entity anymore"] = expect<EntityIsNotRetainedByOwnerException>(() => { var owner1 = new object(); var owner2 = new object(); e.Retain(owner1); e.Retain(owner2); e.Release(owner2); e.Release(owner2); }); context["events"] = () => { it["doesn't dispatch OnEntityReleased when retaining"] = () => { e.OnEntityReleased += delegate { this.Fail(); }; e.Retain(this); }; it["dispatches OnEntityReleased when retain and release"] = () => { var didDispatch = 0; e.OnEntityReleased += entity => { didDispatch += 1; entity.should_be_same(e); }; e.Retain(this); e.Release(this); didDispatch.should_be(1); }; }; }; context["internal caching"] = () => { context["components"] = () => { IComponent[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponents(); }; it["caches components"] = () => { e.GetComponents().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponents().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced with same component"] = () => { e.ReplaceComponentA(Component.A); e.GetComponents().should_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponents().should_not_be_same(cache); }; }; context["component indices"] = () => { int[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponentIndices(); }; it["caches component indices"] = () => { e.GetComponentIndices().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponentIndices().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponentIndices().should_be_same(cache); }; it["updates cache when adding a new component with ReplaceComponent"] = () => { e.ReplaceComponentC(Component.C); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponentIndices().should_not_be_same(cache); }; }; context["ToString"] = () => { context["when component was added"] = () => { string cache = null; before = () => { e.AddComponentA(); cache = e.ToString(); }; it["caches entity description"] = () => { e.ToString().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.ToString().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.ToString().should_not_be_same(cache); }; it["updates cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.ToString().should_not_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.ToString().should_not_be_same(cache); }; it["updates cache when entity gets retained"] = () => { e.Retain(this); e.ToString().should_not_be_same(cache); }; it["updates cache when entity gets released"] = () => { e.Retain(this); cache = e.ToString(); e.Release(this); e.ToString().should_not_be_same(cache); }; it["released entity has updated cache"] = () => { e.Retain(this); cache = e.ToString(); e.OnEntityReleased += (entity) => { e.ToString().should_not_be_same(cache); }; e.Release(this); }; it["updates cache when RemoveAllComponents is called, even if entity has no components"] = () => { cache = e.ToString(); e.RemoveAllComponents(); e.ToString().should_not_be_same(cache); }; }; }; }; }
void when_throwing() { before = () => { var componentNames = new [] { "Health", "Position", "View" }; var contextInfo = new ContextInfo("My Context", componentNames, null); _context = new Context(componentNames.Length, 42, contextInfo); _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 = () => { _context.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 = _context.contextInfo.componentNames; var group = _context.GetGroup(matcher); group.GetSingleEntity(); }); }; context["Collector"] = () => { it["unbalanced goups"] = () => printErrorMessage(() => { var g1 = new Group(Matcher.AllOf(CID.ComponentA)); var g2 = new Group(Matcher.AllOf(CID.ComponentB)); var e1 = GroupEvent.Added; new Collector(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 Context(1, 0, contextInfo); }); it["destroy entity which is not in context"] = () => printErrorMessage(() => { _context.DestroyEntity(new Entity(0, null)); }); it["destroy retained entities"] = () => printErrorMessage(() => { createEntity().Retain(this); _context.DestroyAllEntities(); }); it["releases entity before destroy"] = () => printErrorMessage(() => { _entity.Release(_context); }); it["unknown entityIndex"] = () => printErrorMessage(() => { _context.GetEntityIndex("unknown"); }); it["duplicate entityIndex"] = () => printErrorMessage(() => { var index = new PrimaryEntityIndex<string>(getGroupA(), null); _context.AddEntityIndex("duplicate", index); _context.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(); _context.CreateEntity().AddComponent(CID.ComponentA, nameAge); _context.CreateEntity().AddComponent(CID.ComponentA, nameAge); }); }; }
public void Initialize(int creationIndex, int totalComponents, Stack <IComponent>[] componentPools, ContextInfo contextInfo = null, IAERC aerc = null) { Reactivate(creationIndex); _totalComponents = totalComponents; _components = new IComponent[totalComponents]; _componentPools = componentPools; _contextInfo = contextInfo ?? createDefaultContextInfo(); _aerc = aerc ?? new SafeAERC(this); }
void when_created() { Context ctx = null; before = () => { ctx = new Context(CID.TotalComponents); }; it["increments creationIndex"] = () => { ctx.CreateEntity().creationIndex.should_be(0); ctx.CreateEntity().creationIndex.should_be(1); }; it["starts with given creationIndex"] = () => { new Context(CID.TotalComponents, 42, null).CreateEntity().creationIndex.should_be(42); }; it["has no entities when no entities were created"] = () => { ctx.GetEntities().should_be_empty(); }; it["gets total entity count"] = () => { ctx.count.should_be(0); }; it["creates entity"] = () => { var e = ctx.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["has default ContextInfo"] = () => { ctx.contextInfo.name.should_be("Unnamed Context"); ctx.contextInfo.componentNames.Length.should_be(CID.TotalComponents); for (int i = 0; i < ctx.contextInfo.componentNames.Length; i++) { ctx.contextInfo.componentNames[i].should_be("Index " + i); } }; it["creates component pools"] = () => { ctx.componentPools.should_not_be_null(); ctx.componentPools.Length.should_be(CID.TotalComponents); }; it["creates entity with component pools"] = () => { var e = ctx.CreateEntity(); e.componentPools.should_be_same(ctx.componentPools); }; it["throws when destroying an entity which the context doesn't contain"] = expect<ContextDoesNotContainEntityException>(() => { var e = ctx.CreateEntity(); ctx.DestroyEntity(e); ctx.DestroyEntity(e); }); it["can ToString"] = () => { ctx.ToString().should_be("Unnamed Context"); }; context["when ContextInfo set"] = () => { ContextInfo contextInfo = null; before = () => { var componentNames = new [] { "Health", "Position", "View" }; var componentTypes = new [] { typeof(ComponentA), typeof(ComponentB), typeof(ComponentC) }; contextInfo = new ContextInfo("My Context", componentNames, componentTypes); ctx = new Context(componentNames.Length, 0, contextInfo); }; it["has custom ContextInfo"] = () => { ctx.contextInfo.should_be_same(contextInfo); }; it["creates entity with same ContextInfo"] = () => { ctx.CreateEntity().contextInfo.should_be_same(contextInfo); }; it["throws when componentNames is not same length as totalComponents"] = expect<ContextInfoException>(() => { new Context(contextInfo.componentNames.Length + 1, 0, contextInfo); }); }; context["when entity created"] = () => { Entity e = null; before = () => { e = ctx.CreateEntity(); e.AddComponentA(); }; it["gets total entity count"] = () => { ctx.count.should_be(1); }; it["has entities that were created with CreateEntity()"] = () => { ctx.HasEntity(e).should_be_true(); }; it["doesn't have entities that were not created with CreateEntity()"] = () => { ctx.HasEntity(this.CreateEntity()).should_be_false(); }; it["returns all created entities"] = () => { var e2 = ctx.CreateEntity(); var entities = ctx.GetEntities(); entities.Length.should_be(2); entities.should_contain(e); entities.should_contain(e2); }; it["destroys entity and removes it"] = () => { ctx.DestroyEntity(e); ctx.HasEntity(e).should_be_false(); ctx.count.should_be(0); ctx.GetEntities().should_be_empty(); }; it["destroys an entity and removes all its components"] = () => { ctx.DestroyEntity(e); e.GetComponents().should_be_empty(); }; it["destroys all entities"] = () => { ctx.CreateEntity(); ctx.DestroyAllEntities(); ctx.HasEntity(e).should_be_false(); ctx.count.should_be(0); ctx.GetEntities().should_be_empty(); e.GetComponents().should_be_empty(); }; it["ensures same deterministic order when getting entities after destroying all entities"] = () => { // This is a Unity specific problem. Run Unity Test Tools with in the Entitas.Unity project const int numEntities = 10; for (int i = 0; i < numEntities; i++) { ctx.CreateEntity(); } var order1 = new int[numEntities]; var entities1 = ctx.GetEntities(); for (int i = 0; i < numEntities; i++) { order1[i] = entities1[i].creationIndex; } ctx.DestroyAllEntities(); ctx.ResetCreationIndex(); for (int i = 0; i < numEntities; i++) { ctx.CreateEntity(); } var order2 = new int[numEntities]; var entities2 = ctx.GetEntities(); for (int i = 0; i < numEntities; i++) { order2[i] = entities2[i].creationIndex; } for (int i = 0; i < numEntities; i++) { var index1 = order1[i]; var index2 = order2[i]; index1.should_be(index2); } }; it["throws when destroying all entities and there are still entities retained"] = expect<ContextStillHasRetainedEntitiesException>(() => { ctx.CreateEntity().Retain(new object()); ctx.DestroyAllEntities(); }); }; context["internal caching"] = () => { it["caches entities"] = () => { var entities = ctx.GetEntities(); ctx.GetEntities().should_be_same(entities); }; it["updates entities cache when creating an entity"] = () => { var entities = ctx.GetEntities(); ctx.CreateEntity(); ctx.GetEntities().should_not_be_same(entities); }; it["updates entities cache when destroying an entity"] = () => { var e = ctx.CreateEntity(); var entities = ctx.GetEntities(); ctx.DestroyEntity(e); ctx.GetEntities().should_not_be_same(entities); }; }; context["events"] = () => { var didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnEntityCreated when creating a new entity"] = () => { Entity eventEntity = null; ctx.OnEntityCreated += (p, entity) => { didDispatch += 1; eventEntity = entity; p.should_be_same(p); }; var e = ctx.CreateEntity(); didDispatch.should_be(1); eventEntity.should_be_same(e); }; it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.OnEntityWillBeDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(ctx); entity.should_be_same(e); entity.HasComponentA().should_be_true(); entity.isEnabled.should_be_true(); p.GetEntities().Length.should_be(0); }; ctx.GetEntities(); ctx.DestroyEntity(e); didDispatch.should_be(1); }; it["dispatches OnEntityDestroyed when destroying an entity"] = () => { var e = ctx.CreateEntity(); ctx.OnEntityDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(ctx); entity.should_be_same(e); entity.HasComponentA().should_be_false(); entity.isEnabled.should_be_false(); }; ctx.DestroyEntity(e); didDispatch.should_be(1); }; it["entity is released after OnEntityDestroyed"] = () => { var e = ctx.CreateEntity(); ctx.OnEntityDestroyed += (p, entity) => { didDispatch += 1; entity.retainCount.should_be(1); var newEntity = ctx.CreateEntity(); newEntity.should_not_be_null(); newEntity.should_not_be_same(entity); }; ctx.DestroyEntity(e); var reusedEntity = ctx.CreateEntity(); reusedEntity.should_be_same(e); didDispatch.should_be(1); }; it["throws if entity is released before it is destroyed"] = expect<EntityIsNotDestroyedException>(() => { var e = ctx.CreateEntity(); e.Release(ctx); }); it["dispatches OnGroupCreated when creating a new group"] = () => { Group eventGroup = null; ctx.OnGroupCreated += (p, g) => { didDispatch += 1; p.should_be_same(ctx); eventGroup = g; }; var group = ctx.GetGroup(Matcher.AllOf(0)); didDispatch.should_be(1); eventGroup.should_be_same(group); }; it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => { ctx.GetGroup(Matcher.AllOf(0)); ctx.OnGroupCreated += delegate { this.Fail(); }; ctx.GetGroup(Matcher.AllOf(0)); }; it["dispatches OnGroupCleared when clearing groups"] = () => { Group eventGroup = null; ctx.OnGroupCleared += (p, g) => { didDispatch += 1; p.should_be_same(ctx); eventGroup = g; }; ctx.GetGroup(Matcher.AllOf(0)); var group2 = ctx.GetGroup(Matcher.AllOf(1)); ctx.ClearGroups(); didDispatch.should_be(2); eventGroup.should_be_same(group2); }; it["removes all external delegates when destroying an entity"] = () => { var e = ctx.CreateEntity(); e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; ctx.DestroyEntity(e); var e2 = ctx.CreateEntity(); e2.should_be_same(e); e2.AddComponentA(); e2.ReplaceComponentA(Component.A); e2.RemoveComponentA(); }; it["will not remove external delegates for OnEntityReleased"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; ctx.DestroyEntity(e); didRelease.should_be(1); }; it["removes all external delegates from OnEntityReleased when after being dispatched"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; ctx.DestroyEntity(e); e.Retain(this); e.Release(this); didRelease.should_be(1); }; it["removes all external delegates from OnEntityReleased after being dispatched (when delayed release)"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; e.Retain(this); ctx.DestroyEntity(e); didRelease.should_be(0); e.Release(this); didRelease.should_be(1); e.Retain(this); e.Release(this); didRelease.should_be(1); }; }; context["entity pool"] = () => { it["gets entity from object pool"] = () => { var e = ctx.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(Entity)); }; it["destroys entity when pushing back to object pool"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); e.HasComponent(CID.ComponentA).should_be_false(); }; it["returns pushed entity"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); var entity = ctx.CreateEntity(); entity.HasComponent(CID.ComponentA).should_be_false(); entity.should_be_same(e); }; it["only returns released entities"] = () => { var e1 = ctx.CreateEntity(); e1.Retain(this); ctx.DestroyEntity(e1); var e2 = ctx.CreateEntity(); e2.should_not_be_same(e1); e1.Release(this); var e3 = ctx.CreateEntity(); e3.should_be_same(e1); }; it["returns new entity"] = () => { var e1 = ctx.CreateEntity(); e1.AddComponentA(); ctx.DestroyEntity(e1); ctx.CreateEntity(); var e2 = ctx.CreateEntity(); e2.HasComponent(CID.ComponentA).should_be_false(); e2.should_not_be_same(e1); }; it["sets up entity from pool"] = () => { ctx.DestroyEntity(ctx.CreateEntity()); var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); var e = ctx.CreateEntity(); e.AddComponentA(); g.GetEntities().should_contain(e); }; context["when entity gets destroyed"] = () => { Entity e = null; before = () => { e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); }; it["throws when adding component"] = expect<EntityIsNotEnabledException>(() => e.AddComponentA()); it["throws when removing component"] = expect<EntityIsNotEnabledException>(() => e.RemoveComponentA()); it["throws when replacing component"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA())); it["throws when replacing component with null"] = expect<EntityIsNotEnabledException>(() => e.ReplaceComponentA(null)); }; }; context["groups"] = () => { it["gets empty group for matcher when no entities were created"] = () => { var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); g.should_not_be_null(); g.GetEntities().should_be_empty(); }; context["when entities created"] = () => { Entity eAB1 = null; Entity eAB2 = null; Entity eA = null; IMatcher matcherAB = Matcher.AllOf(new [] { CID.ComponentA, CID.ComponentB }); before = () => { eAB1 = ctx.CreateEntity(); eAB1.AddComponentA(); eAB1.AddComponentB(); eAB2 = ctx.CreateEntity(); eAB2.AddComponentA(); eAB2.AddComponentB(); eA = ctx.CreateEntity(); eA.AddComponentA(); }; it["gets group with matching entities"] = () => { var g = ctx.GetGroup(matcherAB).GetEntities(); g.Length.should_be(2); g.should_contain(eAB1); g.should_contain(eAB2); }; it["gets cached group"] = () => { ctx.GetGroup(matcherAB).should_be_same(ctx.GetGroup(matcherAB)); }; it["cached group contains newly created matching entity"] = () => { var g = ctx.GetGroup(matcherAB); eA.AddComponentB(); g.GetEntities().should_contain(eA); }; it["cached group doesn't contain entity which are not matching anymore"] = () => { var g = ctx.GetGroup(matcherAB); eAB1.RemoveComponentA(); g.GetEntities().should_not_contain(eAB1); }; it["removes destroyed entity"] = () => { var g = ctx.GetGroup(matcherAB); ctx.DestroyEntity(eAB1); g.GetEntities().should_not_contain(eAB1); }; it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => { var g = ctx.GetGroup(matcherAB); var didDispatchRemoved = 0; var didDispatchAdded = 0; var componentA = new ComponentA(); g.OnEntityRemoved += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); didDispatchRemoved++; }; g.OnEntityAdded += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(componentA); didDispatchAdded++; }; eAB1.ReplaceComponentA(componentA); didDispatchRemoved.should_be(1); didDispatchAdded.should_be(1); }; it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => { var updated = 0; var prevComp = eA.GetComponent(CID.ComponentA); var newComp = new ComponentA(); var g = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; group.should_be_same(g); entity.should_be_same(eA); index.should_be(CID.ComponentA); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; eA.ReplaceComponent(CID.ComponentA, newComp); updated.should_be(1); }; it["group with matcher NoneOf doesn't dispatch OnEntityAdded when destroying entity"] = () => { var e = ctx.CreateEntity() .AddComponentA() .AddComponentB(); var matcher = Matcher.AllOf(CID.ComponentB).NoneOf(CID.ComponentA); var g = ctx.GetGroup(matcher); g.OnEntityAdded += delegate { this.Fail(); }; ctx.DestroyEntity(e); }; context["event timing"] = () => { before = () => { ctx = new Context(CID.TotalComponents); }; it["dispatches group.OnEntityAdded events after all groups are updated"] = () => { var groupA = ctx.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB)); var groupB = ctx.GetGroup(Matcher.AllOf(CID.ComponentB)); groupA.OnEntityAdded += delegate { groupB.count.should_be(1); }; var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); }; it["dispatches group.OnEntityRemoved events after all groups are updated"] = () => { ctx = new Context(CID.TotalComponents); var groupB = ctx.GetGroup(Matcher.AllOf(CID.ComponentB)); var groupAB = ctx.GetGroup(Matcher.AllOf(CID.ComponentA, CID.ComponentB)); groupB.OnEntityRemoved += delegate { groupAB.count.should_be(0); }; var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentB(); }; }; }; }; context["EntityIndex"] = () => { it["throws when EntityIndex for key doesn't exist"] = expect<ContextEntityIndexDoesNotExistException>(() => { ctx.GetEntityIndex("unknown"); }); it["adds and EntityIndex"] = () => { const int componentIndex = 1; var entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(componentIndex)), null); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); ctx.GetEntityIndex(componentIndex.ToString()).should_be_same(entityIndex); }; it["throws when adding an EntityIndex with same name"] = expect<ContextEntityIndexDoesAlreadyExistException>(() => { const int componentIndex = 1; var entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(componentIndex)), null); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); }); }; context["reset"] = () => { context["groups"] = () => { it["resets and removes groups from context"] = () => { var m = Matcher.AllOf(CID.ComponentA); var groupsCreated = 0; Group createdGroup = null; ctx.OnGroupCreated += (p, g) => { groupsCreated += 1; createdGroup = g; }; var initialGroup = ctx.GetGroup(m); ctx.ClearGroups(); ctx.GetGroup(m); ctx.CreateEntity().AddComponentA(); groupsCreated.should_be(2); createdGroup.should_not_be_same(initialGroup); initialGroup.count.should_be(0); createdGroup.count.should_be(1); }; it["removes all event handlers from groups"] = () => { var m = Matcher.AllOf(CID.ComponentA); var group = ctx.GetGroup(m); group.OnEntityAdded += delegate { this.Fail(); }; ctx.ClearGroups(); var e = ctx.CreateEntity(); e.AddComponentA(); group.HandleEntity(e, CID.ComponentA, Component.A); }; it["releases entities in groups"] = () => { var m = Matcher.AllOf(CID.ComponentA); ctx.GetGroup(m); var entity = ctx.CreateEntity(); entity.AddComponentA(); ctx.ClearGroups(); entity.retainCount.should_be(1); }; }; context["context"] = () => { it["resets creation index"] = () => { ctx.CreateEntity(); ctx.ResetCreationIndex(); ctx.CreateEntity().creationIndex.should_be(0); }; context["removes all event handlers"] = () => { it["removes OnEntityCreated"] = () => { ctx.OnEntityCreated += delegate { this.Fail(); }; ctx.Reset(); ctx.CreateEntity(); }; it["removes OnEntityWillBeDestroyed"] = () => { ctx.OnEntityWillBeDestroyed += delegate { this.Fail(); }; ctx.Reset(); ctx.DestroyEntity(ctx.CreateEntity()); }; it["removes OnEntityDestroyed"] = () => { ctx.OnEntityDestroyed += delegate { this.Fail(); }; ctx.Reset(); ctx.DestroyEntity(ctx.CreateEntity()); }; it["removes OnGroupCreated"] = () => { ctx.OnGroupCreated += delegate { this.Fail(); }; ctx.Reset(); ctx.GetGroup(Matcher.AllOf(0)); }; it["removes OnGroupCleared"] = () => { ctx.OnGroupCleared += delegate { this.Fail(); }; ctx.Reset(); ctx.GetGroup(Matcher.AllOf(0)); ctx.ClearGroups(); }; }; }; context["component pools"] = () => { before = () => { var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentA(); entity.RemoveComponentB(); }; it["clears all component pools"] = () => { ctx.componentPools[CID.ComponentA].Count.should_be(1); ctx.componentPools[CID.ComponentB].Count.should_be(1); ctx.ClearComponentPools(); ctx.componentPools[CID.ComponentA].Count.should_be(0); ctx.componentPools[CID.ComponentB].Count.should_be(0); }; it["clears a specific component pool"] = () => { ctx.ClearComponentPool(CID.ComponentB); ctx.componentPools[CID.ComponentA].Count.should_be(1); ctx.componentPools[CID.ComponentB].Count.should_be(0); }; it["only clears existing component pool"] = () => { ctx.ClearComponentPool(CID.ComponentC); }; }; context["EntityIndex"] = () => { PrimaryEntityIndex<string> entityIndex = null; before = () => { entityIndex = new PrimaryEntityIndex<string>(ctx.GetGroup(Matcher.AllOf(CID.ComponentA)), (e, c) => ((NameAgeComponent)(c)).name); ctx.AddEntityIndex(CID.ComponentA.ToString(), entityIndex); }; it["deactivates EntityIndex"] = () => { var nameAgeComponent = new NameAgeComponent(); nameAgeComponent.name = "Max"; ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent); entityIndex.HasEntity("Max").should_be_true(); ctx.DeactivateAndRemoveEntityIndices(); entityIndex.HasEntity("Max").should_be_false(); }; it["removes EntityIndex"] = expect<ContextEntityIndexDoesNotExistException>(() => { ctx.DeactivateAndRemoveEntityIndices(); ctx.GetEntityIndex(CID.ComponentA.ToString()); }); }; }; context["EntitasCache"] = () => { it["pops new list from list pool"] = () => { var groupA = ctx.GetGroup(Matcher.AllOf(CID.ComponentA)); var groupAB = ctx.GetGroup(Matcher.AnyOf(CID.ComponentA, CID.ComponentB)); var groupABC = ctx.GetGroup(Matcher.AnyOf(CID.ComponentA, CID.ComponentB, CID.ComponentC)); var didExecute = 0; groupA.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; entity.RemoveComponentA(); }; groupAB.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; }; groupABC.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; }; ctx.CreateEntity().AddComponentA(); didExecute.should_be(3); }; }; }
public ContextInfoException(Context context, ContextInfo contextInfo) : base("Invalid ContextInfo for '" + context + "'!\nExpected " + context.totalComponents + " componentName(s) but got " + contextInfo.componentNames.Length + ":", string.Join("\n", contextInfo.componentNames)) { }