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

describe("Grid",
    function () {
        let Vector = life.Vector;
        it("initially undefined",
            function () {
                let grid = new life.Grid(5, 5);

                expect(grid.get(new Vector(1, 1))).toBe(undefined);
            });

        it("setting a value",
            function () {
                let grid = new life.Grid(5, 5);
                grid.set(new Vector(1, 1), "X");

                expect(grid.get(new Vector(1, 1))).toEqual("X");
            });

        it("forEach",
            function () {
                let grid = new life.Grid(5, 5);
                let 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(2);
            });
    });

describe("BouncingCritter",
    function () {
        let 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 () {
                let clear = { look: function () { return " "; } };

                expect(bob.act(clear)).toEqual({ type: "move", direction: "s" });
            });

        it("act, unclear path",
            function () {
                let unclear = { look: function () { return "#"; }, find: function () { return "n"; } };

                expect(bob.act(unclear)).toEqual({ type: "move", direction: "n" });
            });
    });

describe("World",
    function () {
        it("roundtrip",
            function () {
                let plan = ["############################",
                    "#      #    #      o      ##",
                    "#                          #",
                    "#          #####           #",
                    "##         #   #    ##     #",
                    "###           ##     #     #",
                    "#           ###      #     #",
                    "#   ####                   #",
                    "#   ##       o             #",
                    "# o  #         o       ### #",
                    "#    #                     #",
                    "############################"];
                let world = new life.World(plan, { "#": life.Wall, "o": life.BouncingCritter });
                let rows = world.toString().split("\n");
                // drop blank row
                rows.pop();

                expect(rows).toEqual(plan);
            });
    });