void when_created() { ReactiveSystemSpy system = null; before = () => { _context = new MyTestContext(); }; context["OnEntityAdded"] = () => { before = () => { system = new ReactiveSystemSpy(_context.CreateCollector <TestEntity>(_matcherAB)); }; it["does not execute when no entities were collected"] = () => { system.Execute(); assertEntities(system, null); }; it["executes when triggered"] = () => { var e = createEntityAB(); system.Execute(); assertEntities(system, e); }; it["executes only once when triggered"] = () => { var e = createEntityAB(); system.Execute(); system.Execute(); assertEntities(system, e); }; it["retains and releases collected entities"] = () => { var e = createEntityAB(); var retainCount = e.retainCount; system.Execute(); retainCount.should_be(3); // retained by context, group and collector e.retainCount.should_be(2); // retained by context and group }; it["collects changed entities in execute"] = () => { var e = createEntityAB(); system.executeAction = entities => { entities[0].ReplaceComponentA(Component.A); }; system.Execute(); system.Execute(); assertEntities(system, e, 2); }; it["collects created entities in execute"] = () => { var e1 = createEntityAB(); TestEntity e2 = null; system.executeAction = entities => { if (e2 == null) { e2 = createEntityAB(); } }; system.Execute(); assertEntities(system, e1); system.Execute(); assertEntities(system, e2, 2); }; it["doesn't execute when not triggered"] = () => { _context.CreateEntity().AddComponentA(); system.Execute(); assertEntities(system, null); }; it["deactivates and will not trigger"] = () => { system.Deactivate(); createEntityAB(); system.Execute(); assertEntities(system, null); }; it["activates and will trigger again"] = () => { system.Deactivate(); system.Activate(); var e = createEntityAB(); system.Execute(); assertEntities(system, e); }; it["clears"] = () => { createEntityAB(); system.Clear(); system.Execute(); assertEntities(system, null); }; it["can ToString"] = () => { system.ToString().should_be("ReactiveSystem(ReactiveSystemSpy)"); }; }; context["OnEntityRemoved"] = () => { before = () => { system = new ReactiveSystemSpy(_context.CreateCollector(_matcherAB, GroupEvent.Removed)); }; it["executes when triggered"] = () => { var e = createEntityAB() .RemoveComponentA(); system.Execute(); assertEntities(system, e); }; it["executes only once when triggered"] = () => { var e = createEntityAB() .RemoveComponentA(); system.Execute(); system.Execute(); assertEntities(system, e); }; it["doesn't execute when not triggered"] = () => { createEntityAB() .AddComponentC() .RemoveComponentC(); system.Execute(); assertEntities(system, null); }; it["retains entities until execute completed"] = () => { var e = createEntityAB(); var didExecute = 0; system.executeAction = entities => { didExecute += 1; entities[0].retainCount.should_be(1); }; _context.DestroyEntity(e); system.Execute(); didExecute.should_be(1); e.retainCount.should_be(0); }; }; context["OnEntityAddedOrRemoved"] = () => { before = () => { system = new ReactiveSystemSpy(_context.CreateCollector(_matcherAB, GroupEvent.AddedOrRemoved)); }; it["executes when added"] = () => { var e = createEntityAB(); system.Execute(); assertEntities(system, e); }; it["executes when removed"] = () => { var e = createEntityAB(); system.Execute(); e.RemoveComponentA(); system.Execute(); assertEntities(system, e, 2); }; }; context["multiple contexts"] = () => { IContext <TestEntity> context1 = null; IContext <TestEntity> context2 = null; before = () => { context1 = new MyTestContext(); context2 = new MyTestContext(); var groupA = context1.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA)); var groupB = context2.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentB)); var groups = new [] { groupA, groupB }; var groupEvents = new [] { GroupEvent.Added, GroupEvent.Removed }; var collector = new Collector <TestEntity>(groups, groupEvents); system = new ReactiveSystemSpy(collector); }; it["executes when a triggered by collector"] = () => { var eA1 = context1.CreateEntity().AddComponentA(); context2.CreateEntity().AddComponentA(); var eB1 = context1.CreateEntity().AddComponentB(); var eB2 = context2.CreateEntity().AddComponentB(); system.Execute(); assertEntities(system, eA1); eB1.RemoveComponentB(); eB2.RemoveComponentB(); system.Execute(); assertEntities(system, eB2, 2); }; }; context["filter entities"] = () => { it["filters entities"] = () => { system = new ReactiveSystemSpy(_context.CreateCollector(_matcherAB), e => ((NameAgeComponent)e.GetComponent(CID.ComponentA)).age > 42); _context.CreateEntity() .AddComponentA() .AddComponentC(); var eAB1 = _context.CreateEntity(); eAB1.AddComponentB(); eAB1.AddComponent(CID.ComponentA, new NameAgeComponent { age = 10 }); var eAB2 = _context.CreateEntity(); eAB2.AddComponentB(); eAB2.AddComponent(CID.ComponentA, new NameAgeComponent { age = 50 }); var didExecute = 0; system.executeAction = entities => { didExecute += 1; eAB2.retainCount.should_be(3); // retained by context, group and collector }; system.Execute(); didExecute.should_be(1); system.Execute(); system.entities.Length.should_be(1); system.entities[0].should_be_same(eAB2); eAB1.retainCount.should_be(2); // retained by context and group eAB2.retainCount.should_be(2); }; }; context["clear"] = () => { it["clears reactive system after execute"] = () => { system = new ReactiveSystemSpy(_context.CreateCollector(_matcherAB)); system.executeAction = entities => { entities[0].ReplaceComponentA(Component.A); }; var e = createEntityAB(); system.Execute(); system.Clear(); system.Execute(); assertEntities(system, e); }; }; }
void when_systems() { Contexts contexts = null; before = () => { contexts = new Contexts { test = new Context(10) }; }; context["fixtures"] = () => { it["initializes InitializeSystemSpy"] = () => { var system = new InitializeSystemSpy(); system.didInitialize.should_be(0); system.Initialize(); system.didInitialize.should_be(1); }; it["executes ExecuteSystemSpy"] = () => { var system = new ExecuteSystemSpy(); system.didExecute.should_be(0); system.Execute(); system.didExecute.should_be(1); }; it["cleans up CleanupSystemSpy"] = () => { var system = new CleanupSystemSpy(); system.didCleanup.should_be(0); system.Cleanup(); system.didCleanup.should_be(1); }; it["tears down TearDownSystemSpy"] = () => { var system = new TearDownSystemSpy(); system.didTearDown.should_be(0); system.TearDown(); system.didTearDown.should_be(1); }; it["initializes, executes, cleans up and tears down system"] = () => { var system = new ReactiveSystemSpy(contexts.test.CreateCollector(Matcher.AllOf(CID.ComponentA))); contexts.test.CreateEntity().AddComponentA(); system.didInitialize.should_be(0); system.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); system.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); system.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); system.TearDown(); system.didTearDown.should_be(1); }; it["executes ReactiveSystemSpy"] = () => { var system = createReactiveSystem(contexts); system.Execute(); system.entities.Length.should_be(1); }; }; context["systems"] = () => { Systems systems = null; before = () => { systems = new Systems(); }; it["returns systems when adding system"] = () => { systems.Add(new InitializeSystemSpy()).should_be_same(systems); }; it["initializes IInitializeSystem"] = () => { var system = new InitializeSystemSpy(); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); }; it["executes IExecuteSystem"] = () => { var system = new ExecuteSystemSpy(); systems.Add(system); systems.Execute(); system.didExecute.should_be(1); }; it["wraps IReactiveSystem in a ReactiveSystem"] = () => { var system = new ReactiveSystemSpy(contexts.test.CreateCollector(Matcher.AllOf(CID.ComponentA))); systems.Add(system); contexts.test.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; it["adds ReactiveSystem"] = () => { var system = new ReactiveSystemSpy(contexts.test.CreateCollector(Matcher.AllOf(CID.ComponentA))); systems.Add(system); contexts.test.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; it["cleans up ICleanupSystem"] = () => { var system = new CleanupSystemSpy(); systems.Add(system); systems.Cleanup(); system.didCleanup.should_be(1); }; it["initializes, executes, cleans up and tears down InitializeExecuteCleanupTearDownSystemSpy"] = () => { var system = new ReactiveSystemSpy(contexts.test.CreateCollector(Matcher.AllOf(CID.ComponentA))); contexts.test.CreateEntity().AddComponentA(); systems.Add(system); system.didInitialize.should_be(0); systems.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); systems.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); systems.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); systems.TearDown(); system.didTearDown.should_be(1); }; it["initializes, executes, cleans up and tears down ReactiveSystem"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); system.didInitialize.should_be(0); systems.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); systems.Execute(); systems.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); systems.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); systems.TearDown(); system.didTearDown.should_be(1); }; it["initializes, executes, cleans up and tears down systems recursively"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); system.didInitialize.should_be(0); parentSystems.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); parentSystems.Execute(); parentSystems.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); parentSystems.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); parentSystems.TearDown(); system.didTearDown.should_be(1); }; it["clears reactive systems"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); systems.ClearReactiveSystems(); systems.Execute(); system.didExecute.should_be(0); }; it["clears reactive systems recursively"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); parentSystems.Initialize(); system.didInitialize.should_be(1); parentSystems.ClearReactiveSystems(); parentSystems.Execute(); system.didExecute.should_be(0); }; it["deactivates reactive systems"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); systems.DeactivateReactiveSystems(); systems.Execute(); system.didExecute.should_be(0); }; it["deactivates reactive systems recursively"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); parentSystems.Initialize(); system.didInitialize.should_be(1); parentSystems.DeactivateReactiveSystems(); parentSystems.Execute(); system.didExecute.should_be(0); }; it["activates reactive systems"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); systems.DeactivateReactiveSystems(); systems.ActivateReactiveSystems(); systems.Execute(); system.didExecute.should_be(0); contexts.test.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; it["activates reactive systems recursively"] = () => { var system = createReactiveSystem(contexts); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); parentSystems.Initialize(); system.didInitialize.should_be(1); parentSystems.DeactivateReactiveSystems(); parentSystems.ActivateReactiveSystems(); parentSystems.Execute(); system.didExecute.should_be(0); contexts.test.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; }; }
void when_created() { ReactiveSystemSpy system = null; before = () => { _contexts = new Contexts { test = new Context(CID.TotalComponents) }; }; context["OnEntityAdded"] = () => { before = () => { system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB)); }; it["does not execute when no entities were collected"] = () => { system.Execute(); assertEntities(system, null); }; it["executes when triggered"] = () => { var e = createEntityAB(); system.Execute(); assertEntities(system, e); }; it["executes only once when triggered"] = () => { var e = createEntityAB(); system.Execute(); system.Execute(); assertEntities(system, e); }; it["retains and releases collected entities"] = () => { var e = createEntityAB(); var retainCount = e.retainCount; system.Execute(); retainCount.should_be(3); // retained by context, group and collector e.retainCount.should_be(2); // retained by context and group }; it["collects changed entities in execute"] = () => { var e = createEntityAB(); system.executeAction = entities => { entities[0].ReplaceComponentA(Component.A); }; system.Execute(); system.Execute(); assertEntities(system, e, 2); }; it["collects created entities in execute"] = () => { var e1 = createEntityAB(); Entity e2 = null; system.executeAction = entities => { if(e2 == null) { e2 = createEntityAB(); } }; system.Execute(); assertEntities(system, e1); system.Execute(); assertEntities(system, e2, 2); }; it["doesn't execute when not triggered"] = () => { _contexts.test.CreateEntity().AddComponentA(); system.Execute(); assertEntities(system, null); }; it["deactivates and will not trigger"] = () => { system.Deactivate(); createEntityAB(); system.Execute(); assertEntities(system, null); }; it["activates and will trigger again"] = () => { system.Deactivate(); system.Activate(); var e = createEntityAB(); system.Execute(); assertEntities(system, e); }; it["clears"] = () => { createEntityAB(); system.Clear(); system.Execute(); assertEntities(system, null); }; it["can ToString"] = () => { system.ToString().should_be("ReactiveSystem(ReactiveSystemSpy)"); }; }; context["OnEntityRemoved"] = () => { before = () => { system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB, GroupEvent.Removed)); }; it["executes when triggered"] = () => { var e = createEntityAB() .RemoveComponentA(); system.Execute(); assertEntities(system, e); }; it["executes only once when triggered"] = () => { var e = createEntityAB() .RemoveComponentA(); system.Execute(); system.Execute(); assertEntities(system, e); }; it["doesn't execute when not triggered"] = () => { createEntityAB() .AddComponentC() .RemoveComponentC(); system.Execute(); assertEntities(system, null); }; it["retains entities until execute completed"] = () => { var e = createEntityAB(); var didExecute = 0; system.executeAction = entities => { didExecute += 1; entities[0].retainCount.should_be(1); }; _contexts.test.DestroyEntity(e); system.Execute(); didExecute.should_be(1); e.retainCount.should_be(0); }; }; context["OnEntityAddedOrRemoved"] = () => { before = () => { system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB, GroupEvent.AddedOrRemoved)); }; it["executes when added"] = () => { var e = createEntityAB(); system.Execute(); assertEntities(system, e); }; it["executes when removed"] = () => { var e = createEntityAB(); system.Execute(); e.RemoveComponentA(); system.Execute(); assertEntities(system, e, 2); }; }; context["multiple contexts"] = () => { Context context1 = null; Context context2 = null; before = () => { context1 = new Context(CID.TotalComponents); context2 = new Context(CID.TotalComponents); var groupA = context1.GetGroup(Matcher.AllOf(CID.ComponentA)); var groupB = context2.GetGroup(Matcher.AllOf(CID.ComponentB)); var groups = new [] { groupA, groupB }; var groupEvents = new [] { GroupEvent.Added, GroupEvent.Removed }; var collector = new Collector(groups, groupEvents); system = new ReactiveSystemSpy(collector); }; it["executes when a triggered by collector"] = () => { var eA1 = context1.CreateEntity().AddComponentA(); context2.CreateEntity().AddComponentA(); var eB1 = context1.CreateEntity().AddComponentB(); var eB2 = context2.CreateEntity().AddComponentB(); system.Execute(); assertEntities(system, eA1); eB1.RemoveComponentB(); eB2.RemoveComponentB(); system.Execute(); assertEntities(system, eB2, 2); }; }; context["filter entities"] = () => { it["filters entities"] = () => { system = new ReactiveSystemSpy( _contexts.test.CreateCollector(_matcherAB), e => ((NameAgeComponent)e.GetComponent(CID.ComponentA)).age > 42); _contexts.test.CreateEntity() .AddComponentA() .AddComponentC(); var eAB1 = _contexts.test.CreateEntity() .AddComponentB() .AddComponent(CID.ComponentA, new NameAgeComponent { age = 10 }); var eAB2 = _contexts.test.CreateEntity() .AddComponentB() .AddComponent(CID.ComponentA, new NameAgeComponent { age = 50 }); var didExecute = 0; system.executeAction = entities => { didExecute += 1; eAB2.retainCount.should_be(3); // retained by context, group and collector }; system.Execute(); didExecute.should_be(1); system.Execute(); system.entities.Length.should_be(1); system.entities[0].should_be_same(eAB2); eAB1.retainCount.should_be(2); // retained by context and group eAB2.retainCount.should_be(2); }; }; context["clear"] = () => { it["clears reactive system after execute"] = () => { system = new ReactiveSystemSpy(_contexts.test.CreateCollector(_matcherAB)); system.executeAction = entities => { entities[0].ReplaceComponentA(Component.A); }; var e = createEntityAB(); system.Execute(); system.Clear(); system.Execute(); assertEntities(system, e); }; }; }
void when_systems() { MyTestContext ctx = null; before = () => { ctx = new MyTestContext(); }; context["fixtures"] = () => { it["initializes InitializeSystemSpy"] = () => { var system = new InitializeSystemSpy(); system.didInitialize.should_be(0); system.Initialize(); system.didInitialize.should_be(1); }; it["executes ExecuteSystemSpy"] = () => { var system = new ExecuteSystemSpy(); system.didExecute.should_be(0); system.Execute(); system.didExecute.should_be(1); }; it["cleans up CleanupSystemSpy"] = () => { var system = new CleanupSystemSpy(); system.didCleanup.should_be(0); system.Cleanup(); system.didCleanup.should_be(1); }; it["tears down TearDownSystemSpy"] = () => { var system = new TearDownSystemSpy(); system.didTearDown.should_be(0); system.TearDown(); system.didTearDown.should_be(1); }; it["initializes, executes, cleans up and tears down system"] = () => { var system = new ReactiveSystemSpy(ctx.CreateCollector(Matcher <TestEntity> .AllOf(CID.ComponentA))); ctx.CreateEntity().AddComponentA(); system.didInitialize.should_be(0); system.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); system.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); system.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); system.TearDown(); system.didTearDown.should_be(1); }; it["executes ReactiveSystemSpy"] = () => { var system = createReactiveSystem(ctx); system.Execute(); system.entities.Length.should_be(1); }; }; context["systems"] = () => { Systems systems = null; before = () => { systems = new Systems(); }; it["returns systems when adding system"] = () => { systems.Add(new InitializeSystemSpy()).should_be_same(systems); }; it["initializes IInitializeSystem"] = () => { var system = new InitializeSystemSpy(); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); }; it["executes IExecuteSystem"] = () => { var system = new ExecuteSystemSpy(); systems.Add(system); systems.Execute(); system.didExecute.should_be(1); }; it["wraps IReactiveSystem in a ReactiveSystem"] = () => { var system = new ReactiveSystemSpy(ctx.CreateCollector(Matcher <TestEntity> .AllOf(CID.ComponentA))); systems.Add(system); ctx.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; it["adds ReactiveSystem"] = () => { var system = new ReactiveSystemSpy(ctx.CreateCollector(Matcher <TestEntity> .AllOf(CID.ComponentA))); systems.Add(system); ctx.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; it["cleans up ICleanupSystem"] = () => { var system = new CleanupSystemSpy(); systems.Add(system); systems.Cleanup(); system.didCleanup.should_be(1); }; it["initializes, executes, cleans up and tears down InitializeExecuteCleanupTearDownSystemSpy"] = () => { var system = new ReactiveSystemSpy(ctx.CreateCollector(Matcher <TestEntity> .AllOf(CID.ComponentA))); ctx.CreateEntity().AddComponentA(); systems.Add(system); system.didInitialize.should_be(0); systems.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); systems.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); systems.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); systems.TearDown(); system.didTearDown.should_be(1); }; it["initializes, executes, cleans up and tears down ReactiveSystem"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); system.didInitialize.should_be(0); systems.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); systems.Execute(); systems.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); systems.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); systems.TearDown(); system.didTearDown.should_be(1); }; it["initializes, executes, cleans up and tears down systems recursively"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); system.didInitialize.should_be(0); parentSystems.Initialize(); system.didInitialize.should_be(1); system.didExecute.should_be(0); parentSystems.Execute(); parentSystems.Execute(); system.didExecute.should_be(1); system.didCleanup.should_be(0); parentSystems.Cleanup(); system.didCleanup.should_be(1); system.didTearDown.should_be(0); parentSystems.TearDown(); system.didTearDown.should_be(1); }; it["clears reactive systems"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); systems.ClearReactiveSystems(); systems.Execute(); system.didExecute.should_be(0); }; it["clears reactive systems recursively"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); parentSystems.Initialize(); system.didInitialize.should_be(1); parentSystems.ClearReactiveSystems(); parentSystems.Execute(); system.didExecute.should_be(0); }; it["deactivates reactive systems"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); systems.DeactivateReactiveSystems(); systems.Execute(); system.didExecute.should_be(0); }; it["deactivates reactive systems recursively"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); parentSystems.Initialize(); system.didInitialize.should_be(1); parentSystems.DeactivateReactiveSystems(); parentSystems.Execute(); system.didExecute.should_be(0); }; it["activates reactive systems"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); systems.Initialize(); system.didInitialize.should_be(1); systems.DeactivateReactiveSystems(); systems.ActivateReactiveSystems(); systems.Execute(); system.didExecute.should_be(0); ctx.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; it["activates reactive systems recursively"] = () => { var system = createReactiveSystem(ctx); systems.Add(system); var parentSystems = new Systems(); parentSystems.Add(systems); parentSystems.Initialize(); system.didInitialize.should_be(1); parentSystems.DeactivateReactiveSystems(); parentSystems.ActivateReactiveSystems(); parentSystems.Execute(); system.didExecute.should_be(0); ctx.CreateEntity().AddComponentA(); systems.Execute(); system.didExecute.should_be(1); }; }; }