v/vlib/eventbus/eventbus_test.v

64 lines
1.5 KiB
V
Raw Normal View History

2019-11-23 12:24:25 +01:00
import (
eventbus
)
fn test_eventbus(){
mut eb := eventbus.new()
eb.subscriber.subscribe_once("on_test", on_test)
2019-12-18 06:16:33 +01:00
assert eb.has_subscriber("on_test")
assert eb.subscriber.is_subscribed("on_test")
2019-11-23 12:24:25 +01:00
mut params := eventbus.Params{}
params.put_string("eventbus", "vevent")
2019-12-18 06:16:33 +01:00
eb.publish("on_test", eb, params)
assert !eb.has_subscriber("on_test")
assert !eb.subscriber.is_subscribed("on_test")
eb.subscriber.subscribe("on_test", on_test)
assert eb.has_subscriber("on_test")
assert eb.subscriber.is_subscribed("on_test")
2019-11-23 12:24:25 +01:00
eb.clear_all()
2019-12-18 06:16:33 +01:00
assert !eb.has_subscriber("on_test")
assert !eb.subscriber.is_subscribed("on_test")
2019-11-23 12:24:25 +01:00
}
fn test_params(){
mut params := eventbus.Params{}
2019-12-18 06:16:33 +01:00
params.put_string("string", "some_string")
2019-11-23 12:24:25 +01:00
params.put_int("int", 20)
2019-12-18 06:16:33 +01:00
params.put_bool("bool", true)
arr := [1,2,3]
params.put_array("array", arr)
mp := {"hello": "world"}
params.put_map("map", mp)
2019-11-23 12:24:25 +01:00
2019-12-18 06:16:33 +01:00
assert params.get_string("string") == "some_string"
2019-11-23 12:24:25 +01:00
assert params.get_int("int") == 20
2019-12-18 06:16:33 +01:00
assert params.get_bool("bool") == true
g_arr := params.get_array("array", 0)
assert g_arr[0] == 1
g_m := params.get_map("map", "")
assert g_m["hello"] == "world"
2019-11-23 12:24:25 +01:00
}
2019-12-18 06:16:33 +01:00
fn on_test(sender voidptr, p eventbus.Params) {
mut eb := *(*eventbus.EventBus(sender))
eb.subscriber.subscribe("on_test_2", on_test_2)
eb.clear_all()
assert !eb.has_subscriber("on_test_2")
assert !eb.subscriber.is_subscribed("on_test_2")
2019-11-23 12:24:25 +01:00
assert p.get_string("eventbus") == "vevent"
2019-12-01 08:33:26 +01:00
}
2019-12-18 06:16:33 +01:00
fn on_test_2(sender voidptr, p eventbus.Params){}