diff --git a/README.md b/README.md index 18cb656b..7150b147 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,8 @@ These files for the exercises are intended to be run as jest tests. - Once you have cloned the repository, run `npm install` once in the terminal to install jest (and any necessary dependencies). - To run all exercises/tests in the mandatory folder, run `npm test` - To run a single exercise/test (for example `mandatory/1-writer.js`), run `npm test -- --testPathPattern mandatory/1-writer.js` (Remember, you can use tab-completion to get files relative to the current directory, so m`Tab ↹`/1-`Tab ↹` will autocomplete get you the test file starting with 1-) -- Some of the exercises do not use jest. To run these individually, use node directly. `node mandatory/11-choose-your-own-adventure.js`. These are: - - `4-water-bottle.js` +- Some of the exercises do not use jest. To run these individually, use node directly. `node mandatory/7-recipes.js`. These are: - `7-recipes.js` - - `11-choose-your-own-adventure.js` For more information about tests, look here: diff --git a/mandatory/11-choose-your-own-adventure.js b/mandatory/11-choose-your-own-adventure.js index 1086bc0d..65585254 100644 --- a/mandatory/11-choose-your-own-adventure.js +++ b/mandatory/11-choose-your-own-adventure.js @@ -3,9 +3,18 @@ Create a "Choose Your Own Adventure" game using an object. In these kind of games, the player is in a room and can move to other rooms to the north, east, south or west. -To start the game, run this file with Node as usual. To stop the game, press +To start the game, run this file with Node. Depending on your current directory, run one of: + + node 11-choose-your-own-adventure.js + node mandatory/11-choose-your-own-adventure.js + +To stop the game, press Ctrl-C. +To run the tests for the game, run this file with npm test + + npm test -- --testPathPattern 11-choose-your-own-adventure.js + It has a currentRoom property to store which room the player is in. Give your object methods for: @@ -33,7 +42,9 @@ time to read it carefully. The rooms look something like this: Stretch goal: what happens if you try to move in a direction that the current room doesn't allow? For example if you are in the Classroom and you try to move -east? If there is a bug in your code, try to fix it. +east? If there is a bug in your code, try to fix it. + +To enable the tests for the stretch goals, remove the ".skip" on the appropriate tests below. */ let game = { @@ -150,4 +161,86 @@ function play(method) { ); } -start(); +if (global["test"] == undefined) { + // running in node -> start game + start(); + test = () => {}; + beforeEach = () => {}; + test.skip = () => {}; +} else { + // running in jest + // don't start game, close the readline handle + rl.close(); +} + +// if we reach here, we are running in jest -> run tests + +/* ======= TESTS - ONLY MODIFY TO ENABLE TESTS FOR STRETCH GOALS ===== +- To run the tests for this exercise, run `npm test -- --testPathPattern 11-choose-your-own-adventure.js` +- To run all exercises/tests in the mandatory folder, run `npm test` +- (Reminder: You must have run `npm install` one time before this will work!) +*/ + +beforeEach(() => { + // reset the game object + game.currentRoom = null; +}); + +test("start in hall", () => { + game.start("hall"); + expect(game.currentRoom.name).toEqual("hall"); +}); + +test("start in library", () => { + game.start("library"); + expect(game.currentRoom.name).toEqual("library"); +}); + +test("start in classroom", () => { + game.start("classroom"); + expect(game.currentRoom.name).toEqual("classroom"); +}); + +// remove ".skip" if your code correctly handles a non existent room (by setting currentRoom to null/doing nothing) +test.skip("start in non-existent place", () => { + game.start("does not exist"); + expect(game.currentRoom).toEqual(null); +}); + +test("start in hall and go south", () => { + game.currentRoom = rooms.hall; + game.move("south"); + expect(game.currentRoom.name).toEqual("library"); +}); + +test("start in library and go north", () => { + game.currentRoom = rooms.library; + game.move("north"); + expect(game.currentRoom.name).toEqual("hall"); +}); + +test("start in hall and go east", () => { + game.currentRoom = rooms.hall; + game.move("east"); + expect(game.currentRoom.name).toEqual("classroom"); +}); + +test("start in classroom and go west", () => { + game.currentRoom = rooms.classroom; + game.move("west"); + expect(game.currentRoom.name).toEqual("hall"); +}); + +// remove ".skip" if your code handles trying to go in a direction with no room (by staying in the same room) +test.skip("start in hall and go north (to non-existent room) -> stay in same room", () => { + game.currentRoom = rooms.hall; + game.move("north"); + expect(game.currentRoom.name).toEqual("hall"); +}); + +// remove ".skip" if your code handles trying to go in a direction that doesn't exist (by staying in the same room) +test.skip("start in hall and go backwards (non-existent direction) -> stay in same room", () => { + game.currentRoom = rooms.hall; + game.move("backwards"); + expect(game.currentRoom.name).toEqual("hall"); +}); diff --git a/mandatory/4-water-bottle.js b/mandatory/4-water-bottle.js index 0301c25d..4e914d11 100644 --- a/mandatory/4-water-bottle.js +++ b/mandatory/4-water-bottle.js @@ -58,200 +58,95 @@ Once you have completed your object run the following and see if your answer matches the expected result at the bottom :) */ -// ONLY READ AND DO NOT MODIFY BELOW - -// ACTIONS -let failed = false; -bottle.fillUp(); - -// CHECKS -if (bottle.isFull()) { - console.log(`That's correct! Bottle is full.`); -} else { - failed = true; - console.warn(`Not quite right! Bottle should be full but it is not.`); -} - -if (!bottle.isEmpty()) { - console.log(`That's correct! Bottle isn't empty.`); -} else { - failed = true; - console.warn( - `Not quite right! Bottle should not be empty but it is already.` - ); -} - -// ACTIONS -bottle.pour(); - -// CHECKS -if (bottle.volume === 100) { - console.log( - `That's correct. Bottle is already full water volume cannot go beyond.` - ); -} else { - failed = true; - console.warn( - `Whoops!!! Looks like you've changed your bottle to a bigger one, it went beyond its maximum capacity up to ${bottle.volume} unit.` - ); -} - -if (bottle.isFull()) { - console.log(`That's correct! Bottle is still full.`); -} else { - failed = true; - console.warn(`Not quite right! Bottle should be still full but is not.`); -} - -// ACTIONS -bottle.drink(); -bottle.drink(); -bottle.drink(); - -// CHECKS -if (bottle.volume === 70) { - console.log(`That's correct! Water volume is ${bottle.volume}.`); -} else { - failed = true; - console.warn( - `Not quite right! Water volume should be 70 unit instead of ${bottle.volume}.` - ); -} - -// ACTIONS -bottle.drink(); -bottle.drink(); -bottle.drink(); - -// CHECKS -if (!bottle.isFull()) { - console.log(`That's correct! Bottle isn't full.`); -} else { - failed = true; - console.warn(`Not quite right! Bottle should not be full but it is.`); -} - -if (!bottle.isEmpty()) { - console.log(`That's correct! Bottle isn't empty yet.`); -} else { - failed = true; - - console.warn( - `Not quite right! Bottle should not be still empty but it is already.` - ); -} - -// ACTIONS -bottle.pour(); - -// CHECKS -if (bottle.volume === 50) { - console.log(`That's correct! Water volume is ${bottle.volume}.`); -} else { - failed = true; - console.warn( - `Not quite right! Water volume should be 50 unit instead of ${bottle.volume}.` - ); -} - -// ACTIONS -bottle.drink(); -bottle.drink(); -bottle.drink(); -bottle.drink(); -bottle.drink(); - -// CHECKS -if (bottle.isEmpty()) { - console.log(`That's correct! Bottle is finally emptied.`); -} else { - failed = true; - - console.warn( - `Not quite right. Bottle should be already empty but it is not.` - ); -} - -if (bottle.volume === 0) { - console.log(`That's correct! Empty bottle volume is repesented as zero.`); -} else { - failed = true; - - console.warn( - `Not quite right. Volume should be zero instead of ${bottle.volume}.` - ); -} - -// ACTIONS -bottle.drink(); - -// CHECKS -if (bottle.volume === 0) { - console.log(`That's correct! Water volume cannot go below zero.`); -} else { - failed = true; - - console.warn( - `Whoops!!! Looks like your water volume went negative. Your water volume is ${bottle.volume} unit.` - ); -} - -if (bottle.isEmpty()) { - console.log(`That's correct! Bottle is still empty.`); -} else { - console.warn(`Not quite right. Bottle should be empty but it is not.`); -} - -// ACTIONS -bottle.pour(); - -// CHECKS -if (bottle.volume === 10) { - console.log(`That's correct! Water volume is ${bottle.volume}.`); -} else { - failed = true; - - console.warn( - `Not quite right! Water volume should be 10 unit instead of ${bottle.volume}.` - ); -} - -if (!bottle.isFull()) { - console.log(`That's correct! Bottle isn't yet full.`); -} else { - failed = true; - - console.warn(`Not quite right! Bottle should not be full but it is.`); -} - -if (!bottle.isEmpty()) { - console.log(`That's correct! Bottle isn't empty anymore.`); -} else { - failed = true; - - console.warn( - `Not quite right! Bottle should not be empty again but it is still.` - ); -} - -// ACTIONS -bottle.drink(); - -// CHECKS -if (bottle.isEmpty()) { - console.log(`That's correct! Bottle is emptied once more.`); -} else { - failed = true; - - console.warn(`Not quite right. Bottle should be empty again but it is not.`); -} - -console.log(""); +/* ======= TESTS - DO NOT MODIFY ===== +- To run the tests for this exercise, run `npm test -- --testPathPattern 4-water-bottle.js` +- To run all exercises/tests in the mandatory folder, run `npm test` +- (Reminder: You must have run `npm install` one time before this will work!) +*/ -if (failed) { - console.log( - "RESULT: Incorrect. Please read what went wrong above and try again" - ); -} else { - console.log("RESULT: Correct! Congratulations!"); -} +test("When filled up, bottle is full", () => { + bottle.volume = 0; + bottle.fillUp(); + expect(bottle.isFull()).toEqual(true); +}); + +test("When filled up, bottle is not empty", () => { + bottle.volume = 0; + bottle.fillUp(); + expect(bottle.isEmpty()).toEqual(false); +}); + +test("When emptied, bottle is not full", () => { + bottle.volume = 0; + expect(bottle.isFull()).toEqual(false); +}); + +test("When emptied, bottle is empty", () => { + bottle.volume = 0; + expect(bottle.isEmpty()).toEqual(true); +}); + +test("When partially filled, bottle is not empty", () => { + bottle.volume = 40; // arbitrary amount + expect(bottle.isEmpty()).toEqual(false); +}); + +test("When partially filled, bottle is not full", () => { + bottle.volume = 40; // arbitrary amount + expect(bottle.isFull()).toEqual(false); +}); + +test("Given a full bottle, when pour is called, then the volume does not increase", () => { + bottle.volume = 100; + bottle.pour(); + expect(bottle.volume).toEqual(100); +}); + +test("Multiple calls to drink reduce the volume correctly", () => { + bottle.volume = 100; + // arbitrary number of calls to drink + bottle.drink(); + bottle.drink(); + bottle.drink(); + expect(bottle.volume).toEqual(70); +}); + +test("Given a full bottle, when drink has been called, then it is neither full nor empty", () => { + bottle.volume = 100; + // arbitrary number of calls to drink + bottle.drink(); + bottle.drink(); + bottle.drink(); + bottle.drink(); + bottle.drink(); + bottle.drink(); + expect(bottle.isEmpty()).toEqual(false); + expect(bottle.isFull()).toEqual(false); +}); + +test("Given a full bottle, when drink called 10 times, then bottle is empty", () => { + bottle.volume = 100; + for (var i = 0; i < 10; i++) { + bottle.drink(); + } + expect(bottle.isEmpty()).toEqual(true); +}); + +test("Given an empty bottle, when drink is called, then the volume does not decrease", () => { + bottle.volume = 0; + bottle.drink(); + expect(bottle.volume).toEqual(0); +}); + +test("Given an empty bottle, when pour is called, then the volume increases", () => { + bottle.volume = 0; + bottle.pour(); + expect(bottle.volume).toEqual(10); +}); + +test("Given an empty bottle, calling pour then drink, then the bottle is empty", () => { + bottle.volume = 0; + bottle.pour(); + bottle.drink(); + expect(bottle.volume).toEqual(0); +}); \ No newline at end of file