mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
second acceptance test complete
This commit is contained in:
parent
0b23e79346
commit
a061a2b898
23
beholder.lua
23
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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user