mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
initial acceptance test (simplest case) is passing
This commit is contained in:
parent
0fcddff0bf
commit
0b23e79346
32
beholder.lua
32
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
|
29
spec/acceptance.lua
Normal file
29
spec/acceptance.lua
Normal file
@ -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)
|
@ -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)
|
Loading…
Reference in New Issue
Block a user