parent
8fb07e22cc
commit
719201e950
@ -0,0 +1,136 @@
|
|||||||
|
require("lunit")
|
||||||
|
|
||||||
|
local network = require("network")
|
||||||
|
local serialization = require("serialization")
|
||||||
|
local BaseControl = require("bc")
|
||||||
|
|
||||||
|
module("listening", package.seeall, lunit.testcase)
|
||||||
|
|
||||||
|
function test_invalid_query()
|
||||||
|
local bc = BaseControl:new()
|
||||||
|
local invalids = {
|
||||||
|
bc.Query.Equals,
|
||||||
|
bc.Query.Above,
|
||||||
|
bc.Query.Below,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, q in ipairs(invalids) do
|
||||||
|
assert_error_match("invalid", function()
|
||||||
|
bc:listen("invq1", q, function() end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function test_listen_simple()
|
||||||
|
local bc = BaseControl:new()
|
||||||
|
bc:register("lisimpl1", 1234)
|
||||||
|
bc:finalize()
|
||||||
|
|
||||||
|
local n = 0
|
||||||
|
bc:listen("lisimpl1", bc.Query.Change, function(new)
|
||||||
|
n = n + 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert_equal(0, n, "wrong number of listener invokations")
|
||||||
|
bc:set("lisimpl1", 4321)
|
||||||
|
assert_equal(1, n, "wrong number of listener invokations")
|
||||||
|
bc:set("lisimpl1", 42)
|
||||||
|
assert_equal(2, n, "wrong number of listener invokations")
|
||||||
|
end
|
||||||
|
|
||||||
|
function test_listen_network_simple()
|
||||||
|
local bc1 = BaseControl:new()
|
||||||
|
local addr1 = network.get_scene()
|
||||||
|
bc1:register("lin1", 1234)
|
||||||
|
bc1:finalize()
|
||||||
|
|
||||||
|
local bc2 = BaseControl:new()
|
||||||
|
local addr2 = network.get_scene()
|
||||||
|
|
||||||
|
local tmp, n = 0, 0
|
||||||
|
bc2:listen("lin1", bc1.Query.Change, function(new)
|
||||||
|
assert_equal(tmp, new, "value did not propagate")
|
||||||
|
n = n + 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
network.set_scene(addr1)
|
||||||
|
tmp = 12
|
||||||
|
assert_equal(0, n, "wrong number of listener invokations")
|
||||||
|
bc1:set("lin1", 12)
|
||||||
|
assert_equal(1, n, "wrong number of listener invokations")
|
||||||
|
|
||||||
|
tmp = 16
|
||||||
|
bc1:set("lin1", 16)
|
||||||
|
assert_equal(2, n, "wrong number of listener invokations")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Generate testcases for queries
|
||||||
|
do
|
||||||
|
local tests = {
|
||||||
|
{name="change", query=BaseControl.Query.Change, pos1=1, neg=1, pos2=2},
|
||||||
|
{name="rising", query=BaseControl.Query.Rising, pos1=1, neg=-1, pos2=1},
|
||||||
|
{name="falling", query=BaseControl.Query.Falling, pos1=-1, neg=1, pos2=-1},
|
||||||
|
{name="equals", query=BaseControl.Query.Equals(123), pos1=123, neg=124, pos2=123},
|
||||||
|
{name="above", query=BaseControl.Query.Above(100), pos1=101, neg=99, pos2=123},
|
||||||
|
{name="below", query=BaseControl.Query.Below(100), pos1=12, neg=124, pos2=23},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test in ipairs(tests) do
|
||||||
|
-- Local test
|
||||||
|
listening["test_listen_query_"..test.name] = function()
|
||||||
|
local bc = BaseControl:new()
|
||||||
|
local noun = "lq"..test.name.."1"
|
||||||
|
bc:register(noun, 0)
|
||||||
|
bc:finalize()
|
||||||
|
|
||||||
|
local n = 0
|
||||||
|
bc:listen(noun, test.query, function(new)
|
||||||
|
n = n + 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert_equal(0, n, "wrong number of listener invokations")
|
||||||
|
bc:set(noun, test.pos1)
|
||||||
|
assert_equal(1, n, "wrong number of listener invokations")
|
||||||
|
bc:set(noun, test.neg)
|
||||||
|
assert_equal(1, n, "wrong number of listener invokations")
|
||||||
|
bc:set(noun, test.pos2)
|
||||||
|
assert_equal(2, n, "wrong number of listener invokations")
|
||||||
|
|
||||||
|
bc:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Remote test
|
||||||
|
listening["test_listen_network_query_"..test.name] = function()
|
||||||
|
local bc1 = BaseControl:new()
|
||||||
|
local addr1 = network.get_scene()
|
||||||
|
local noun = "lnq"..test.name.."1"
|
||||||
|
bc1:register(noun, 0)
|
||||||
|
bc1:finalize()
|
||||||
|
|
||||||
|
local bc2 = BaseControl:new()
|
||||||
|
local addr2 = network.get_scene()
|
||||||
|
|
||||||
|
local tmp, n = 0, 0
|
||||||
|
bc2:listen(noun, test.query, function(new)
|
||||||
|
assert_equal(tmp, new, "value did not propagate")
|
||||||
|
n = n + 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
network.set_scene(addr1)
|
||||||
|
assert_equal(0, n, "wrong number of listener invokations")
|
||||||
|
tmp = test.pos1
|
||||||
|
bc1:set(noun, test.pos1)
|
||||||
|
assert_equal(1, n, "wrong number of listener invokations")
|
||||||
|
tmp = test.neg
|
||||||
|
bc1:set(noun, test.neg)
|
||||||
|
assert_equal(1, n, "wrong number of listener invokations")
|
||||||
|
tmp = test.pos2
|
||||||
|
bc1:set(noun, test.pos2)
|
||||||
|
assert_equal(2, n, "wrong number of listener invokations")
|
||||||
|
|
||||||
|
bc1:close()
|
||||||
|
network.set_scene(addr2)
|
||||||
|
bc2:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in new issue