diff --git a/beholder.lua b/beholder.lua index e69de29..5cefd82 100644 --- a/beholder.lua +++ b/beholder.lua @@ -0,0 +1,32 @@ +-- beholder.lua - v1.0 (2011-11) +-- requires middleclass 2.0 + +-- Copyright (c) 2011 Enrique GarcĂ­a Cota +-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +-- 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 = {} } + + +function beholder:reset() + self.actions = {} +end + +function beholder:observe(event, action) + self.actions[event] = action + return event +end + +function beholder:stopObserving(id) + self.actions[id] = nil +end + +function beholder:trigger(event) + local action = self.actions[event] + if action then action() end +end + + +return beholder diff --git a/spec/acceptance.lua b/spec/acceptance.lua new file mode 100644 index 0000000..2fdd62d --- /dev/null +++ b/spec/acceptance.lua @@ -0,0 +1,29 @@ +local beholder = require 'beholder' + + +describe("Acceptance", function() + + before(function() + beholder:reset() + end) + + test("Normal behavior", function() + + local counter = 0 + + local id = beholder:observe("EVENT", function() counter = counter + 1 end) + + beholder:trigger("EVENT") + beholder:trigger("EVENT") + + assert_equal(counter, 2) + + beholder:stopObserving(id) + + beholder:trigger("EVENT") + + assert_equal(counter, 2) + + end) + +end) diff --git a/spec/unit.lua b/spec/unit.lua index e69de29..6dace8a 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -0,0 +1,35 @@ +local beholder = require 'beholder' + +describe("Unit", function() + + before(function() + beholder:reset() + end) + + describe(":observe", function() + it("notices simple events so that trigger works", function() + local counter = 0 + beholder:observe("EVENT", function() counter = counter + 1 end) + beholder:trigger("EVENT") + assert_equal(counter, 1) + end) + end) + + describe(":stopObserving", function() + 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) + beholder:trigger("EVENT") + beholder:stopObserving(id) + beholder:trigger("EVENT") + assert_equal(counter, 1) + end) + end) + + + describe(":reset", function() + + end) + + +end)