Skip to content

Commit

Permalink
Merge pull request #183 from uqbar-project/update-exeption-handler
Browse files Browse the repository at this point in the history
Update exception handler for game
  • Loading branch information
PalumboN authored May 23, 2024
2 parents 78d2bf0 + 87c3761 commit 54159db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
23 changes: 13 additions & 10 deletions src/wollok/game.wlk
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import wollok.vm.runtime
*/
object game {

/** Collection of visual objects in the game */
const visuals = []
/** Is Game running? */
var property running = false
/**
* Allows to configure a visual component as "error reporter".
* Then every error in game board will be reported by this visual component,
* in a balloon message form.
*/
var property errorReporter = null

override method initialize() {
super()
Expand Down Expand Up @@ -212,8 +220,10 @@ object game {
*/
method start() {
self.running(true)
io.exceptionHandler({ exception => console.println(exception)})
io.domainExceptionHandler({exception => self.say(exception.source(), exception.message())})
io.exceptionHandler({ exception => exception.printStackTrace() })
io.domainExceptionHandler({ exception =>
const reporter = if (errorReporter == null) exception.source() else errorReporter
self.say(reporter, exception.message())})
}

/**
Expand Down Expand Up @@ -298,14 +308,7 @@ object game {
* Default behavior is to show them, so this is not necessary.
*/
method showAttributes(visual) native

/**
* Allows to configure a visual component as "error reporter".
* Then every error in game board will be reported by this visual component,
* in a balloon message form.
*/
method errorReporter(visual) native


/**
* Returns a sound object. Audio file must be a .mp3, .ogg or .wav file.
*/
Expand Down
28 changes: 21 additions & 7 deletions test/game/actions.wpgm
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@ object visual {
method position() = game.origin()
}

program addVisual {
object reporter {
method position() = game.origin()
}

program say {
game.addVisual(visual)
game.start()
game.say(visual, "Hi!")
}

program removeVisual {
program domainError {
game.addVisual(visual)
game.removeVisual(visual)
new Tick(interval = 1, action = { visual.error("DOMAIN_ERROR") }).start()
game.start()
game.flushEvents(1)
}

program say {
program domainErrorWithReporter {
game.addVisual(visual)
game.say(visual, "Hi!")
game.addVisual(reporter)
game.errorReporter(reporter)
new Tick(interval = 1, action = { visual.error("DOMAIN_ERROR") }).start()
game.start()
game.flushEvents(1)
}

program clear {
program genericError {
game.addVisual(visual)
game.clear()
new Tick(interval = 1, action = { throw new Exception(message = "ERROR") }).start()
game.start()
game.flushEvents(1)
}
8 changes: 7 additions & 1 deletion test/sanity/game/game.wtest
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ describe "Game" {
assert.equals([myVisual, visual2, visual3].asSet(), game.allVisuals().asSet())
}

test "clear remove all visuals" {
game.addVisual(myVisual)
game.clear()
assert.that(game.allVisuals().isEmpty())
}

test "cell size fails when set to 0" {
assert.throwsException { => game.cellSize(0) }
}
Expand Down Expand Up @@ -160,7 +166,7 @@ describe "Game" {
game.addVisualCharacter(character)
// Simulate
io.queueEvent(["keypress", "ArrowUp"])
io.flushEvents(0)
game.flushEvents(0)

assert.equals(game.at(0, 1), character.position())
}
Expand Down

0 comments on commit 54159db

Please sign in to comment.