added self-checks

This commit is contained in:
Enrique García 2011-10-30 17:13:21 +01:00
parent 1565149a8b
commit 46377b7ca3
2 changed files with 38 additions and 5 deletions

View File

@ -7,6 +7,15 @@
local beholder = {} local beholder = {}
local function initialize(self)
self._root = { callbacks={}, children={} }
self._nodesById = setmetatable({}, {__mode="k"})
end
local function checkSelf(self, methodName)
assert(type(self)=="table" and self._root and self._nodesById, "Use beholder:" .. methodName .. " instead of beholder." .. methodName)
end
local function falseIfZero(n) local function falseIfZero(n)
return n > 0 and n return n > 0 and n
end end
@ -83,28 +92,34 @@ local function removeCallbackFromNode(node, id)
return true return true
end end
function beholder:reset() -------
self._root = { callbacks={}, children={} }
self._nodesById = setmetatable({}, {__mode="k"})
end
function beholder:observe(...) function beholder:observe(...)
checkSelf(self, 'observe')
local event, callback = extractEventAndCallbackFromParams({...}) local event, callback = extractEventAndCallbackFromParams({...})
return addCallbackToNode(self, findOrCreateDescendantNode(self._root, event), callback) return addCallbackToNode(self, findOrCreateDescendantNode(self._root, event), callback)
end end
function beholder:stopObserving(id) function beholder:stopObserving(id)
checkSelf(self, 'stopObserving')
return removeCallbackFromNode(findNodeById(self, id), id) return removeCallbackFromNode(findNodeById(self, id), id)
end end
function beholder:trigger(...) function beholder:trigger(...)
checkSelf(self, 'trigger')
return falseIfZero( executeEventCallbacks(self._root, {...}) ) return falseIfZero( executeEventCallbacks(self._root, {...}) )
end end
function beholder:triggerAll(...) function beholder:triggerAll(...)
checkSelf(self, 'triggerAll')
return falseIfZero( executeAllCallbacks(self._root, {...}) ) return falseIfZero( executeAllCallbacks(self._root, {...}) )
end end
beholder:reset() function beholder:reset()
checkSelf(self, 'reset')
initialize(self)
end
initialize(beholder)
return beholder return beholder

View File

@ -7,6 +7,10 @@ describe("Unit", function()
end) end)
describe(":observe", function() describe(":observe", function()
it("checks self", function()
assert_error(function() beholder.observe("X", function() end) end)
end)
it("notices simple events so that trigger works", function() it("notices simple events so that trigger works", function()
local counter = 0 local counter = 0
beholder:observe("EVENT", function() counter = counter + 1 end) beholder:observe("EVENT", function() counter = counter + 1 end)
@ -44,6 +48,9 @@ describe("Unit", function()
end) end)
describe(":stopObserving", function() describe(":stopObserving", function()
it("checks self", function()
assert_error(function() beholder.stopObserving() end)
end)
it("stops noticing events so trigger doesn't work any more", function() it("stops noticing events so trigger doesn't work any more", function()
local counter = 0 local counter = 0
local id = beholder:observe("EVENT", function() counter = counter + 1 end) local id = beholder:observe("EVENT", function() counter = counter + 1 end)
@ -97,6 +104,9 @@ describe("Unit", function()
end) end)
describe(":trigger", function() describe(":trigger", function()
it("checks self", function()
assert_error(function() beholder.trigger() end)
end)
it("does not error on random stuff", function() it("does not error on random stuff", function()
assert_not_error(function() beholder:trigger("FOO") end) assert_not_error(function() beholder:trigger("FOO") end)
end) end)
@ -128,6 +138,9 @@ describe("Unit", function()
end) end)
describe(":triggerAll", function() describe(":triggerAll", function()
it("checks self", function()
assert_error(function() beholder.triggerAll() end)
end)
it("calls all registered callbacks", function() it("calls all registered callbacks", function()
local counter = 0 local counter = 0
beholder:observe("X", function() counter = counter + 1 end) beholder:observe("X", function() counter = counter + 1 end)
@ -148,7 +161,12 @@ describe("Unit", function()
beholder:observe("Y", function() end) beholder:observe("Y", function() end)
assert_equal(2, beholder:triggerAll()) assert_equal(2, beholder:triggerAll())
end) end)
end)
describe(":reset", function()
it("checks self", function()
assert_error(function() beholder.reset() end)
end)
end) end)