Skip to content

Commit

Permalink
Clean up some of the tests and add a bunch more
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Mosedale committed May 24, 2012
1 parent 2522ba0 commit 37d0c72
Showing 1 changed file with 90 additions and 11 deletions.
101 changes: 90 additions & 11 deletions src/core/space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ define(
setup: function() {},
teardown: function() {}
});


test("Constructing a space with new clock", function(){
expect(6);
Expand All @@ -21,6 +22,7 @@ define(
ok(mySpace.clock instanceof Clock);
});


test("Constructing a space without a clock", function(){
expect(6);

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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");
});

};
Expand Down

0 comments on commit 37d0c72

Please sign in to comment.