initial acceptance test (simplest case) is passing

This commit is contained in:
Enrique García 2011-10-24 09:06:29 +02:00
parent 0fcddff0bf
commit 0b23e79346
3 changed files with 96 additions and 0 deletions

View File

@ -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
View 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)

View File

@ -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)