/*global $, before, be_false, be_true, describe, equal, expect, it, Map, Screw, Sprite, Tyler, TYLERCONFIG */ Screw.Unit(function(){ describe('Tyler class', function(){ var tyler; before(function(){ tyler = new Tyler(); }); describe('keydown', function(){ var hero; before(function(){ var map = new Map(); map.add('@pass').box(-1, -1, 1, 1); tyler.hero(hero = new Sprite({x:0, y:0, speed:1})); hero.map(map); }); it('returns true for arrow keys', function(){ expect(tyler.keydown(37, hero)).to(be_true); expect(tyler.keydown(38, hero)).to(be_true); expect(tyler.keydown(39, hero)).to(be_true); expect(tyler.keydown(40, hero)).to(be_true); }); it('returns false for unrecognized keys', function(){ expect(tyler.keydown(255, hero)).to(be_false); }); it('on "up", moves the hero north', function(){ tyler.keydown(38, hero); tyler.update(); expect(hero.x()).to(equal, 0); expect(hero.y()).to(equal, -1 / TYLERCONFIG.framesPerSecond); }); it('on "right", moves the hero east', function(){ tyler.keydown(39, hero); tyler.update(); expect(hero.x()).to(equal, 1 / TYLERCONFIG.framesPerSecond); expect(hero.y()).to(equal, 0); }); it('on "down", moves the hero south', function(){ tyler.keydown(40, hero); tyler.update(); expect(hero.x()).to(equal, 0); expect(hero.y()).to(equal, 1 / TYLERCONFIG.framesPerSecond); }); it('on "left", moves the hero west', function(){ tyler.keydown(37, hero); tyler.update(); expect(hero.x()).to(equal, -1 / TYLERCONFIG.framesPerSecond); expect(hero.y()).to(equal, 0); }); }); describe('map()', function(){ var map; var viewport; var wasDrawMapCalled; before(function(){ map = new Map(); viewport = $('
'); tyler.renderer({drawMap:function(){wasDrawMapCalled=true;}}); wasDrawMapCalled = false; }); it('can be set and read', function(){ tyler.map(map); expect(tyler.map()).to(equal, map); }); it('when set, renders the map', function(){ map.add('foo').at(0, 0); tyler.map(map); expect(wasDrawMapCalled).to(be_true); }); describe('interaction with hero', function(){ var hero; var heroMap; before(function(){ hero = {map: function(value){heroMap = value;}}; heroMap = null; }); it('when hero exists, changing map tells hero about map', function(){ tyler.hero(hero); tyler.map(map); expect(heroMap).to(equal, map); }); it('when map exists, changing hero tells hero about map', function(){ tyler.map(map); tyler.hero(hero); expect(heroMap).to(equal, map); }); }); }); }); });