Newer
Older
Labyrinth / src / MazeMap.spec.ts
import {expect} from "chai";
import { Direction, MazeMap, MazeTile, MazeValidationError, TileValidationError } from "./MazeMap";
// import { describe, it } from "mocha";

describe("Scenario: validate a single tile", function(){
    describe("When it is valid", function(){
        const tile = new MazeTile("id");
        tile.paths.push(Direction.right);
        const result = tile.validate();
        it("then it returns an empty array.", function(){
            expect(result).to.not.be.null;
            expect(Array.isArray(result)).to.be.true;
            expect(result.length).to.equal(0);
        });
    });

    describe("When id is null", function(){
        const tile = new MazeTile(null);
        const result = tile.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(TileValidationError.InvalidID);
        });
    });

    describe("When id is empty", function(){
        const tile = new MazeTile("");
        const result = tile.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(TileValidationError.InvalidID);
        });
    });

    describe("When no path way is given", function(){
        const tile = new MazeTile("id");
        const result = tile.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(TileValidationError.NoPathways);
        });
    });

    describe("When the only path is \"none\"", function(){
        const tile = new MazeTile("id");
        tile.paths.push(Direction.none);
        const result = tile.validate();
        it("then it is valid", function(){
            expect(result.length).to.equal(0);
        });
    });
});

function createMinimalisticMaze(): MazeMap
{
    const maze = new MazeMap("id");
    let tile = new MazeTile("1");
    tile.paths.push(Direction.right);
    maze.layout.push([tile]);
    tile = new MazeTile("2");
    tile.paths.push(Direction.left);
    maze.layout[0].push(tile);
    return maze;
}

describe("Scenario: validate a whole maze", function(){
    describe("When it is valid", function(){
        const maze = createMinimalisticMaze();
        const result = maze.validate();
        it("then it returns an empty array.", function(){
            expect(result).to.not.be.null;
            expect(Array.isArray(result)).to.be.true;
            expect(result.length).to.equal(0);
        });
    });

    describe("When the id is null", function(){
        const maze = createMinimalisticMaze();
        maze.id = null;
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.InvalidID);
        });
    });

    describe("When the id is empty", function(){
        const maze = createMinimalisticMaze();
        maze.id = "";
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.InvalidID);
        });
    });

    describe("When one tile is invalid", function(){
        const maze = createMinimalisticMaze();
        maze.layout[0][0].id = null;
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.InvalidTiles);
        });
    });

    describe("When two tiles have the same id", function(){
        const maze = createMinimalisticMaze();
        maze.layout[0][1].id = maze.layout[0][0].id;
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.TileIdsNotUnique);
        });
    });

    describe("When it is not rectangular shaped", function(){
        const maze = createMinimalisticMaze();
        const tile = new MazeTile("3");
        tile.paths.push(Direction.none);
        maze.layout.push([tile]);
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.WrongShape);
        });
    });

    describe("When it has a null tile", function(){
        const maze = createMinimalisticMaze();
        const tile = new MazeTile("3");
        tile.paths.push(Direction.none);
        maze.layout.push([tile, null]);
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.NoHolesAllowed);
        });
    });

    describe("When it has a null row at the end", function(){
        const maze = createMinimalisticMaze();
        maze.layout.push(null);
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.NoHolesAllowed);
        });
    });

    describe("When a path leaves the map on the top side", function(){
        const maze = createMinimalisticMaze();
        maze.layout[0][0].paths.push(Direction.top);
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.PathWithoutDestination);
        });
    });

    describe("When a path leaves the map on the left side", function(){
        const maze = createMinimalisticMaze();
        maze.layout[0][0].paths.push(Direction.left);
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.PathWithoutDestination);
        });
    });

    describe("When a path leaves the map on the bottom side", function(){
        const maze = createMinimalisticMaze();
        maze.layout[0][0].paths.push(Direction.bottom);
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.PathWithoutDestination);
        });
    });

    describe("When a path leaves the map on the right side", function(){
        const maze = createMinimalisticMaze();
        maze.layout[0][1].paths.push(Direction.right);
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.PathWithoutDestination);
        });
    });

    describe("When a path does not have an opposite on the other side", function(){
        const maze = createMinimalisticMaze();
        maze.layout[0][1].paths[0] = Direction.none;
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.PathWithoutDestination);
        });
    });

    describe("When the maze is empty", function(){
        const maze = new MazeMap("id");
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.WrongShape);
        });
    });

    describe("When the start position is outside the map", function(){
        const maze = createMinimalisticMaze();
        maze.startPosition[0] = -1;
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.StartPositionNotOnMap);
        });
    });

    describe("When the start position has no pathways", function(){
        const maze = createMinimalisticMaze();
        const tile = new MazeTile("3");
        tile.paths.push(Direction.none);
        maze.layout[0].push(tile);
        maze.startPosition[1] = 2;
        const result = maze.validate();
        it("then it is invalid", function(){
            expect(result.length).to.be.greaterThan(0);
            expect(result).to.contain(MazeValidationError.InvalidStartPosition);
        });
    });
});