2011-10-24 07:06:29 +00:00
|
|
|
local beholder = require 'beholder'
|
|
|
|
|
|
|
|
describe("Unit", function()
|
|
|
|
|
|
|
|
before(function()
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.reset()
|
2011-10-24 07:06:29 +00:00
|
|
|
end)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
describe(".observe", function()
|
2011-10-24 07:06:29 +00:00
|
|
|
it("notices simple events so that trigger works", function()
|
|
|
|
local counter = 0
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("EVENT", function() counter = counter + 1 end)
|
|
|
|
beholder.trigger("EVENT")
|
2011-10-24 07:06:29 +00:00
|
|
|
assert_equal(counter, 1)
|
|
|
|
end)
|
2011-10-25 22:18:17 +00:00
|
|
|
|
|
|
|
it("remembers if more than one action is associated to the same event", function()
|
|
|
|
local counter1, counter2 = 0,0
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("EVENT", function() counter1 = counter1 + 1 end)
|
|
|
|
beholder.observe("EVENT", function() counter2 = counter2 + 1 end)
|
|
|
|
beholder.trigger("EVENT")
|
2011-10-25 22:18:17 +00:00
|
|
|
assert_equal(counter1, 1)
|
|
|
|
assert_equal(counter2, 1)
|
|
|
|
end)
|
2011-10-29 00:45:39 +00:00
|
|
|
|
|
|
|
it("allows observing composed events", function()
|
|
|
|
local counter = 0
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("KEYPRESS", "start", function() counter = counter + 1 end)
|
|
|
|
beholder.trigger("KEYPRESS", "start")
|
2011-10-29 00:45:39 +00:00
|
|
|
assert_equal(counter, 1)
|
|
|
|
end)
|
2011-10-30 11:33:34 +00:00
|
|
|
|
|
|
|
it("observes all events with the nil event", function()
|
|
|
|
local counter = 0
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe(function(_,x) counter = counter + x end)
|
|
|
|
beholder.trigger("FOO", 1)
|
|
|
|
beholder.trigger("BAR", 2)
|
2011-10-30 11:33:34 +00:00
|
|
|
assert_equal(3, counter)
|
|
|
|
end)
|
2011-10-30 11:57:21 +00:00
|
|
|
|
|
|
|
it("throws an error if called without at least one parameter", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
assert_error(function() beholder.observe() end)
|
2011-10-30 11:57:21 +00:00
|
|
|
end)
|
2011-11-01 21:36:43 +00:00
|
|
|
|
|
|
|
it("does not store hard references to variables", function()
|
|
|
|
local counter = 0
|
|
|
|
local x = {}
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe(x, function() counter = counter + 1 end)
|
|
|
|
beholder.triggerAll()
|
2011-11-01 21:36:43 +00:00
|
|
|
x = nil
|
|
|
|
collectgarbage("collect")
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.triggerAll()
|
2011-11-01 21:36:43 +00:00
|
|
|
assert_equal(1, counter)
|
|
|
|
end)
|
2011-10-24 07:06:29 +00:00
|
|
|
end)
|
|
|
|
|
2012-02-03 17:15:57 +00:00
|
|
|
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)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
describe(".stopObserving", function()
|
2011-10-24 07:06:29 +00:00
|
|
|
it("stops noticing events so trigger doesn't work any more", function()
|
|
|
|
local counter = 0
|
2012-01-29 12:56:34 +00:00
|
|
|
local id = beholder.observe("EVENT", function() counter = counter + 1 end)
|
|
|
|
beholder.trigger("EVENT")
|
|
|
|
beholder.stopObserving(id)
|
|
|
|
beholder.trigger("EVENT")
|
2011-10-24 07:06:29 +00:00
|
|
|
assert_equal(counter, 1)
|
|
|
|
end)
|
2011-10-25 22:18:17 +00:00
|
|
|
|
2011-10-28 23:24:10 +00:00
|
|
|
it("stops observing one id without disturbing the others", function()
|
2011-10-25 22:18:17 +00:00
|
|
|
local counter1, counter2 = 0,0
|
2012-01-29 12:56:34 +00:00
|
|
|
local id1 = beholder.observe("EVENT", function() counter1 = counter1 + 1 end)
|
|
|
|
beholder.observe("EVENT", function() counter2 = counter2 + 1 end)
|
|
|
|
beholder.trigger("EVENT")
|
2011-10-25 22:18:17 +00:00
|
|
|
|
|
|
|
assert_equal(counter1, 1)
|
|
|
|
assert_equal(counter2, 1)
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.stopObserving(id1)
|
|
|
|
beholder.trigger("EVENT")
|
2011-10-25 22:18:17 +00:00
|
|
|
|
|
|
|
assert_equal(counter1, 1)
|
|
|
|
assert_equal(counter2, 2)
|
|
|
|
|
|
|
|
end)
|
2011-10-28 23:39:19 +00:00
|
|
|
|
2011-10-25 22:31:33 +00:00
|
|
|
it("passes parameters to the actions", function()
|
|
|
|
local counter = 0
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("EVENT", function(x) counter = counter + x end)
|
|
|
|
beholder.trigger("EVENT", 1)
|
2011-10-25 22:31:33 +00:00
|
|
|
|
|
|
|
assert_equal(counter, 1)
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("EVENT", 5)
|
2011-10-25 22:31:33 +00:00
|
|
|
|
|
|
|
assert_equal(counter, 6)
|
|
|
|
end)
|
2011-10-28 23:39:19 +00:00
|
|
|
|
|
|
|
it("does not raise an error when stopping observing an inexisting event", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
assert_not_error(function() beholder.stopObserving({}) end)
|
2011-10-28 23:39:19 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("returns false when no action was found for an id", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
assert_equal(false, beholder.stopObserving({}))
|
2011-10-28 23:39:19 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("returns true when an action was found and removed", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
local id = beholder.observe("X", function() end)
|
|
|
|
assert_true(beholder.stopObserving(id))
|
2011-10-28 23:39:19 +00:00
|
|
|
end)
|
|
|
|
|
2011-10-24 07:06:29 +00:00
|
|
|
end)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
describe(".trigger", function()
|
2011-10-29 00:45:39 +00:00
|
|
|
it("does not error on random stuff", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
assert_not_error(function() beholder.trigger("FOO") end)
|
2011-10-29 00:45:39 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("returns false on events with no actions", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
assert_equal(false, beholder.trigger("FOO"))
|
2011-10-29 00:45:39 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("returns false if there was a node with no actions", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("ONE","TWO", function() end)
|
|
|
|
assert_equal(false, beholder.trigger("ONE"))
|
2011-10-29 00:45:39 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("returns the number of actions executed", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("X", function() end)
|
|
|
|
beholder.observe("X", function() end)
|
|
|
|
assert_equal(2, beholder.trigger("X"))
|
2011-10-29 00:45:39 +00:00
|
|
|
end)
|
|
|
|
|
2011-10-30 12:52:14 +00:00
|
|
|
it("triggers callbacks within the nil event only", function()
|
2011-10-30 11:33:34 +00:00
|
|
|
local counter = 0
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("X", function() counter = counter + 10 end)
|
|
|
|
beholder.observe(function() counter = counter + 5 end)
|
2011-10-30 11:33:34 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger()
|
2011-10-30 11:33:34 +00:00
|
|
|
|
2011-10-30 12:52:14 +00:00
|
|
|
assert_equal(5, counter)
|
2011-10-30 11:33:34 +00:00
|
|
|
end)
|
2011-10-29 00:45:39 +00:00
|
|
|
end)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
describe(".triggerAll", function()
|
2011-10-30 12:52:14 +00:00
|
|
|
it("calls all registered callbacks", function()
|
|
|
|
local counter = 0
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("X", function() counter = counter + 1 end)
|
|
|
|
beholder.triggerAll()
|
2011-10-30 12:52:14 +00:00
|
|
|
assert_equal(1, counter)
|
|
|
|
end)
|
|
|
|
it("passes parameters to callbacks", function()
|
|
|
|
local counter = 0
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe(function(x) counter = counter + x end)
|
|
|
|
beholder.triggerAll(2)
|
2011-10-30 12:52:14 +00:00
|
|
|
assert_equal(2, counter)
|
|
|
|
end)
|
|
|
|
it("returns false if no actions where found", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
assert_false(beholder.triggerAll())
|
2011-10-30 12:52:14 +00:00
|
|
|
end)
|
|
|
|
it("returns the number of actions executed", function()
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("X", function() end)
|
|
|
|
beholder.observe("Y", function() end)
|
|
|
|
assert_equal(2, beholder.triggerAll())
|
2011-10-30 12:52:14 +00:00
|
|
|
end)
|
2011-10-30 16:13:21 +00:00
|
|
|
end)
|
2011-10-24 07:06:29 +00:00
|
|
|
end)
|