Skip to content

Commit

Permalink
Merge pull request #230 from uqbar-project/fix_blocks_error_handling_…
Browse files Browse the repository at this point in the history
…wollok_ts_cli_issue_201

wrap actions using io.runHandler (fix #229 )
  • Loading branch information
ivojawer authored Dec 28, 2024
2 parents 582ae90 + 2e3e90d commit 853c9d5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/wollok/game.wlk
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object game {
*/
method whenCollideDo(visual, action) {
io.addCollitionHandler(visual.identity(), { =>
self.colliders(visual).forEach({it => action.apply(it)})
self.colliders(visual).forEach({it => io.runHandler({action.apply(it)})})
})
}

Expand All @@ -117,7 +117,7 @@ object game {
var lastColliders = []
io.addCollitionHandler(visual.identity(), { =>
const colliders = self.colliders(visual)
colliders.forEach({ it => if (self.hasVisual(visual) and !lastColliders.contains(it)) action.apply(it) })
colliders.forEach({ it => if (self.hasVisual(visual) and !lastColliders.contains(it)) io.runHandler{action.apply(it)} })
lastColliders = colliders
})
}
Expand All @@ -133,7 +133,7 @@ object game {
method onTick(milliseconds, name, action) {
var times = 0
const initTime = io.currentTime()
io.addTimeHandler(name, { time => if (milliseconds == 0 or (time - initTime).div(milliseconds) > times) { action.apply(); times+=1 } })
io.addTimeHandler(name, { time => if (milliseconds == 0 or (time - initTime).div(milliseconds) > times) { io.runHandler(action); times+=1 } })
}

/**
Expand All @@ -146,7 +146,7 @@ object game {
method schedule(milliseconds, action) {
const name = action.identity()
self.onTick(milliseconds, name, {
action.apply()
io.runHandler(action)
io.removeTimeHandler(name)
})
}
Expand Down Expand Up @@ -692,7 +692,7 @@ class Tick {
**/
method start() {
if (self.isRunning()) {game.error("This tick is already started.")}
if (inmediate) {action.apply()}
if (inmediate) {io.runHandler(action)}
game.onTick(interval, name, action)
}

Expand Down
80 changes: 74 additions & 6 deletions test/sanity/game/events.wtest
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import wollok.game.*
import wollok.lang.io.*

object closureMock {
class CallCountingClosureMock {
var property calledCount = 0
method called() = calledCount > 0
method apply(args...) { calledCount += 1 }
}

object closureErrorMock {
method apply(args...) { self.error('FAIL') }
object closureMock inherits CallCountingClosureMock { }

object closureErrorMock inherits CallCountingClosureMock {
method apply(args...) { super(args) ; self.error('FAIL') }
}

class Visual {
Expand Down Expand Up @@ -37,7 +39,6 @@ describe 'events' {
game.clear()
assert.notThat(io.eventHandlersFor(event).contains(closureMock))
}


test 'whenCollideDo never called' {
game.whenCollideDo(visual, closureMock)
Expand All @@ -52,6 +53,7 @@ describe 'events' {
assert.that(closureMock.called())
}


test 'whenCollideDo many times called' {
game.whenCollideDo(visual, closureMock)
game.addVisual(new Visual())
Expand All @@ -60,7 +62,6 @@ describe 'events' {
assert.equals(2, closureMock.calledCount())
}


test 'onCollideDo never called' {
game.onCollideDo(visual, closureMock)
io.flushEvents(0)
Expand All @@ -71,9 +72,21 @@ describe 'events' {
game.onCollideDo(visual, closureMock)
game.addVisual(new Visual())
io.flushEvents(0)
assert.that(closureMock.called())
io.flushEvents(1)
io.flushEvents(2)
assert.equals(1, closureMock.calledCount())
}

test 'onCollideDo once called when a user error occurs' {
game.onCollideDo(visual, closureErrorMock)
game.addVisual(new Visual())
io.flushEvents(0)
io.flushEvents(1)
io.flushEvents(2)
assert.equals(1, closureErrorMock.calledCount())
}


test 'onCollideDo only once called in same collision' {
game.onCollideDo(visual, closureMock)
game.addVisual(new Visual())
Expand All @@ -94,6 +107,19 @@ describe 'events' {
assert.equals(2, closureMock.calledCount())
}

test 'onCollideDo many times called in many collisions when a user error occurs' {
game.onCollideDo(visual, closureErrorMock)
const collider = new Visual()
game.addVisual(collider)
io.flushEvents(0) // First collision
collider.position(game.at(1, 1))
io.flushEvents(1) // No collision, different positions
collider.position(game.origin())
io.flushEvents(2) // Second collision
assert.equals(2, closureErrorMock.calledCount())
}


test 'onCollideDo never called when visual is not in the game' {
game.onCollideDo(visual, closureMock)
game.addVisual(new Visual())
Expand All @@ -111,17 +137,28 @@ describe 'events' {

test 'onTick once called' {
game.onTick(1000, "", closureMock)
io.flushEvents(500)
assert.notThat(closureMock.called())
io.flushEvents(1000)
assert.that(closureMock.called())
}

test 'onTick many times called' {
game.onTick(1000, "", closureMock)
io.flushEvents(1000)
io.flushEvents(1500)
io.flushEvents(2000)
assert.equals(2, closureMock.calledCount())
}

test 'onTick many times called when a user error occurs' {
game.onTick(1000, "", closureErrorMock)
io.flushEvents(1000)
io.flushEvents(1500)
io.flushEvents(2000)
assert.equals(2, closureErrorMock.calledCount())
}

test 'onTick gets executed as many times as expected' {
game.onTick(100, "", closureMock)
io.flushEvents(200)
Expand Down Expand Up @@ -150,6 +187,12 @@ describe 'events' {
assert.that(closureMock.called())
}

test 'tickEvent executes action when started if inmediate set to true when a user error occurs' {
const newTickObject = new Tick(interval = 1000, inmediate = true, action = closureErrorMock)
newTickObject.start()
assert.that(closureErrorMock.called())
}

test 'tickEvent does not execute action when started if not specified' {
const newTickObject = new Tick(interval = 1000, action = closureMock)
newTickObject.start()
Expand Down Expand Up @@ -198,6 +241,24 @@ describe 'events' {
assert.equals(4, closureMock.calledCount()) // "" "" "" "" , proving interval got updated
}

test "update interval of an existent tick when a user error occurs" {
const newTickObject = new Tick(interval = 1000, action = closureErrorMock)
assert.equals(0, closureErrorMock.calledCount()) // hasn't counted yet
newTickObject.start() // starts tick event with an interval of 1sec
io.flushEvents(1000)
assert.equals(1, closureErrorMock.calledCount()) // 1 sec after, counter increments
io.flushEvents(2000)
assert.equals(2, closureErrorMock.calledCount()) // 1 sec after, it increments again
io.flushEvents(2500)
assert.equals(2, closureErrorMock.calledCount()) // despite being 0.5sec later, counter doesn't increment
newTickObject.interval(500) // restarts the tick with an interval of 0.5sec
assert.equals(2, closureErrorMock.calledCount()) // counter doesn't change since currentTime's still the same
io.flushEvents(3000)
assert.equals(3, closureErrorMock.calledCount()) // 0.5sec later, it increments again
io.flushEvents(3500)
assert.equals(4, closureErrorMock.calledCount()) // "" "" "" "" , proving interval got updated
}

test "a running tick throws exception if started again" {
const newTickObject = new Tick(interval = 1000, action = closureMock)
newTickObject.start()
Expand Down Expand Up @@ -232,6 +293,13 @@ describe 'events' {
assert.equals(1, closureMock.calledCount())
}

test 'schedule only once called when a user error occurs' {
game.schedule(1000, closureErrorMock)
io.flushEvents(1000)
io.flushEvents(2000)
assert.equals(1, closureErrorMock.calledCount())
}

test 'schedule on 0 ms should not throws exception' {
io.exceptionHandler(closureErrorMock)
game.schedule(0, closureMock)
Expand Down

0 comments on commit 853c9d5

Please sign in to comment.