diff --git a/README.MD b/README.MD index 150ee94..f45dede 100644 --- a/README.MD +++ b/README.MD @@ -2,7 +2,7 @@ ========= ## About -*Labyrinth* is a web api to navigate through a maze. +*Labyrinth* is a web API to navigate through a maze. # How to run diff --git a/README.MD b/README.MD index 150ee94..f45dede 100644 --- a/README.MD +++ b/README.MD @@ -2,7 +2,7 @@ ========= ## About -*Labyrinth* is a web api to navigate through a maze. +*Labyrinth* is a web API to navigate through a maze. # How to run diff --git a/package.json b/package.json index 690e23f..7d1c926 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "labyrinth", "version": "0.1.0", - "description": "Labyrinth =========", + "description": "Labyrinth is a web API to navigate through a maze.", "main": "app.js", "scripts": { "test": "tsc && ./node_modules/mocha/bin/mocha dist/**/*.spec.js" diff --git a/README.MD b/README.MD index 150ee94..f45dede 100644 --- a/README.MD +++ b/README.MD @@ -2,7 +2,7 @@ ========= ## About -*Labyrinth* is a web api to navigate through a maze. +*Labyrinth* is a web API to navigate through a maze. # How to run diff --git a/package.json b/package.json index 690e23f..7d1c926 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "labyrinth", "version": "0.1.0", - "description": "Labyrinth =========", + "description": "Labyrinth is a web API to navigate through a maze.", "main": "app.js", "scripts": { "test": "tsc && ./node_modules/mocha/bin/mocha dist/**/*.spec.js" diff --git a/src/GameServer.spec.ts b/src/GameServer.spec.ts index 76c8775..743c0ff 100644 --- a/src/GameServer.spec.ts +++ b/src/GameServer.spec.ts @@ -58,12 +58,52 @@ expect(position).to.be.null; }); }); + + describe("When userID is null", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(null); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When userID is undefined", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(undefined); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When userID is an empty string", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(""); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); + + describe("When userID is a whitespace string", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(" "); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); }); describe("Scenario: Starting the game", function(){ const userID = "UserID"; const mapID = "lvl1"; - describe("when a user starts an existing valid level", function(){ + describe("When a user starts an existing valid level", function(){ const games = createGameServer(); const position = games.startGame(userID, mapID); @@ -76,6 +116,46 @@ expect(current.id).to.equal(position.id); }); }); + + describe("When the supplied userID is null", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame(null, mapID); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When the supplied userID is undefined", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame(undefined, mapID); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When the supplied userID is an empty string", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame("", mapID); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); + + describe("When the supplied userID has only white spaces", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame(" ", mapID); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); }); describe("Scenario: Navigating in ongoing game", function(){ diff --git a/README.MD b/README.MD index 150ee94..f45dede 100644 --- a/README.MD +++ b/README.MD @@ -2,7 +2,7 @@ ========= ## About -*Labyrinth* is a web api to navigate through a maze. +*Labyrinth* is a web API to navigate through a maze. # How to run diff --git a/package.json b/package.json index 690e23f..7d1c926 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "labyrinth", "version": "0.1.0", - "description": "Labyrinth =========", + "description": "Labyrinth is a web API to navigate through a maze.", "main": "app.js", "scripts": { "test": "tsc && ./node_modules/mocha/bin/mocha dist/**/*.spec.js" diff --git a/src/GameServer.spec.ts b/src/GameServer.spec.ts index 76c8775..743c0ff 100644 --- a/src/GameServer.spec.ts +++ b/src/GameServer.spec.ts @@ -58,12 +58,52 @@ expect(position).to.be.null; }); }); + + describe("When userID is null", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(null); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When userID is undefined", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(undefined); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When userID is an empty string", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(""); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); + + describe("When userID is a whitespace string", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.currentPosition(" "); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); }); describe("Scenario: Starting the game", function(){ const userID = "UserID"; const mapID = "lvl1"; - describe("when a user starts an existing valid level", function(){ + describe("When a user starts an existing valid level", function(){ const games = createGameServer(); const position = games.startGame(userID, mapID); @@ -76,6 +116,46 @@ expect(current.id).to.equal(position.id); }); }); + + describe("When the supplied userID is null", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame(null, mapID); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When the supplied userID is undefined", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame(undefined, mapID); + } + expect(expectedToFail).to.throw(/.*userid.*/i); + }); + }); + + describe("When the supplied userID is an empty string", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame("", mapID); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); + + describe("When the supplied userID has only white spaces", function(){ + const games = createGameServer(); + it("then it should throw an exception", function(){ + function expectedToFail(){ + games.startGame(" ", mapID); + } + expect(expectedToFail).to.throw(/.*userid.*empty.*/i); + }); + }); }); describe("Scenario: Navigating in ongoing game", function(){ diff --git a/src/GameServer.ts b/src/GameServer.ts index 04a0818..5884e9d 100644 --- a/src/GameServer.ts +++ b/src/GameServer.ts @@ -55,6 +55,7 @@ public startGame(userID: string, mapID: string): MazeTile { + this.userIdGuard(userID); const map = this.mapList.find(m => m.id === mapID); if(map == null){ throw new Error(`Map "${mapID}" does not exist.`); @@ -78,6 +79,8 @@ public navigate(userID: string, direction:Direction): MazeTile { + this.userIdGuard(userID); + const state = this.states.find(s => s.userID === userID); if(state == null){ return null; @@ -109,6 +112,8 @@ public currentPosition(userID: string): MazeTile { + this.userIdGuard(userID); + const state = this.states.find(s => s.userID === userID); if(state == null){ return null; @@ -118,4 +123,19 @@ const tile = map.getTile(state.tileID); return tile; } + + /** + * Makes sure, that a userID is acceptable. + * If not throw the appropriate error. + * @param userID supplied userID + */ + private userIdGuard(userID: string): void + { + if(userID === undefined || userID === null){ + throw new Error("UserID must be supplied!"); + } + if(userID === "" || /^\s+$/.test(userID)){ + throw new Error("UserID must not be empty."); + } + } }