Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update exception handler for game #183

Merged
merged 6 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())})
PalumboN marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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