Add in testing suite

master
sigonasr2 4 years ago
parent b3396a365e
commit 476dbfd6df
  1. 25
      game.js
  2. 158
      game.test.js
  3. 2
      index.html

@ -79,12 +79,11 @@ function createGrid(width,height) {
return grid
}
function runBot() {
function runBot(testing) {
//console.log(new Date().getTime())
if (lastGameUpdate<new Date().getTime()) {
//console.log("Update")
if (lastGameUpdate<new Date().getTime()||testing) {
lastGameUpdate=new Date().getTime()+gameSpeed
//console.log("Update")
var nextSquare = {}
switch (BOT_DIR) {
case UP:{nextSquare = gameGrid[--BOT_Y][BOT_X];}break;
@ -97,13 +96,13 @@ function runBot() {
if (nextSquare.type==="BRANCH") {
//console.log("Branch found")
if (BOT_TAPE[0].color===nextSquare.color1) {
console.log("Matches color1")
//console.log("Matches color1")
//Move towards left side of the branch.
BOT_DIR = LeftOf(nextSquare.direction)
ConsumeTape()
} else
if (BOT_TAPE[0].color===nextSquare.color2) {
console.log("Matches color2")
//console.log("Matches color2")
//Move towards left side of the branch.
BOT_DIR = RightOf(nextSquare.direction)
ConsumeTape()
@ -111,12 +110,12 @@ function runBot() {
} else {
BOT_DIR = nextSquare.direction
}
console.log("Direction is now "+BOT_DIR)
//console.log("Direction is now "+BOT_DIR)
} else {
gameState = REVIEWING
BOT_STATE = DEAD
}
renderGame()
if (!testing){renderGame()}
}
}
@ -184,12 +183,4 @@ function LeftOf(dir) {
function RightOf(dir) {
if (dir===0) {dir=4}
return (dir-1)%4
}
setupGame();
loadLevel(LEVEL2,0,2)
setInterval(()=>{
step()
draw()
},1000/60)
console.log("Running")
}

@ -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<level.length;x++) {
for (var y=0;y<level.length;y++) {
if (Object.keys(level[y][x]).length!==0) {
return false;
}
}
}
return true;
}
function runTests() {
console.log("Running test suite...")
new describe("Bot moving")
.beforeEach(()=>{
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)
}
}

@ -1,4 +1,4 @@
<body>
<div id="game"></div>
<script type="text/javascript" src="game.js"></script>
<script type="text/javascript" src="game.test.js"></script>
</body>
Loading…
Cancel
Save