From 2aa41a28c5bd4f0d843ad218352e5209eb298066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Sat, 29 Oct 2011 01:39:19 +0200 Subject: [PATCH] approaching previous status - trying to capture composed events --- beholder.lua | 21 +++++++++++++++------ spec/acceptance.lua | 12 ------------ spec/unit.lua | 40 +++++++++++++++------------------------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/beholder.lua b/beholder.lua index 4dd717c..062466c 100644 --- a/beholder.lua +++ b/beholder.lua @@ -14,6 +14,10 @@ local function findNode(self, event) return self._nodes[event] end +local function findNodeById(self, id) + return self._nodesById[id] +end + local function createNode(self, event) self._nodes[event] = {actions={}} return self._nodes[event] @@ -30,8 +34,14 @@ local function addActionToNode(self, node, action) return id end -local function executeNodeActions(node) - for _,action in pairs(node.actions) do action() end +local function removeActionFromNode(node, id) + if not node then return false end + node.actions[id] = nil + return true +end + +local function executeNodeActions(node, ...) + for _,action in pairs(node.actions) do action(...) end end function beholder:reset() @@ -44,13 +54,12 @@ function beholder:observe(event, action) end function beholder:stopObserving(id) - local node = self._nodesById[id] - node.actions[id] = nil + return removeActionFromNode(findNodeById(self, id), id) end -function beholder:trigger(event) +function beholder:trigger(event, ...) local node = findNode(self, event) - if node then executeNodeActions(node) end + if node then executeNodeActions(node, ...) end end beholder:reset() diff --git a/spec/acceptance.lua b/spec/acceptance.lua index 5750c2c..6851725 100644 --- a/spec/acceptance.lua +++ b/spec/acceptance.lua @@ -52,17 +52,6 @@ describe("Acceptance", function() assert_equal(counter2, 3) end) ---[[ - test("callback parameters", function() - local counter = 0 - - beholder:observe("EVENT", function(x) counter = counter + x end) - beholder:trigger("EVENT", 1) - assert_equal(counter, 1) - - beholder:trigger("EVENT", 5) - assert_equal(counter, 6) - end) test("composed events", function() local counter = 0 @@ -87,6 +76,5 @@ describe("Acceptance", function() assert_false(escapePressed) end) -]] end) diff --git a/spec/unit.lua b/spec/unit.lua index cfce5db..84040c2 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -22,29 +22,6 @@ describe("Unit", function() assert_equal(counter1, 1) assert_equal(counter2, 1) end) ---[[ - describe("when observing a table", function() - local counter - before(function() - counter = 0 - beholder:observe({"KEYPRESS", "enter"}, function() counter = counter + 1 end) - end) - - it("matches a trigger of a structurally identical table", function() - beholder:trigger({"KEYPRESS", "enter"}) - assert_equal(counter, 1) - end) - - it("matches a trigger of a structurally identical params list", function() - beholder:trigger("KEYPRESS", "enter") - assert_equal(counter, 1) - end) - - --it("triggering partials does not ", function() - --end) - - end) -]] end) describe(":stopObserving", function() @@ -72,7 +49,7 @@ describe("Unit", function() assert_equal(counter2, 2) end) ---[[ + it("passes parameters to the actions", function() local counter = 0 @@ -84,7 +61,20 @@ describe("Unit", function() assert_equal(counter, 6) end) -]] + + it("does not raise an error when stopping observing an inexisting event", function() + assert_not_error(function() beholder:stopObserving({}) end) + end) + + it("returns false when no action was found for an id", function() + assert_equal(false, beholder:stopObserving({})) + end) + + it("returns true when an action was found and removed", function() + local id = beholder:observe("X", function() end) + assert_true(beholder:stopObserving(id)) + end) + end)