diff options
Diffstat (limited to 'test/04-test-builder.js')
| -rw-r--r-- | test/04-test-builder.js | 155 |
1 files changed, 134 insertions, 21 deletions
diff --git a/test/04-test-builder.js b/test/04-test-builder.js index f1c0f71..6863e5d 100644 --- a/test/04-test-builder.js +++ b/test/04-test-builder.js @@ -28,13 +28,40 @@ var east = new Rect( new vec(2,6), new vec(1,5) ) var corner = new Rect( new vec(3,7), new vec(3,7) ) var peninsula = new Rect( new vec(4,6), new vec(6,8) ) +var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) +var east_room = new Room({ id: "east", rect: east, height: 2 }) +var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) +var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 }) + +var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) + function report(a) { console.log( a.join("\n") ) } function reportSides(walls) { console.log(walls.map(function(w){ return sidesToString(w.side) }).join("\n")) } +function count_wall_sides (wall_groups) { + var wall_sides = {} + wall_sides[LEFT] = 0 + wall_sides[RIGHT] = 0 + wall_sides[FRONT] = 0 + wall_sides[BACK] = 0 + wall_sides[TOP] = 0 + wall_sides[BOTTOM] = 0 + wall_sides.total = 0 + wall_groups.map(function(walls){ + walls.forEach(function(wall){ + wall_sides[wall.side] += 1 + wall_sides.total += 1 + }) + }) + return wall_sides +} function reset(){ + Rooms.forEach(function(room){ + room.reset() + }) Rooms.list = {} Rooms.regions = [] } @@ -48,7 +75,7 @@ function rebuild(){ describe('builder', function(){ reset() - var rect_room = Rooms.add_with_rect( rect ) + Rooms.add( rect_room ) rebuild() describe('#build_walls(rect)', function(){ @@ -72,42 +99,128 @@ describe('builder', function(){ }) }) - // + // rect vs east reset() - var rect_room = Rooms.add_with_rect( rect ) - var rect_east = Rooms.add_with_rect( east ) + Rooms.add( rect_room ) + Rooms.add( east_room ) var regions = rebuild() -console.log("\n\n") -console.log(regions.join("\n")) -console.log("\n\n") - describe('#build_walls(rect, east)', function(){ - var walls = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) - walls.map(function(w){ reportSides(w); console.log("--") }) + var wall_groups = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) + var wall_sides = count_wall_sides(wall_groups) - it("should return 3 walls", function(){ - assert.equal(4, walls.length) + // reportSides(w); console.log("--") + + it("should return 8 walls", function(){ + assert.equal(8, wall_sides.total) }) - it("should have one side per wall", function(){ - assert.equal(1, bitcount(walls[0].side)) - assert.equal(1, bitcount(walls[1].side)) - assert.equal(1, bitcount(walls[2].side)) - assert.equal(1, bitcount(walls[3].side)) + it("should have 3 front walls", function(){ + assert.equal(3, wall_sides[FRONT]) + }) + it("should have 3 back walls", function(){ + assert.equal(3, wall_sides[BACK]) + }) + it("should have 1 left wall", function(){ + assert.equal(1, wall_sides[LEFT]) + }) + it("should have 1 right wall", function(){ + assert.equal(1, wall_sides[RIGHT]) }) }) describe('#build_floors(rect, east)', function(){ - var floors = Rooms.builder.build_floors(rect_room) - it("should make 2 floors", function(){ - assert.equal(2, floors.length) + var fg = Rooms.builder.build_floors(rect_room) + var fg2 = Rooms.builder.build_floors(east_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 4 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(2, fg2_floors.length) + }) + it("should make 0 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(0, fg2_halves.length) + }) + }) + + // rect vs corner + + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + var regions = rebuild() + + describe('#build_walls(rect, corner)', function(){ + + var wall_groups = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) + var wall_sides = count_wall_sides(wall_groups) + + // reportSides(w); console.log("--") + + it("should return 12 walls", function(){ + assert.equal(12, wall_sides.total) + }) + it("should have 3 front walls", function(){ + assert.equal(3, wall_sides[FRONT]) + }) + it("should have 3 back walls", function(){ + assert.equal(3, wall_sides[BACK]) + }) + it("should have 3 left wall", function(){ + assert.equal(3, wall_sides[LEFT]) + }) + it("should have 3 right wall", function(){ + assert.equal(3, wall_sides[RIGHT]) }) - // reportSides(floors) }) + describe('#build_floors(rect, corner)', function(){ + var fg = Rooms.builder.build_floors(rect_room) + var fg2 = Rooms.builder.build_floors(corner_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + it("should make 4 floors", function(){ + assert.equal(1, fg_floors.length) + assert.equal(3, fg2_floors.length) + }) + it("should make 2 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(2, fg2_halves.length) + }) + }) + + // taller (rect) vs east + + reset() + Rooms.add( taller_room ) + Rooms.add( east_room ) + var regions = rebuild() + + describe('#build_floors(taller, east)', function(){ + var fg = Rooms.builder.build_floors(taller_room) + var fg2 = Rooms.builder.build_floors(east_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 4 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(2, fg2_floors.length) + }) + it("should make 1 half-wall", function(){ + assert.equal(0, fg_halves.length) + assert.equal(1, fg2_halves.length) + }) + }) |
