2011-10-24 07:06:29 +00:00
|
|
|
local beholder = require 'beholder'
|
|
|
|
|
|
|
|
|
|
|
|
describe("Acceptance", 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 13:14:43 +00:00
|
|
|
test("Basic behavior", function()
|
2011-10-24 07:06:29 +00:00
|
|
|
|
|
|
|
local counter = 0
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
local id = beholder.observe("EVENT", function() counter = counter + 1 end)
|
2011-10-24 07:06:29 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("EVENT")
|
|
|
|
beholder.trigger("EVENT")
|
2011-10-24 07:06:29 +00:00
|
|
|
|
|
|
|
assert_equal(counter, 2)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.stopObserving(id)
|
2011-10-24 07:06:29 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("EVENT")
|
2011-10-24 07:06:29 +00:00
|
|
|
|
|
|
|
assert_equal(counter, 2)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
2012-02-03 17:15:57 +00:00
|
|
|
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)
|
|
|
|
|
2011-10-25 22:18:17 +00:00
|
|
|
test("several actions on the same event", function()
|
2011-10-28 23:24:10 +00:00
|
|
|
|
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)
|
|
|
|
local id2 = beholder.observe("EVENT", function() counter2 = counter2 + 1 end)
|
2011-10-25 22:18:17 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("EVENT")
|
|
|
|
beholder.trigger("EVENT")
|
2011-10-25 22:18:17 +00:00
|
|
|
|
|
|
|
assert_equal(counter1, 2)
|
|
|
|
assert_equal(counter2, 2)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.stopObserving(id1)
|
2011-10-25 22:18:17 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("EVENT")
|
2011-10-25 22:18:17 +00:00
|
|
|
assert_equal(counter1, 2)
|
|
|
|
assert_equal(counter2, 3)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.stopObserving(id2)
|
2011-10-25 22:18:17 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("EVENT")
|
2011-10-25 22:18:17 +00:00
|
|
|
assert_equal(counter1, 2)
|
|
|
|
assert_equal(counter2, 3)
|
|
|
|
|
|
|
|
end)
|
2011-10-25 22:31:33 +00:00
|
|
|
|
2011-10-28 23:24:10 +00:00
|
|
|
test("composed events", function()
|
|
|
|
local counter = 0
|
|
|
|
local lastKey = ""
|
|
|
|
local enterPressed = false
|
|
|
|
local escapePressed = false
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("KEYPRESS", function() counter = counter + 1 end)
|
|
|
|
beholder.observe("KEYPRESS", function(key) lastKey = key end)
|
|
|
|
beholder.observe("KEYPRESS", "enter", function() enterPressed = true end)
|
2011-10-28 23:24:10 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("KEYPRESS", "space")
|
2011-10-28 23:24:10 +00:00
|
|
|
assert_equal(counter, 1)
|
|
|
|
assert_equal(lastKey, "space")
|
|
|
|
assert_false(enterPressed)
|
|
|
|
assert_false(escapePressed)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("KEYPRESS", "enter")
|
2011-10-28 23:24:10 +00:00
|
|
|
assert_equal(counter, 2)
|
|
|
|
assert_equal(lastKey, "enter")
|
|
|
|
assert_true(enterPressed)
|
|
|
|
assert_false(escapePressed)
|
2011-10-25 22:31:33 +00:00
|
|
|
end)
|
|
|
|
|
2011-10-30 11:33:34 +00:00
|
|
|
test("nil events", function()
|
|
|
|
local counter = 0
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
local id = beholder.observe(function(_, x) counter = counter + x end)
|
2011-10-30 11:33:34 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.trigger("FOO", 1)
|
|
|
|
beholder.trigger("BAR", 2)
|
2011-10-30 11:33:34 +00:00
|
|
|
|
|
|
|
assert_equal(3, counter)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.stopObserving(id)
|
2011-10-30 11:33:34 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe(function() counter = 10 end)
|
2011-10-30 12:52:14 +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(10, counter)
|
|
|
|
end)
|
|
|
|
|
|
|
|
test("triggering all events", function()
|
|
|
|
local even = 0
|
|
|
|
local uneven = 1
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.observe("EVEN", function(x) even = even + 2*x end)
|
|
|
|
beholder.observe("UNEVEN", function(x) uneven = uneven + 2*x end)
|
2011-10-30 12:52:14 +00:00
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.triggerAll(1)
|
2011-10-30 12:52:14 +00:00
|
|
|
|
|
|
|
assert_equal(even, 2)
|
|
|
|
assert_equal(uneven, 3)
|
|
|
|
|
2012-01-29 12:56:34 +00:00
|
|
|
beholder.triggerAll(2)
|
2011-10-30 12:52:14 +00:00
|
|
|
|
|
|
|
assert_equal(even, 6)
|
|
|
|
assert_equal(uneven, 7)
|
2011-10-30 11:33:34 +00:00
|
|
|
|
|
|
|
end)
|
|
|
|
|
2011-10-28 23:24:10 +00:00
|
|
|
|
2011-10-24 07:06:29 +00:00
|
|
|
end)
|