v2 / vlib / v / tests / options / option_unwrap_fn_test.v
64 lines · 54 sloc · 938 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1@[heap]
2struct Chan[T] {
3 c chan T
4}
5
6pub type EventListener[T] = fn (T) !
7
8pub type Check[T] = fn (T) bool
9
10struct EventWaiter[T] {
11 check ?Check[T]
12 c Chan[T]
13}
14
15pub struct EventController[T] {
16mut:
17 id int
18 wait_fors map[int]EventWaiter[T]
19 listeners map[int]EventListener[T]
20}
21
22fn (mut ec EventController[T]) generate_id() int {
23 return ec.id++
24}
25
26pub fn (ec EventController[T]) emit(e T) bool {
27 for i, w in ec.wait_fors {
28 mut b := false
29 c := w.check
30 if c != none {
31 b = c(e)
32 } else {
33 b = true
34 }
35 println('${i} passed: ${b}')
36 return b
37 }
38 return false
39}
40
41struct Foo {}
42
43struct Bar {}
44
45fn use[T](z EventController[T]) bool {
46 return z.emit(T{})
47}
48
49fn test_main() {
50 assert use(EventController[Foo]{
51 wait_fors: {
52 0: EventWaiter[Foo]{
53 check: fn (t Foo) bool {
54 return false
55 }
56 }
57 }
58 }) == false
59 assert use(EventController[Bar]{
60 wait_fors: {
61 0: EventWaiter[Bar]{}
62 }
63 }) == true
64}
65