2020-04-26 13:49:31 +02:00
|
|
|
import eventbus
|
2019-11-23 12:24:25 +01:00
|
|
|
|
2020-01-22 17:41:08 +01:00
|
|
|
struct EventData {
|
2020-10-14 23:39:09 +02:00
|
|
|
data string
|
2020-01-22 17:41:08 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:32:24 +02:00
|
|
|
struct FakeReceiver {
|
|
|
|
ok bool
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:39:09 +02:00
|
|
|
fn test_eventbus() {
|
2020-01-22 17:41:08 +01:00
|
|
|
ev_data := &EventData{'hello'}
|
2019-11-23 12:24:25 +01:00
|
|
|
mut eb := eventbus.new()
|
2020-10-14 23:39:09 +02:00
|
|
|
eb.subscriber.subscribe_once('on_test', on_test)
|
|
|
|
assert eb.has_subscriber('on_test')
|
|
|
|
assert eb.subscriber.is_subscribed('on_test')
|
|
|
|
eb.publish('on_test', eb, ev_data)
|
|
|
|
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()
|
2020-10-14 23:39:09 +02:00
|
|
|
assert !eb.has_subscriber('on_test')
|
|
|
|
assert !eb.subscriber.is_subscribed('on_test')
|
2019-11-23 12:24:25 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:32:24 +02:00
|
|
|
fn test_subscribe_method() {
|
|
|
|
// Does not really test subscribe_method idinvidually though
|
|
|
|
// given
|
|
|
|
mut eb := eventbus.new()
|
|
|
|
r := FakeReceiver{}
|
|
|
|
|
|
|
|
assert !eb.subscriber.is_subscribed_method('on_test_with_receiver', r)
|
|
|
|
// when
|
|
|
|
eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r)
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert eb.subscriber.is_subscribed_method('on_test_with_receiver', r)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_unsubscribe_method() {
|
|
|
|
// given
|
|
|
|
mut eb := eventbus.new()
|
|
|
|
r := FakeReceiver{}
|
|
|
|
r2 := FakeReceiver{}
|
|
|
|
|
|
|
|
// when
|
|
|
|
eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r)
|
|
|
|
eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r2)
|
|
|
|
eb.subscriber.unsubscribe_method('on_test_with_receiver', r)
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert !eb.subscriber.is_subscribed_method('on_test_with_receiver', r)
|
|
|
|
assert eb.subscriber.is_subscribed_method('on_test_with_receiver', r2)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_publish() {
|
|
|
|
// given
|
|
|
|
ev_data := &EventData{'hello'}
|
|
|
|
mut eb := eventbus.new()
|
|
|
|
|
|
|
|
// when
|
|
|
|
eb.subscriber.subscribe_once('on_test', on_test)
|
|
|
|
eb.subscriber.subscribe_once('on_test', on_test)
|
|
|
|
eb.publish('on_test', eb, ev_data)
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert !eb.subscriber.is_subscribed('on_test')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_publish_with_receiver() {
|
|
|
|
// given
|
|
|
|
mut eb := eventbus.new()
|
|
|
|
ev_data := &EventData{'hello'}
|
|
|
|
r := FakeReceiver{}
|
|
|
|
|
|
|
|
// when
|
|
|
|
eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r)
|
|
|
|
eb.publish('on_test_with_receiver', eb, ev_data)
|
|
|
|
|
|
|
|
// then asserts are in on_test_with_receiver, don't know how to be sure
|
|
|
|
// that it has been properly called...
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_unsubscribe_reveiver() {
|
|
|
|
// given
|
|
|
|
mut eb := eventbus.new()
|
|
|
|
r := &FakeReceiver{}
|
|
|
|
|
|
|
|
// when
|
|
|
|
eb.subscriber.subscribe_method('on_test_with_receiver', on_test_with_receiver, r)
|
|
|
|
eb.subscriber.subscribe_method('on_test', on_test, r)
|
|
|
|
eb.subscriber.unsubscribe_receiver(r)
|
|
|
|
assert !eb.subscriber.is_subscribed_method('on_test_with_receiver', r)
|
|
|
|
assert !eb.subscriber.is_subscribed_method('on_test', r)
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:08:16 +02:00
|
|
|
fn on_test(receiver voidptr, ev &EventData, sender voidptr) {
|
|
|
|
assert receiver == 0
|
|
|
|
assert sender != 0
|
2020-10-14 23:39:09 +02:00
|
|
|
assert ev.data == 'hello'
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
2021-03-30 14:32:24 +02:00
|
|
|
|
|
|
|
fn on_test_with_receiver(receiver &FakeReceiver, ev &EventData, sender voidptr) {
|
|
|
|
assert receiver.ok == false
|
|
|
|
assert sender != 0
|
|
|
|
assert ev.data == 'hello'
|
|
|
|
}
|