UNB/ CS/ David Bremner/ teaching/ cs2613/ labs/ L13/ life.spec.js
var life=require ("./life.js");

describe("Grid",
         function() {
             var Vector = life.Vector;
             var grid = new life.Grid(5, 5);
             it("initially undefined",
                function() {
                    expect(grid.get(new 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 () {
             it("roundtrip",
                function() {
                    var plan = ["############################",
                                "#      #    #      o      ##",
                                "#                          #",
                                "#          #####           #",
                                "##         #   #    ##     #",
                                "###           ##     #     #",
                                "#           ###      #     #",
                                "#   ####                   #",
                                "#   ##       o             #",
                                "# o  #         o       ### #",
                                "#    #                     #",
                                "############################"];
                    var world = new life.World(plan, {"#": life.Wall, "o": life.BouncingCritter});
                    var rows = world.toString().split("\n");
                    // drop blank row
                    rows.pop();
                    expect(rows).toEqual(plan);
                });
         });