You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
263 lines
7.3 KiB
263 lines
7.3 KiB
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({["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({["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({["light"]=true}, {["toggle_light"]=function() end})
|
|
local addr = network.get_scene()
|
|
local i = false
|
|
local expct = true
|
|
|
|
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)
|
|
|
|
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
|
|
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({["light"]=true}, {
|
|
["toggle_light"]=function(b)
|
|
b:set_noun("light", not b:get_noun("light"))
|
|
end,
|
|
["set_light"]=function(b, state)
|
|
b:set_noun("light", state)
|
|
end,
|
|
})
|
|
local addr = network.get_scene()
|
|
-- Call verb
|
|
network.send(addr, 1234, ser.serialize({ty=2, verb="toggle_light"}))
|
|
assert_equal(false, bci:get_noun("light"), "Verb did not do its job")
|
|
|
|
network.send(addr, 1234, ser.serialize({ty=2, verb="set_light", param=true}))
|
|
assert_equal(true, bci:get_noun("light"), "Verb did not do its job")
|
|
|
|
network.send(addr, 1234, ser.serialize({ty=2, verb="set_light", param=false}))
|
|
assert_equal(false, bci:get_noun("light"), "Verb did not do its job")
|
|
end
|
|
|
|
function test_multinode_call_verb()
|
|
local bc1 = bc:init({["light0"]=true}, {["toggle_light0"]=function(b)
|
|
b:set_noun("light0", not b:get_noun("light0"))
|
|
end})
|
|
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({["light1"]=key}, {["toggle_light1"]=function(b)
|
|
b:set_noun("light1", not b:get_noun("light1"))
|
|
end})
|
|
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({["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)
|
|
i = true
|
|
end)
|
|
bc1:set_noun("foo", 111)
|
|
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({["noun"]=10}, {})
|
|
local i = false
|
|
local id = 0
|
|
-- onchange
|
|
local id = bci:listen_noun("noun", "onchange", nil, function(bc, noun)
|
|
i = true
|
|
end)
|
|
bci:set_noun("noun", 11)
|
|
assert_true(i, "Listening \"onchange\" failed")
|
|
bci:listen_cancel("noun", id)
|
|
i = false
|
|
-- onrising
|
|
id = bci:listen_noun("noun", "onrising", nil, function(bc, noun)
|
|
i = true
|
|
end)
|
|
bci:set_noun("noun", 10)
|
|
assert_false(i, "Listening \"onrising\" failed(A)")
|
|
bci:set_noun("noun", 11)
|
|
assert_true(i, "Listening \"onrising\" failed(B)")
|
|
bci:listen_cancel("noun", id)
|
|
i = false
|
|
-- onfalling
|
|
id = bci:listen_noun("noun", "onfalling", nil, function(bc, noun)
|
|
i = true
|
|
end)
|
|
bci:set_noun("noun", 12)
|
|
assert_false(i, "Listening \"onfalling\" failed(A)")
|
|
bci:set_noun("noun", 10)
|
|
assert_true(i, "Listening \"onfalling\" failed(B)")
|
|
bci:listen_cancel("noun", id)
|
|
i = false
|
|
-- onvalue
|
|
id = bci:listen_noun("noun", "onvalue", 99, function(bc, noun)
|
|
i = true
|
|
end)
|
|
bci:set_noun("noun", 100)
|
|
assert_false(i, "Listening \"onvalue\" failed(A)")
|
|
bci:set_noun("noun", 99)
|
|
assert_true(i, "Listening \"onvalue\" failed(B)")
|
|
bci:listen_cancel("noun", id)
|
|
i = false
|
|
-- onabove
|
|
id = bci:listen_noun("noun", "onabove", 100, function(bc, noun)
|
|
i = true
|
|
end)
|
|
bci:set_noun("noun", 10)
|
|
assert_false(i, "Listening \"onabove\" failed(A)")
|
|
bci:set_noun("noun", 110)
|
|
assert_true(i, "Listening \"onabove\" failed(B)")
|
|
bci:listen_cancel("noun", id)
|
|
i = false
|
|
-- onbelow
|
|
id = bci:listen_noun("noun", "onbelow", 10, function(bc, noun)
|
|
i = true
|
|
end)
|
|
bci:set_noun("noun", 11)
|
|
assert_false(i, "Listening \"onbelow\" failed(A)")
|
|
bci:set_noun("noun", 1)
|
|
assert_true(i, "Listening \"onbelow\" failed(B)")
|
|
bci:listen_cancel("noun", id)
|
|
end
|
|
|
|
function test_get_unknown_noun()
|
|
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()
|
|
-- 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()
|
|
|
|
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()
|
|
|
|
assert_false(bci:has_noun("unknown"))
|
|
bci:listen_noun("unknown", "onchange", nil, function() end)
|
|
end
|
|
|
|
function test_late_install()
|
|
local bc1 = bc:init()
|
|
|
|
local i = false
|
|
|
|
assert_false(bc1:has_noun("foo123"))
|
|
bc1:listen_noun("foo123", "onchange", nil, function()
|
|
i = true
|
|
end)
|
|
|
|
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(nil, {
|
|
["verb"] = function(bc, foo)
|
|
var = var + foo
|
|
end
|
|
})
|
|
local bc2 = bc:init()
|
|
bc2:call_verb("verb", 1)
|
|
assert_equal(1, var, "Verb was not called correctly")
|
|
bc2:call_verb("verb", 2)
|
|
assert_equal(3, var, "Verb was not called correctly(Second attempt)")
|
|
end
|