From 476dbfd6dfbad4897ffc540d396a1f3702da3921 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 29 Aug 2020 11:10:15 +0900 Subject: [PATCH] Add in testing suite --- game.js | 25 +++----- game.test.js | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 2 +- 3 files changed, 167 insertions(+), 18 deletions(-) create mode 100644 game.test.js diff --git a/game.js b/game.js index 64e229e..c4cc7d8 100644 --- a/game.js +++ b/game.js @@ -79,12 +79,11 @@ function createGrid(width,height) { return grid } -function runBot() { +function runBot(testing) { //console.log(new Date().getTime()) - if (lastGameUpdate{ - step() - draw() -},1000/60) -console.log("Running") \ No newline at end of file +} \ No newline at end of file diff --git a/game.test.js b/game.test.js new file mode 100644 index 0000000..eb9ba12 --- /dev/null +++ b/game.test.js @@ -0,0 +1,158 @@ +function loadScript(url, callback) +{ + var head = document.head; + var script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = url; + script.onreadystatechange = callback; + script.onload = callback; + head.appendChild(script); +} + +var testsPass=undefined; + +class describe { + constructor(testname) { + this.testname=testname + this.beforecb = undefined; + this.cb = undefined; + console.log(this.testname) + } + + beforeEach = (cb)=>{ + this.beforecb = cb + return this + } + + it = (checkname,cb)=>{ + console.log("->"+checkname) + this.beforecb() + this.cb=cb; + this.cb() + return this + } +} + +function expect(testval1,testval2,test) { + if (testval1!==testval2) { + console.log(" Test Failed!") + testsPass=false + } else + { + console.log(" Test Passed!") + } +} + +function AllBlankSpaces(level) { + for (var x=0;x{ + gameGrid=[] + gameState=WAITING + BOT_X=-1 + BOT_Y=-1 + BOT_DIR=RIGHT + BOT_STATE=ALIVE + BOT_TAPE=[{color:RED},{color:BLUE}] + lastGameUpdate=0 + }) + .it("Blank level exists.",()=>{ + var starttime = new Date().getTime() + expect(AllBlankSpaces(LEVEL0),true) + console.log(" ("+(new Date().getTime()-starttime)+"ms)") + }) + .it("Bot moves to the right initially.",()=>{ + var starttime = new Date().getTime() + expect(function(){ + gameGrid=createGrid(5,5) + placeBot(0,2) + runBot(true) + if (BOT_X===1&&BOT_Y===2) { + return true + } else { + return false + } + }(),true) + console.log(" ("+(new Date().getTime()-starttime)+"ms)") + }) + .it("Bot obeys conveyor belt rules",()=>{ + var starttime = new Date().getTime() + expect(function(){ + loadLevel(LEVEL1,0,2) + for (var i=0;i<11;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===1) { + return true + } else { + return false + } + }(),true) + console.log(" ("+(new Date().getTime()-starttime)+"ms)") + }) + .it("Bot obeys branch rules",()=>{ + var starttime = new Date().getTime() + expect(function(){ + loadLevel(LEVEL2,0,2) + for (var i=0;i<3;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===3) { + return true + } else { + return false + } + }(),true) + console.log(" ("+(new Date().getTime()-starttime)+"ms)") + }) + .it("Bot obeys branch rules with different colored tape.",()=>{ + var starttime = new Date().getTime() + expect(function(){ + loadLevel(LEVEL2,0,2) + BOT_TAPE = [{color:BLUE}] + for (var i=0;i<3;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===1) { + return true + } else { + return false + } + }(),true) + console.log(" ("+(new Date().getTime()-starttime)+"ms)") + }) + + if (testsPass===undefined) { + testsPass=true + } +} + + +function runGame() { + setupGame(); + loadLevel(LEVEL2,0,2) + setInterval(()=>{ + step() + draw() + },1000/60) + console.log("Running") +} + +loadScript("game.js",runTests) + +initializeGame() + +function initializeGame() { + if (testsPass) { + runGame() + } else { + setTimeout(()=>{ + initializeGame() + },1000) + } +} diff --git a/index.html b/index.html index 8a2cb26..ef8719b 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,4 @@
- + \ No newline at end of file