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.

69 lines
2.3 KiB

require "lunit"
bc = require("bc")
ser = require("serialization")
module("test_bc", package.seeall, lunit.testcase)
function test_init()
bci = bc:init("a1", {["light"]=false}, {["toggle_light"]=function() end})
end
function test_set_get_noun()
bci = bc:init("a1", {["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()
bci = bc:init("a1", {["light"]=true}, {["toggle_light"]=function() end})
local i = false
local expct = true
remote = require("modem"):init("some_addr", function(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"}))
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"}))
assert_true(i, "Noun response did not happen or was incorrect")
end
function test_call_verb()
bci = bc:init("a1", {["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)
-- Call verb
remote:send("a1", 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()
bc1 = bc:init("a1", {["light"]=true}, {["toggle_light"]=function(b)
b:set_noun("light", not b:get_noun("light"))
end})
bc2 = bc:init("a2", {}, {})
bc1:call_verb("toggle_light")
assert_equal(false, bc1:get_noun("light"), "First verb invokation did not go to plan")
bc2:call_verb("toggle_light")
assert_equal(true, bc1:get_noun("light"), "Second verb invokation did not go to plan")
end
function test_multinode_get_noun()
key = "foobar"
bc1 = bc:init("a1", {["light"]=key}, {["toggle_light"]=function(b)
b:set_noun("light", not b:get_noun("light"))
end})
bc2 = bc:init("a2", {}, {})
assert_equal(key, bc1:get_noun("light"), "Local get failed")
assert_equal(key, bc2:get_noun("light"), "Remote get failed")
end