Skip to content

Commit

Permalink
Smoke test for game
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Nov 25, 2023
1 parent 797478a commit acb09e2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/run-examples/basic-example/mainGame.wpgm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

object pepita {
var energy = 100
var nombre = "Pepita"
var property image = "pepita.png"
var property position = new MutablePosition(x = 0, y = 1)
var height = 10
var width = 10

method energy() = energy

method eat(grams) {
energy = energy + grams * 10
}

method fly(minutes) {
energy = energy - minutes * 3
}

}

program PepitaGame {
game.addVisualCharacter(pepita)
console.println("starting...")
game.start()
game.onTick(500, "end", {
console.println("finishing...")
game.stop()
})
}
16 changes: 10 additions & 6 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default async function (programFQN: Name, options: Options): Promise<void
const debug = logger.getLevel() <= logger.levels.DEBUG
if (debug) time(successDescription('Run initiated successfully'))


const ioGame: Server | undefined = initializeGameClient(options)
const interpreter = game ? getGameInterpreter(environment, ioGame!) : interpret(environment, { ...natives })
const programPackage = environment.getNodeByFQN<Package>(programFQN).parent as Package
Expand Down Expand Up @@ -188,16 +189,19 @@ export const eventsFor = (io: Server, interpreter: Interpreter, dynamicDiagramCl
socket.emit('sizeCanvasInic', [sizeCanvas.width, sizeCanvas.height])

const id = setInterval(() => {
const game = interpreter?.object('wollok.game.game')
socket.emit('cellPixelSize', game.get('cellSize')!.innerNumber!)
const gameSingleton = interpreter?.object('wollok.game.game')
socket.emit('cellPixelSize', gameSingleton.get('cellSize')!.innerNumber!)

Check warning on line 193 in src/commands/run.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/run.ts#L192-L193

Added lines #L192 - L193 were not covered by tests
try {
interpreter.send('flushEvents', game, interpreter.reify(timer))
interpreter.send('flushEvents', gameSingleton, interpreter.reify(timer))

Check warning on line 195 in src/commands/run.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/run.ts#L195

Added line #L195 was not covered by tests
timer += 300
// We can pass the interpreter but a program does not change its interpreter
// We can pass the interpreter but a program does not change it
dynamicDiagramClient.onReload()
if (!game.get('running')) { clearInterval(id) }
if (!gameSingleton.get('running')?.innerBoolean) {
clearInterval(id)
process.exit(0)

Check warning on line 201 in src/commands/run.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/run.ts#L198-L201

Added lines #L198 - L201 were not covered by tests
}
} catch (error: any) {
interpreter.send('stop', game)
interpreter.send('stop', gameSingleton)
socket.emit('errorDetected', error.message)

Check warning on line 205 in src/commands/run.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/run.ts#L204-L205

Added lines #L204 - L205 were not covered by tests
clearInterval(id)
}
Expand Down
28 changes: 26 additions & 2 deletions test/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import chai from 'chai'
import { join } from 'path'
import run, { getSoundsFolder, getAssetsFolder } from '../src/commands/run'
import { mkdirSync, rmdirSync } from 'fs'
import { join } from 'path'
import sinon from 'sinon'
import run, { getAssetsFolder, getSoundsFolder } from '../src/commands/run'
import { spyCalledWithSubstring } from './assertions'

chai.should()
Expand Down Expand Up @@ -94,4 +94,28 @@ describe('testing run', () => {
})
})

describe('run a simple game', () => {

let clock: sinon.SinonFakeTimers

beforeEach(() => {
clock = sinon.useFakeTimers()
})

afterEach(() => {
sinon.restore()
})


it ('smoke teset - should work if program has no errors', async () => {
run('mainGame.PepitaGame', {
project: join('examples', 'run-examples', 'basic-example'),
skipValidations: false,
game: true,
startDiagram: true,
})
await clock.runAllAsync()
})
})

})

0 comments on commit acb09e2

Please sign in to comment.