From 37d0c7225fc10936e4c20455a9096a6d20124db8 Mon Sep 17 00:00:00 2001 From: Dan Mosedale Date: Thu, 24 May 2012 16:04:15 -0700 Subject: [PATCH] Clean up some of the tests and add a bunch more --- src/core/space.test.js | 101 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 11 deletions(-) diff --git a/src/core/space.test.js b/src/core/space.test.js index c1b67b1..6e6354f 100644 --- a/src/core/space.test.js +++ b/src/core/space.test.js @@ -7,6 +7,7 @@ define( setup: function() {}, teardown: function() {} }); + test("Constructing a space with new clock", function(){ expect(6); @@ -21,6 +22,7 @@ define( ok(mySpace.clock instanceof Clock); }); + test("Constructing a space without a clock", function(){ expect(6); @@ -33,21 +35,24 @@ define( ok(mySpace.clock instanceof Clock); }); - test("Add entities works normally", function(){ - expect(8); + + // XXX split up into add/remove test & tagged test + test("basic adding, removing, and finding entities works", function(){ + expect(6); var clock = new Clock(); var mySpace = new Space(clock); - var entityDupe1 = new Entity("duplicate", [], ["uniqueTag1", "commonTag"]); - var entityDupe2 = new Entity("duplicate", [], ["uniqueTag2", "commonTag"]); - var uniqueNameEntity = new Entity("unique", [], ["commonTag"]); + var entityDupe1 = new Entity("duplicate", [], ["uniqueTag1"]); + var entityDupe2 = new Entity("duplicate", [], []); + var uniqueNameEntity = new Entity("unique", [], []); var emptyEntity = new Entity(); mySpace.add(emptyEntity); equal(mySpace.size, 1, "empty entity added properly"); mySpace.add(uniqueNameEntity); - equal(mySpace.findNamed("unique"), uniqueNameEntity, "single entity added properly"); + equal(mySpace.findNamed("unique"), uniqueNameEntity, + "single entity added properly"); mySpace.add(entityDupe1); mySpace.add(entityDupe2); @@ -56,12 +61,23 @@ define( equal(mySpace.findNamed("duplicate").tags[0], "uniqueTag1", "make sure that findNamed returns the first of duplicate names"); - deepEqual(mySpace.findAllTagged("commonTag"), [uniqueNameEntity, entityDupe1, entityDupe2], - "find all tagged returns the correct items"); + mySpace.remove(uniqueNameEntity); + mySpace.remove(emptyEntity); + mySpace.remove(entityDupe1); + mySpace.remove(entityDupe2); + equal(mySpace.size, 0, + "removing all added entities results in empty space"); - equal(mySpace.findTagged("uniqueTag1"), entityDupe1, - "find tagged returns the first of multiple entities with the same tag"); + mySpace.remove(uniqueNameEntity); // already removed + ok(true, "removing an entity that is not in a space does not throw"); + }); + + test("add and remove handle entities with children", function() { + expect(2); + var clock = new Clock(); + var mySpace = new Space(clock); + var parentEntity = new Entity("parent", [], []); var childEntity = new Entity("child", [], [], parentEntity); @@ -72,9 +88,72 @@ define( mySpace.remove(parentEntity); equal(mySpace.findNamed("child"), null, "space recursively removes child entities"); + }); - //Find entities with a given component type + test("finding tagged entities", function() { + expect(6); + var clock = new Clock(); + var mySpace = new Space(clock); + + var entity1 = new Entity("entity1", [], ["tag1"]); + var entity2 = new Entity("entity2", [], ["tag2", "tag1"]); + + equal(mySpace.findTagged("tag1"), null, + "findTagged should return null if no entities present"); + + deepEqual(mySpace.findAllWith("tag1"), [], + "findAllTagged should return empty array if no entities present"); + + mySpace.add(entity1); + mySpace.add(entity2); + + // note that these tests enforce return in the order entities added + deepEqual(mySpace.findTagged("tag1"), entity1, + "findTagged should return the first entity added of a given type"); + deepEqual(mySpace.findAllTagged("tag1"), [entity1, entity2], + "findAllTagged should return all entities of a given type in order"); + + equal(mySpace.findTagged("NoSuchTag"), null, + "findTagged should return null if no entities of given type present"); + deepEqual(mySpace.findAllTagged("NoSuchTag"), [], + "findAllTagged should return [] if no entities of given type present"); + }); + + + test("finding entities with a type of component", function() { + expect(6); + var clock = new Clock(); + var mySpace = new Space(clock); + + var component1 = { + type: "Type1", + dependsOn: [], + setOwner: function() {} + }; + + var entity1 = new Entity("entity1", [component1], []); + var entity2 = new Entity("entity2", [component1], []); + + equal(mySpace.findWith("Type1"), null, + "findWith should return null if no entities present"); + + deepEqual(mySpace.findAllWith("Type1"), [], + "findAllWith should return empty array if no entities present"); + + mySpace.add(entity1); + mySpace.add(entity2); + + // note that these tests enforce return in the order entities added + deepEqual(mySpace.findWith("Type1"), entity1, + "findWith should return the first entity added of a given type"); + deepEqual(mySpace.findAllWith("Type1"), [entity1, entity2], + "findAllWith should return all entities of a given type in order"); + + equal(mySpace.findWith("NoSuchType"), null, + "findWith should return null if no entities of given type present"); + deepEqual(mySpace.findAllWith("NoSuchType"), [], + "findAllWith should return [] if no entities of given type present"); }); };