X-Git-Url: https://git.r.bdr.sh/rbdr/serpentity/blobdiff_plain/e936d6df77f8fe351fdde3fd9be829f9bc3aae97..3f00fec5fc38ff34000721903dd94c30ffc864f3:/test/integration.js?ds=sidebyside diff --git a/test/integration.js b/test/integration.js index 7d88432..70109eb 100644 --- a/test/integration.js +++ b/test/integration.js @@ -1,162 +1,286 @@ -'use strict'; - -let test = function test (Serpentity) { - - /* eslint no-console: 0 */ - - ///////////////// - // Load the stuff - ///////////////// - console.log('\n## Loading'); - console.log('Serpentity: ' + (typeof Serpentity !== 'undefined' ? 'LOAD OK' : 'FAIL')); - console.log('Serpentity.Entity: ' + (typeof Serpentity !== 'undefined' && Serpentity.Entity ? 'LOAD OK' : 'FAIL')); - console.log('Serpentity.Component: ' + (typeof Serpentity !== 'undefined' && Serpentity.Component ? 'LOAD OK' : 'FAIL')); - console.log('Serpentity.System: ' + (typeof Serpentity !== 'undefined' && Serpentity.System ? 'LOAD OK' : 'FAIL')); - console.log('Serpentity.Node: ' + (typeof Serpentity !== 'undefined' && Serpentity.Node ? 'LOAD OK' : 'FAIL')); - console.log('Serpentity.NodeCollection: ' + (typeof Serpentity !== 'undefined' && Serpentity.NodeCollection ? 'LOAD OK' : 'FAIL')); - - ////////////////////// - // Create test classes - ////////////////////// - console.log('\n## Creating Test Classes'); - let TestSystem = class TestSystem extends Serpentity.System { - added (engine) { - this.testNodes = engine.getNodes(TestNode); - console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL')); - console.log('System added callback: EXEC OK'); - } +import Code from '@hapi/code'; // assertion library +import Lab from '@hapi/lab'; +import Serpentity, { Component, Entity, Node, System } from '../lib/serpentity.js'; - removed (engine) { - this.testNodes = null; - console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL')); - console.log('System removed callback: EXEC OK'); - } +const internals = { + context: {}, + system: class TestSystem extends System { + added(engine) { - update (dt) { - this.testNodes.forEach(function (node) { - console.log('Running Node: ' + (node.test.testMessage === 'test' ? 'SYSTEM OK' : 'FAIL')); - }); - console.log('dt is number: ' + (typeof dt === 'number' ? 'OK' : 'FAIL')); - console.log('System update callback: EXEC OK'); - } - }; - let testSystem = new TestSystem(); - - let LowProTestSystem = class LowProTestSystem extends Serpentity.System { - added (engine) { - this.testNodes = engine.getNodes(TestNode); - console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL')); - console.log('System added callback: EXEC OK'); + this.testNodes = engine.getNodes(internals.node); + this.addedCalled = true; + this.addedEngine = engine; } - removed (engine) { + removed(engine) { + this.testNodes = null; - console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL')); - console.log('System removed callback: EXEC OK'); + this.removedCalled = true; + this.removedEngine = engine; } - update (dt) { - this.testNodes.forEach(function (node) { - console.log('Running Low Priority Node: ' + (node.test.testMessage === 'test' ? 'SYSTEM OK' : 'FAIL')); - }); - console.log('dt is number: ' + (typeof dt === 'number' ? 'OK' : 'FAIL')); - console.log('System update callback: EXEC OK'); - } - }; - let lowProTestSystem = new LowProTestSystem(); - console.log('LowProTestSystem: CREATE OK'); - - let MidProTestSystem = class MidProTestSystem extends Serpentity.System { - added (engine) { - this.testNodes = engine.getNodes(TestNode); - console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL')); - console.log('System added callback: EXEC OK'); - } + update(dt) { - removed (engine) { - this.testNodes = null; - console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL')); - console.log('System removed callback: EXEC OK'); - } + this.updateCalled = Date.now(); + this.updateDt = dt; - update (dt) { - this.testNodes.forEach(function (node) { - console.log('Running Mid Priority Node: ' + (node.test.testMessage === 'test' ? 'SYSTEM OK' : 'FAIL')); - }); - console.log('dt is number: ' + (typeof dt === 'number' ? 'OK' : 'FAIL')); - console.log('System update callback: EXEC OK'); - } - }; - var midProTestSystem = new MidProTestSystem(); - console.log('MidProTestSystem: CREATE OK'); + for (const node of this.testNodes) { + node.test.called = true; + node.entity.called = true; + } + while (Date.now() === this.updateCalled) { /* pass some time */ } + } + }, + component: class TestComponent extends Component { + constructor(config) { - let TestComponent = class TestComponent extends Serpentity.Component { - constructor (config) { super(config); - this.testMessage = this.testMessage || 'test'; + this.called = false; } - }; - console.log('TestComponent: CREATE OK'); + }, + node: class TestNode extends Node {}, + delta: 10 +}; - let TestNode = class TestNode extends Serpentity.Node {}; - TestNode.types = { - test : TestComponent - }; - console.log('TestNode: CREATE OK'); +// adds a component to the node +internals.node.types = { + test: internals.component +}; - console.log('\n## Adding system to the engine'); +const lab = Lab.script({ + schedule: false +}); - let engine = new Serpentity(); - console.log('engine: CREATE OK'); +const { beforeEach, experiment, test } = lab; +const { expect } = Code; - engine.addSystem(testSystem, 0); +experiment('loading', () => { - console.log('\n## Running update without any entities'); - engine.update(10); + test('Serpentity should be exported', () => { - console.log('\n## Adding system to the engine and updating'); - let entity = new Serpentity.Entity(); - entity.addComponent(new TestComponent()); - engine.addEntity(entity); - engine.update(10); + expect(typeof Serpentity).to.not.be.undefined(); + }); - console.log('\n## Adding Low Priority System'); - engine.addSystem(lowProTestSystem, 10); - engine.update(10); + test('Serpentity should include the Entity class', () => { - console.log('\n## Adding Mid Priority System'); - engine.addSystem(midProTestSystem, 5); - engine.update(10); + expect(typeof Entity).to.not.be.undefined(); + }); - console.log('\n## Removing the system and readding'); - engine.removeSystem(testSystem); - engine.update(10); - engine.addSystem(testSystem, 0); - engine.update(10); + test('Serpentity should include the Component class', () => { - console.log('\n## Adding a second entity'); - entity = new Serpentity.Entity(); - entity.addComponent(new TestComponent()); - engine.addEntity(entity); - engine.update(10); + expect(typeof Component).to.not.be.undefined(); + }); - console.log('\n## Removing entity'); - engine.removeEntity(entity); - engine.update(10); + test('Serpentity should include the System class', () => { - console.log('\n## Removing system'); - engine.removeSystem(testSystem); - engine.update(10); + expect(typeof System).to.not.be.undefined(); + }); -}; + test('Serpentity should include the Node class', () => { -if (typeof require === 'function') { - let Serpentity = require('serpentity'); - test(Serpentity); -} else { - window.addEventListener('load', function () { - test(window.Serpentity); + expect(typeof Node).to.not.be.undefined(); }); -} + + test('Serpentity should include the NodeCollection class', () => { + + expect(typeof NodeCollection).to.not.be.undefined(); + }); +}); + +experiment('Engine Tests', () => { + + beforeEach(() => { + + internals.context.engine = new Serpentity(); + + internals.context.regularSystem = new internals.system(); + internals.context.highPrioritySystem = new internals.system(); + internals.context.lowPrioritySystem = new internals.system(); + + internals.context.firstEntity = new Entity(); + internals.context.firstEntity.addComponent(new internals.component()); + internals.context.secondEntity = new Entity(); + internals.context.secondEntity.addComponent(new internals.component()); + internals.context.emptyEntity = new Entity(); + + // Add entity before the systems + internals.context.engine.addEntity(internals.context.firstEntity); + + internals.context.engine.addSystem(internals.context.regularSystem, 100); + internals.context.engine.addSystem(internals.context.highPrioritySystem, 0); + internals.context.engine.addSystem(internals.context.lowPrioritySystem, 1000); + + // Add entity after the systems + internals.context.engine.addEntity(internals.context.secondEntity); + internals.context.engine.addEntity(internals.context.emptyEntity); + }); + + test('Engine should call added callback on added systems', () => { + + // Ensure the added callback is being called + expect(internals.context.regularSystem.addedCalled).to.be.true(); + expect(internals.context.highPrioritySystem.addedCalled).to.be.true(); + expect(internals.context.lowPrioritySystem.addedCalled).to.be.true(); + }); + + test('Engine should send the engine instance in added callback', () => { + + // Ensure the added callback is sending the engine + expect(internals.context.regularSystem.addedEngine instanceof Serpentity).to.be.true(); + expect(internals.context.highPrioritySystem.addedEngine instanceof Serpentity).to.be.true(); + expect(internals.context.lowPrioritySystem.addedEngine instanceof Serpentity).to.be.true(); + }); + + test('Engine should not add duplicate systems', () => { + + const originalSystemsLength = internals.context.engine.systems.length; + const added = internals.context.engine.addSystem(internals.context.regularSystem, 0); + const newSystemsLength = internals.context.engine.systems.length; + + // Ensure we don't add the same system twice + expect(added).to.be.false(); + expect(originalSystemsLength).to.be.equal(newSystemsLength); + }); + + test('Engine should call update callback on added systems', () => { + + internals.context.engine.update(internals.delta); + + // Ensure update function called + expect(!!internals.context.regularSystem.updateCalled).to.be.true(); + expect(!!internals.context.highPrioritySystem.updateCalled).to.be.true(); + expect(!!internals.context.lowPrioritySystem.updateCalled).to.be.true(); + }); + + test('Engine should call update callback in the order of priorities', () => { + + internals.context.engine.update(internals.delta); + + // Ensure order of priorities + expect(internals.context.regularSystem.updateCalled).to.be.lessThan(internals.context.lowPrioritySystem.updateCalled); + expect(internals.context.regularSystem.updateCalled).to.be.greaterThan(internals.context.highPrioritySystem.updateCalled); + }); + + test('Engine should send the delta in the update callback', () => { + + internals.context.engine.update(internals.delta); + + // Ensure delta is being sent + expect(internals.context.regularSystem.updateDt).to.be.equal(internals.delta); + expect(internals.context.highPrioritySystem.updateDt).to.be.equal(internals.delta); + expect(internals.context.lowPrioritySystem.updateDt).to.be.equal(internals.delta); + }); + + test('System remove callback', () => { + + const originalSystemLength = internals.context.engine.systems.length; + const originalRemoved = internals.context.engine.removeSystem(internals.context.lowPrioritySystem); + const intermediateSystemLength = internals.context.engine.systems.length; + const finalRemoved = internals.context.engine.removeSystem(internals.context.lowPrioritySystem); + const finalSystemLength = internals.context.engine.systems.length; + internals.context.engine.update(internals.delta); + + // Check for return value + expect(originalRemoved).to.be.true(); + expect(finalRemoved).to.be.false(); + + // Confirm that only removed if found by checking length of systems + // array + expect(originalSystemLength).to.be.above(intermediateSystemLength); + expect(finalSystemLength).to.be.equal(intermediateSystemLength); + + // Ensure callback is sent + expect(!!internals.context.regularSystem.removedCalled).to.be.false(); + expect(!!internals.context.highPrioritySystem.removedCalled).to.be.false(); + expect(!!internals.context.lowPrioritySystem.removedCalled).to.be.true(); + + // Ensure update is no longer sent + expect(!!internals.context.regularSystem.updateCalled).to.be.true(); + expect(!!internals.context.highPrioritySystem.updateCalled).to.be.true(); + expect(!!internals.context.lowPrioritySystem.updateCalled).to.be.false(); + }); + + test('Entity node selection', () => { + + internals.context.engine.update(internals.delta); + + // Ensure component is called for each entity + expect(!!internals.context.firstEntity._components[0].called).to.be.true(); + expect(!!internals.context.secondEntity._components[0].called).to.be.true(); + + // Ensure entity not in node collection not called + expect(!!internals.context.firstEntity.called).to.be.true(); + expect(!!internals.context.secondEntity.called).to.be.true(); + expect(!!internals.context.emptyEntity.called).to.be.false(); + }); + + test('Entity node removal', () => { + + internals.context.engine.removeEntity(internals.context.secondEntity); + internals.context.engine.update(internals.delta); + + expect(!!internals.context.firstEntity._components[0].called).to.be.true(); + expect(!!internals.context.secondEntity._components[0].called).to.be.false(); + + expect(!!internals.context.firstEntity.called).to.be.true(); + expect(!!internals.context.secondEntity.called).to.be.false(); + expect(!!internals.context.emptyEntity.called).to.be.false(); + }); + + test('Entity should not add duplicate components', () => { + + const originalComponentsLength = internals.context.secondEntity._components.length; + const result = internals.context.secondEntity.addComponent(new internals.component()); + const newComponentsLength = internals.context.secondEntity._components.length; + + expect(result).to.be.false(); + expect(originalComponentsLength).to.be.equal(newComponentsLength); + }); + + test('Entity should allow access to components by class', () => { + + const firstComponent = internals.context.firstEntity.getComponent(internals.component); + const emptyComponent = internals.context.emptyEntity.getComponent(internals.component); + + expect(firstComponent instanceof internals.component).to.be.true(); + expect(emptyComponent).to.be.equal(undefined); + }); + + test('Engine should not add duplicate entities', () => { + + const originalEntitiesLength = internals.context.engine.entities.length; + const added = internals.context.engine.addEntity(internals.context.firstEntity); + const finalEntitiesLength = internals.context.engine.entities.length; + + expect(added).to.be.false(); + + expect(originalEntitiesLength).to.be.equal(finalEntitiesLength); + }); + + test('Engine should remove entities', () => { + + const originalEntityLength = internals.context.engine.entities.length; + const originalRemoved = internals.context.engine.removeEntity(internals.context.firstEntity); + const intermediateEntityLength = internals.context.engine.entities.length; + const finalRemoved = internals.context.engine.removeEntity(internals.context.firstEntity); + const finalEntityLength = internals.context.engine.entities.length; + internals.context.engine.update(internals.delta); + + // Check for return value + expect(originalRemoved).to.be.true(); + expect(finalRemoved).to.be.false(); + + // Confirm that only removed if found by checking length of systems + // array + expect(originalEntityLength).to.be.above(intermediateEntityLength); + expect(finalEntityLength).to.be.equal(intermediateEntityLength); + + // Ensure callback is sent + expect(!!internals.context.firstEntity.called).to.be.false(); + expect(!!internals.context.secondEntity.called).to.be.true(); + }); +}); + +Lab.report(lab).then((result) => process.exit(result.code));