From 46377b7ca39565ef00de0f139bb48120819b252d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Sun, 30 Oct 2011 17:13:21 +0100 Subject: [PATCH] added self-checks --- beholder.lua | 25 ++++++++++++++++++++----- spec/unit.lua | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/beholder.lua b/beholder.lua index 0412fd6..7b40ac9 100644 --- a/beholder.lua +++ b/beholder.lua @@ -7,6 +7,15 @@ 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) return n > 0 and n end @@ -83,28 +92,34 @@ local function removeCallbackFromNode(node, id) return true end -function beholder:reset() - self._root = { callbacks={}, children={} } - self._nodesById = setmetatable({}, {__mode="k"}) -end +------- function beholder:observe(...) + checkSelf(self, 'observe') local event, callback = extractEventAndCallbackFromParams({...}) return addCallbackToNode(self, findOrCreateDescendantNode(self._root, event), callback) end function beholder:stopObserving(id) + checkSelf(self, 'stopObserving') return removeCallbackFromNode(findNodeById(self, id), id) end function beholder:trigger(...) + checkSelf(self, 'trigger') return falseIfZero( executeEventCallbacks(self._root, {...}) ) end function beholder:triggerAll(...) + checkSelf(self, 'triggerAll') return falseIfZero( executeAllCallbacks(self._root, {...}) ) end -beholder:reset() +function beholder:reset() + checkSelf(self, 'reset') + initialize(self) +end + +initialize(beholder) return beholder diff --git a/spec/unit.lua b/spec/unit.lua index b4350bd..31a4e26 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -7,6 +7,10 @@ describe("Unit", function() end) 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() local counter = 0 beholder:observe("EVENT", function() counter = counter + 1 end) @@ -44,6 +48,9 @@ describe("Unit", function() end) 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() local counter = 0 local id = beholder:observe("EVENT", function() counter = counter + 1 end) @@ -97,6 +104,9 @@ describe("Unit", function() end) describe(":trigger", function() + it("checks self", function() + assert_error(function() beholder.trigger() end) + end) it("does not error on random stuff", function() assert_not_error(function() beholder:trigger("FOO") end) end) @@ -128,6 +138,9 @@ describe("Unit", function() end) describe(":triggerAll", function() + it("checks self", function() + assert_error(function() beholder.triggerAll() end) + end) it("calls all registered callbacks", function() local counter = 0 beholder:observe("X", function() counter = counter + 1 end) @@ -148,7 +161,12 @@ describe("Unit", function() beholder:observe("Y", function() end) assert_equal(2, beholder:triggerAll()) end) + end) + describe(":reset", function() + it("checks self", function() + assert_error(function() beholder.reset() end) + end) end)