1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
import eventbus
struct EventData {
data string
}
struct FakeReceiver {
ok bool
}
fn test_eventbus() {
ev_data := &EventData{'hello'}
mut eb := eventbus.new()
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')
eb.clear_all()
assert !eb.has_subscriber('on_test')
assert !eb.subscriber.is_subscribed('on_test')
}
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)
}
fn on_test(receiver voidptr, ev &EventData, sender voidptr) {
assert receiver == 0
assert sender != 0
assert ev.data == 'hello'
}
fn on_test_with_receiver(receiver &FakeReceiver, ev &EventData, sender voidptr) {
assert receiver.ok == false
assert sender != 0
assert ev.data == 'hello'
}
|