|
|
|
|
@ -1,77 +1,99 @@
|
|
|
|
|
require "lunit"
|
|
|
|
|
|
|
|
|
|
network = require("network")
|
|
|
|
|
bc = require("bc")
|
|
|
|
|
ser = require("serialization")
|
|
|
|
|
|
|
|
|
|
module("test_bc", package.seeall, lunit.testcase)
|
|
|
|
|
|
|
|
|
|
function test_init()
|
|
|
|
|
local bci = bc:init("a1", {["light"]=false}, {["toggle_light"]=function() end})
|
|
|
|
|
local bci = bc:init({["light"]=false}, {["toggle_light"]=function() end})
|
|
|
|
|
assert_true(bci:has_noun("light"), "Nouns were not initialized")
|
|
|
|
|
assert_true(bci:has_verb("toggle_light"), "Verbs were not initialized")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_set_get_noun()
|
|
|
|
|
local bci = bc:init("a1", {["light"]=true}, {["toggle_light"]=function() end})
|
|
|
|
|
local bci = bc:init({["light"]=true}, {["toggle_light"]=function() end})
|
|
|
|
|
assert_equal(true, bci:get_noun("light"), "First get failed with wrong value")
|
|
|
|
|
bci:set_noun("light", false)
|
|
|
|
|
assert_equal(false, bci:get_noun("light"), "Second get failed with wrong value")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_request_noun()
|
|
|
|
|
local bci = bc:init("a1", {["light"]=true}, {["toggle_light"]=function() end})
|
|
|
|
|
local bci = bc:init({["light"]=true}, {["toggle_light"]=function() end})
|
|
|
|
|
local addr = network.get_scene()
|
|
|
|
|
local i = false
|
|
|
|
|
local expct = true
|
|
|
|
|
remote = require("modem"):init("some_addr", function(laddr, raddr, p, d, msg)
|
|
|
|
|
|
|
|
|
|
network.register("tester0", function(m, laddr, raddr, p, d, msg)
|
|
|
|
|
m = ser.unserialize(msg)
|
|
|
|
|
if m.ty == 3 and m.noun == "light" and m.value == expct then
|
|
|
|
|
i = true
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
remote:send("a1", 1234, ser.serialize({ty=1, noun="light"}))
|
|
|
|
|
|
|
|
|
|
network.send(addr, 1234, ser.serialize({ty=1, noun="light"}))
|
|
|
|
|
assert_true(i, "Noun response did not happen or was incorrect")
|
|
|
|
|
-- Reset and change noun
|
|
|
|
|
i = false
|
|
|
|
|
bci:set_noun("light", false)
|
|
|
|
|
expct = false
|
|
|
|
|
remote:send("a1", 1234, ser.serialize({ty=1, noun="light"}))
|
|
|
|
|
network.send(addr, 1234, ser.serialize({ty=1, noun="light"}))
|
|
|
|
|
assert_true(i, "Noun response did not happen or was incorrect")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_call_verb()
|
|
|
|
|
local bci = bc:init("a1", {["light"]=true}, {["toggle_light"]=function(b)
|
|
|
|
|
local bci = bc:init({["light"]=true}, {["toggle_light"]=function(b)
|
|
|
|
|
b:set_noun("light", not b:get_noun("light"))
|
|
|
|
|
end})
|
|
|
|
|
remote = require("modem"):init("some_addr", function(laddr, raddr, p, d, msg) end)
|
|
|
|
|
local addr = network.get_scene()
|
|
|
|
|
-- Call verb
|
|
|
|
|
remote:send("a1", 1234, ser.serialize({ty=2, verb="toggle_light"}))
|
|
|
|
|
network.send(addr, 1234, ser.serialize({ty=2, verb="toggle_light"}))
|
|
|
|
|
assert_equal(false, bci:get_noun("light"), "Verb did not do its job")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_multinode_call_verb()
|
|
|
|
|
local bc1 = bc:init("a1", {["light"]=true}, {["toggle_light"]=function(b)
|
|
|
|
|
b:set_noun("light", not b:get_noun("light"))
|
|
|
|
|
local bc1 = bc:init({["light0"]=true}, {["toggle_light0"]=function(b)
|
|
|
|
|
b:set_noun("light0", not b:get_noun("light0"))
|
|
|
|
|
end})
|
|
|
|
|
local bc2 = bc:init("a2", {}, {})
|
|
|
|
|
assert_true(bc1:call_verb("toggle_light"))
|
|
|
|
|
assert_equal(false, bc1:get_noun("light"), "First verb invokation did not go to plan")
|
|
|
|
|
assert_true(bc2:call_verb("toggle_light"))
|
|
|
|
|
assert_equal(true, bc1:get_noun("light"), "Second verb invokation did not go to plan")
|
|
|
|
|
local a1 = network.get_scene()
|
|
|
|
|
local bc2 = bc:init({}, {})
|
|
|
|
|
local a2 = network.get_scene()
|
|
|
|
|
|
|
|
|
|
network.set_scene(a1)
|
|
|
|
|
assert_true(bc1:call_verb("toggle_light0"))
|
|
|
|
|
assert_equal(false, bc1:get_noun("light0"), "First verb invocation did not go to plan")
|
|
|
|
|
|
|
|
|
|
network.set_scene(a2)
|
|
|
|
|
assert_true(bc2:call_verb("toggle_light0"))
|
|
|
|
|
|
|
|
|
|
network.set_scene(a1)
|
|
|
|
|
assert_equal(true, bc1:get_noun("light0"), "Second verb invocation did not go to plan")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_multinode_get_noun()
|
|
|
|
|
local key = "foobar"
|
|
|
|
|
local bc1 = bc:init("a1", {["light"]=key}, {["toggle_light"]=function(b)
|
|
|
|
|
b:set_noun("light", not b:get_noun("light"))
|
|
|
|
|
local bc1 = bc:init({["light1"]=key}, {["toggle_light1"]=function(b)
|
|
|
|
|
b:set_noun("light1", not b:get_noun("light1"))
|
|
|
|
|
end})
|
|
|
|
|
local bc2 = bc:init("a2", {}, {})
|
|
|
|
|
assert_equal(key, bc1:get_noun("light"), "Local get failed")
|
|
|
|
|
assert_equal(key, bc2:get_noun("light"), "Remote get failed")
|
|
|
|
|
local a1 = network.get_scene()
|
|
|
|
|
local bc2 = bc:init({}, {})
|
|
|
|
|
local a2 = network.get_scene()
|
|
|
|
|
|
|
|
|
|
network.set_scene(a1)
|
|
|
|
|
assert_equal(key, bc1:get_noun("light1"), "Local get failed")
|
|
|
|
|
|
|
|
|
|
network.set_scene(a2)
|
|
|
|
|
assert_equal(key, bc2:get_noun("light1"), "Remote get failed")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_multinode_listening()
|
|
|
|
|
local bc1 = bc:init("a1", {["foo"]=123}, {})
|
|
|
|
|
local bc2 = bc:init("a2", {}, {})
|
|
|
|
|
local bc1 = bc:init({["foo"]=123}, {})
|
|
|
|
|
local a1 = network.get_scene()
|
|
|
|
|
local bc2 = bc:init({}, {})
|
|
|
|
|
local a2 = network.get_scene()
|
|
|
|
|
|
|
|
|
|
network.set_scene(a1)
|
|
|
|
|
local i = false
|
|
|
|
|
-- Local listening
|
|
|
|
|
local id = bc1:listen_noun("foo", "onchange", nil, function(bc, foo)
|
|
|
|
|
@ -81,21 +103,29 @@ function test_multinode_listening()
|
|
|
|
|
assert_true(i, "Local listening failed")
|
|
|
|
|
i = false
|
|
|
|
|
bc1:listen_cancel("foo", id) -- Test wether cancelling works
|
|
|
|
|
|
|
|
|
|
network.set_scene(a2)
|
|
|
|
|
local j = false
|
|
|
|
|
local rid = bc2:listen_noun("foo", "onchange", nil, function(bc, foo)
|
|
|
|
|
j = true
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
network.set_scene(a1)
|
|
|
|
|
bc1:set_noun("foo", 1234)
|
|
|
|
|
assert_true(j, "Remote listening failed")
|
|
|
|
|
assert_false(i, "Cancelling local listener failed")
|
|
|
|
|
|
|
|
|
|
network.set_scene(a2)
|
|
|
|
|
bc2:listen_cancel("foo", rid)
|
|
|
|
|
j = false
|
|
|
|
|
|
|
|
|
|
network.set_scene(a1)
|
|
|
|
|
bc1:set_noun("foo", 34)
|
|
|
|
|
assert_false(j, "Cancelling remote listener failed")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_listen_modes()
|
|
|
|
|
local bci = bc:init("a1", {["noun"]=10}, {})
|
|
|
|
|
local bci = bc:init({["noun"]=10}, {})
|
|
|
|
|
local i = false
|
|
|
|
|
local id = 0
|
|
|
|
|
-- onchange
|
|
|
|
|
@ -158,62 +188,62 @@ function test_listen_modes()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_get_unknown_noun()
|
|
|
|
|
local bci = bc:init("a1")
|
|
|
|
|
local bci = bc:init()
|
|
|
|
|
|
|
|
|
|
assert_false(bci:has_noun("bar"))
|
|
|
|
|
assert_equal(nil, bci:get_noun("bar"))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_get_offline_node()
|
|
|
|
|
local bc1 = bc:init("a1")
|
|
|
|
|
local bc2 = bc:init("a2", {["foo"]=true})
|
|
|
|
|
|
|
|
|
|
-- Simulate node going offline
|
|
|
|
|
bc2.modem:remove_self()
|
|
|
|
|
bc2 = nil
|
|
|
|
|
|
|
|
|
|
local val = bc1:get_noun("foo")
|
|
|
|
|
assert_equal(nil, val)
|
|
|
|
|
end
|
|
|
|
|
-- function test_get_offline_node()
|
|
|
|
|
-- local bc1 = bc:init()
|
|
|
|
|
-- local bc2 = bc:init({["foo"]=true})
|
|
|
|
|
--
|
|
|
|
|
-- -- Simulate node going offline
|
|
|
|
|
-- bc2.modem:remove_self()
|
|
|
|
|
-- bc2 = nil
|
|
|
|
|
--
|
|
|
|
|
-- local val = bc1:get_noun("foo")
|
|
|
|
|
-- assert_equal(nil, val)
|
|
|
|
|
-- end
|
|
|
|
|
|
|
|
|
|
function test_call_unknown_verb()
|
|
|
|
|
local bci = bc:init("a1")
|
|
|
|
|
local bci = bc:init()
|
|
|
|
|
|
|
|
|
|
assert_false(bci:has_verb("unknown"))
|
|
|
|
|
assert_equal(false, bci:call_verb("unknown"))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_install_listen_on_unknown_noun()
|
|
|
|
|
local bci = bc:init("a1")
|
|
|
|
|
local bci = bc:init()
|
|
|
|
|
|
|
|
|
|
assert_false(bci:has_noun("unknown"))
|
|
|
|
|
bci:listen_noun("unknown", "onchange", nil, function() end)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_late_install()
|
|
|
|
|
local bc1 = bc:init("a1")
|
|
|
|
|
local bc1 = bc:init()
|
|
|
|
|
|
|
|
|
|
local i = false
|
|
|
|
|
|
|
|
|
|
assert_false(bc1:has_noun("foo"))
|
|
|
|
|
bc1:listen_noun("foo", "onchange", nil, function()
|
|
|
|
|
assert_false(bc1:has_noun("foo123"))
|
|
|
|
|
bc1:listen_noun("foo123", "onchange", nil, function()
|
|
|
|
|
i = true
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
local bc2 = bc:init("a2", {["foo"] = 123})
|
|
|
|
|
bc2:set_noun("foo", 321)
|
|
|
|
|
local bc2 = bc:init({["foo123"] = 123})
|
|
|
|
|
bc2:set_noun("foo123", 321)
|
|
|
|
|
|
|
|
|
|
assert_true(i, "Listener was not called")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function test_verb_multicall_bug()
|
|
|
|
|
local var = 0
|
|
|
|
|
local bc1 = bc:init("a1", nil, {
|
|
|
|
|
local bc1 = bc:init(nil, {
|
|
|
|
|
["verb"] = function(bc, foo)
|
|
|
|
|
var = var + foo
|
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
local bc2 = bc:init("a2")
|
|
|
|
|
local bc2 = bc:init()
|
|
|
|
|
bc2:call_verb("verb", 1)
|
|
|
|
|
assert_equal(1, var, "Verb was not called correctly")
|
|
|
|
|
bc2:call_verb("verb", 2)
|
|
|
|
|
|