UNB/ CS/ David Bremner/ teaching/ cs2613/ assignments/ A4/ life.spec.js
var life=require ("./life.js");
var plan = ["############################",
            "#      #    #      o      ##",
            "#                          #",
            "#          #####           #",
            "##         #   #    ##     #",
            "###           ##     #     #",
            "#           ###      #     #",
            "#   ####                   #",
            "#   ##       o             #",
            "# o  #         o       ### #",
            "#    #                     #",
            "############################"];

var Vector = life.Vector;

describe("Grid",
    function() {
        var grid = new life.Grid(5, 5);
        it("initially undefined",
            function() {
                expect(grid.get(new life.Vector(1, 1))).toBe(undefined);
            });
        it("setting a value",
            function() {
                grid.set(new Vector(1, 1), "X");
                expect(grid.get(new Vector(1, 1))).toEqual("X");
            });
        it("forEach",
           function() {
               var test = {grid: grid,  sum: 0,
                           method: function () {
                               this.grid.forEach(function() { this.sum++; }, this);
                           }
                          };

               test.grid.set(new Vector(2,3), "#");
               test.grid.set(new Vector(3,4), "#");
               test.method();
               expect(test.sum).toBe(3);
           });
    });

describe("BouncingCritter",
         function() {
             var bob = null;
             beforeEach(function () {
                 spyOn(Math, 'random').and.returnValue(0.5);
                 bob=new life.BouncingCritter();
             });
             it("constructor",
                function() {
                    expect('direction' in bob).toBe(true);
                    expect(bob.direction).toBe('s');
                });
             it("act, clear path",
                function () {
                    var clear = {look: function () {return " ";}};
                    expect(bob.act(clear)).toEqual({type: "move", direction: "s"});
                });
             it("act, unclear path",
                function () {
                    var unclear = {look: function () {return "#";}, find: function () { return "n";}};
                    expect(bob.act(unclear)).toEqual({type: "move", direction: "n"});
                });
         });

describe("World",
         function () {
             var world = new life.World(plan, {"#": life.Wall, "o": life.BouncingCritter});
             it("roundtrip",
                function() {
                    var rows = world.toString().split("\n");
                    // drop blank row
                    rows.pop();
                    expect(rows).toEqual(plan);
                });
             it("turn",
                function () {
                    var count=0;
                    spyOn(world, 'letAct').and.callFake(function(critter,vector) {count++;});
                    world.turn();
                    expect(count).toBe(4);
                });
             it("checkDestination",
                function () {
                    expect(world.checkDestination({direction: 's'},
                                                  new life.Vector(19,1))).toEqual(new life.Vector(19,2));
                    expect(world.checkDestination({direction: 'n'},
                                                  new life.Vector(0,0))).toEqual(undefined);
                });
             it("letAct",
                function () {
                    var src=new life.Vector(19,1);
                    var dest=new life.Vector(19,2);
                    var bob=world.grid.get(src);
                    spyOn(bob,'act').and.returnValue({type: 'move', direction: 's'});
                    world.letAct(bob, src);
                    expect(world.grid.get(dest)).toEqual(bob);
                });
         });

describe("View",
         function () {
             var world = new life.World(plan, {"#": life.Wall, "o": life.BouncingCritter});
             var View=life.View;
             var position=new Vector(15,9);
             it("constructor",
                function () {
                    var view=new View(world, position);
                    expect(view.vector).toEqual(position);
                });
             it("look",
                function () {
                    var view=new View(world, position);
                    expect(view.look("s")).toBe(" ");
                });
             it("findAll",
                function () {
                    var view=new View(world, position);
                    var directionNames = [ 'e', 'n', 'ne', 'nw', 's', 'se', 'sw', 'w' ];
                    expect(view.findAll(" ").sort()).toEqual(directionNames);
                });
             it("find",
                function () {
                    var view=new View(world, position);
                    spyOn(Math, 'random').and.returnValue(0.5);
                    expect(view.find(" ")).toBe('s');
                });

         });