until now observeSubject works exactly like observe. First <really interesting> failing test

This commit is contained in:
Enrique García Cota 2012-02-03 18:15:57 +01:00
parent d24baefb4e
commit f7940a7705
3 changed files with 93 additions and 0 deletions

View File

@ -121,6 +121,13 @@ function beholder.observe(...)
return addCallbackToNode(node, callback)
end
function beholder.observeSubject(subject, ...)
assert(subject ~= nil, "Subject was nil. Please provide a valid subject")
local event, callback = extractEventAndCallbackFromParams({...})
local node = findOrCreateDescendantNode(root, event)
return addCallbackToNode(node, callback)
end
function beholder.stopObserving(id)
local node = findNodeById(id)
if not node then return false end

View File

@ -26,6 +26,26 @@ describe("Acceptance", function()
end)
test("Observing subjects", function()
local counter = 0
local button = {}
beholder.observeSubject(button, "EVENT", function() counter = counter + 1 end)
beholder.trigger("EVENT")
beholder.trigger("EVENT")
assert_equal(2, counter)
button = nil
collectgarbage("collect")
beholder.trigger("EVENT")
assert_equal(2, counter)
end)
test("several actions on the same event", function()
local counter1, counter2 = 0,0

View File

@ -54,6 +54,72 @@ describe("Unit", function()
end)
end)
describe(".observeSubject", function()
it("throws an error if subject is nil", function()
assert_error(function() beholder.observeSubject(nil, 'blah') end)
end)
it("notices simple events so that trigger works", function()
local counter = 0
local button = {}
beholder.observeSubject(button, "PUSH", function() counter = counter + 1 end)
beholder.trigger("PUSH")
assert_equal(counter, 1)
end)
it("remembers if more than one action is associated to the same event", function()
local counter1, counter2 = 0,0
local button = {}
beholder.observeSubject(button, "PUSH", function() counter1 = counter1 + 1 end)
beholder.observeSubject(button, "PUSH", function() counter2 = counter2 + 1 end)
beholder.trigger("PUSH")
assert_equal(counter1, 1)
assert_equal(counter2, 1)
end)
it("allows observing composed events", function()
local counter = 0
local button = {}
beholder.observeSubject(button, "PUSH", "start", function() counter = counter + 1 end)
beholder.trigger("PUSH", "start")
assert_equal(counter, 1)
end)
it("observes all events with the nil event", function()
local counter = 0
local button = {}
beholder.observeSubject(button, function(_,x) counter = counter + x end)
beholder.trigger("FOO", 1)
beholder.trigger("BAR", 2)
assert_equal(3, counter)
end)
it("does not store hard references to variables", function()
local counter = 0
local x = {}
local button = {}
beholder.observeSubject(button, x, function() counter = counter + 1 end)
beholder.triggerAll()
x = nil
collectgarbage("collect")
beholder.triggerAll()
assert_equal(1, counter)
end)
it("does not react to triggers once the subject has been garbage-collected", function()
local counter = 0
local button = {}
beholder.observeSubject(button, "PUSH", function() counter = counter + 1 end)
beholder.trigger("PUSH")
assert_equal(1, counter)
button = nil
collectgarbage("collect")
beholder.trigger("PUSH")
assert_equal(1, counter)
end)
end)
describe(".stopObserving", function()
it("stops noticing events so trigger doesn't work any more", function()
local counter = 0