diff options
Diffstat (limited to 'test/02-test-rect.js')
| -rw-r--r-- | test/02-test-rect.js | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/test/02-test-rect.js b/test/02-test-rect.js new file mode 100644 index 0000000..39693f5 --- /dev/null +++ b/test/02-test-rect.js @@ -0,0 +1,116 @@ +var assert = require("assert") +var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 +var ALL = FRONT | BACK | LEFT | RIGHT + +describe('rect', function(){ + describe('#intersects()', function(){ + var rect = new Rect(0, 0, 10, 10) + + it('intersects itself', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 10) )); + }) + it('intersects more wide', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 5, 10) )); + }) + it('intersects less wide', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 15, 10) )); + }) + it('intersects more tall', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 5) )); + }) + it('intersects less tall', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 15) )); + }) + it('intersects if right-adjacent', function(){ + assert.equal(true, rect.intersects( new Rect(10, 0, 20, 10) )); + }) + it('intersects if bottom-adjacent', function(){ + assert.equal(true, rect.intersects( new Rect(0, 10, 10, 20) )); + }) + it('does not intersect if to the right', function(){ + assert.equal(false, rect.intersects( new Rect(20, 0, 40, 10) )); + }) + it('does not intersect if beneath', function(){ + assert.equal(false, rect.intersects( new Rect(0, 20, 10, 40) )); + }) + /* + it('does not intersect if corners intersect', function(){ + assert.equal(false, rect.intersects( new Rect(10, 10, 20, 20) )); + }) + */ + + }) + + var rect = new Rect( new vec(1,4), new vec(1,4) ) + + var east_in = new Rect( new vec(2,3), new vec(1,4) ) + var east_edge = new Rect( new vec(2,4), new vec(1,4) ) + var east = new Rect( new vec(2,5), new vec(1,4) ) + + var south_in = new Rect( new vec(1,4), new vec(2,3) ) + var south_edge = new Rect( new vec(1,4), new vec(2,4) ) + var south = new Rect( new vec(1,4), new vec(2,5) ) + + var corner = new Rect( new vec(3,5), new vec(3,5) ) + + function sides (s) { + return s.reduce(function(prev, curr){ + return prev | curr.sides + }, 0) + } + + describe('#split(rect, east)', function(){ + var s0 = rect.split(east) + var s1 = east.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/back/left', function(){ + assert.equal(FRONT | BACK | LEFT, sides(s0)) + }) + it('east is front/back/right', function(){ + assert.equal(FRONT | BACK | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, east_in)', function(){ + var s0 = rect.split(east_in) + var s1 = east_in.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('east_in only has front/back', function(){ + assert.equal(FRONT | BACK, sides(s1)) + }) + }) + + describe('#split(rect, east_edge)', function(){ + var s0 = rect.split(east_edge) + var s1 = east_edge.split(rect) + + console.log("\n") + console.log(rect+"") + console.log(east_edge+"") + console.log(s0.map(function(r){ return r.toString() })) + console.log(s1.map(function(r){ return r.toString() })) + + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/back/left', function(){ + assert.equal(FRONT | BACK | LEFT, sides(s0)) + }) + it('east is front/back/right', function(){ + assert.equal(FRONT | BACK | RIGHT, sides(s1)) + }) + }) + +}) +/* +*/ + |
