From a061a2b898a180a69f7932156423823df2984c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Wed, 26 Oct 2011 00:18:17 +0200 Subject: [PATCH] second acceptance test complete --- beholder.lua | 23 +++++++++++++++-------- spec/acceptance.lua | 27 +++++++++++++++++++++++++++ spec/unit.lua | 26 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/beholder.lua b/beholder.lua index 5cefd82..c0c65f6 100644 --- a/beholder.lua +++ b/beholder.lua @@ -7,26 +7,33 @@ -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra -local beholder = { actions = {} } - +local beholder = {} function beholder:reset() - self.actions = {} + self._actions = {} + self._ids = {} end function beholder:observe(event, action) - self.actions[event] = action - return event + local id = {} + self._actions[event] = self._actions[event] or {} + self._actions[event][id] = action + self._ids[id] = event + return id end function beholder:stopObserving(id) - self.actions[id] = nil + local event = self._ids[id] + self._actions[event][id] = nil end function beholder:trigger(event) - local action = self.actions[event] - if action then action() end + local actions = self._actions[event] or {} + for _,action in pairs(actions) do + action() + end end +beholder:reset() return beholder diff --git a/spec/acceptance.lua b/spec/acceptance.lua index 2fdd62d..9574371 100644 --- a/spec/acceptance.lua +++ b/spec/acceptance.lua @@ -26,4 +26,31 @@ describe("Acceptance", function() end) + test("several actions on the same event", function() + + local counter1, counter2 = 0,0 + + local id1 = beholder:observe("EVENT", function() counter1 = counter1 + 1 end) + local id2 = beholder:observe("EVENT", function() counter2 = counter2 + 1 end) + + beholder:trigger("EVENT") + beholder:trigger("EVENT") + + assert_equal(counter1, 2) + assert_equal(counter2, 2) + + beholder:stopObserving(id1) + + beholder:trigger("EVENT") + assert_equal(counter1, 2) + assert_equal(counter2, 3) + + beholder:stopObserving(id2) + + beholder:trigger("EVENT") + assert_equal(counter1, 2) + assert_equal(counter2, 3) + + end) + end) diff --git a/spec/unit.lua b/spec/unit.lua index 6dace8a..b9571af 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -13,6 +13,16 @@ describe("Unit", function() beholder:trigger("EVENT") assert_equal(counter, 1) end) + + it("remembers if more than one action is associated to the same event", function() + local counter1, counter2 = 0,0 + beholder:observe("EVENT", function() counter1 = counter1 + 1 end) + beholder:observe("EVENT", function() counter2 = counter2 + 1 end) + beholder:trigger("EVENT") + assert_equal(counter1, 1) + assert_equal(counter2, 1) + end) + end) describe(":stopObserving", function() @@ -24,6 +34,22 @@ describe("Unit", function() beholder:trigger("EVENT") assert_equal(counter, 1) end) + + it("when given an id, it stops observing it", function() + local counter1, counter2 = 0,0 + local id1 = beholder:observe("EVENT", function() counter1 = counter1 + 1 end) + beholder:observe("EVENT", function() counter2 = counter2 + 1 end) + beholder:trigger("EVENT") + + assert_equal(counter1, 1) + assert_equal(counter2, 1) + beholder:stopObserving(id1) + beholder:trigger("EVENT") + + assert_equal(counter1, 1) + assert_equal(counter2, 2) + + end) end)