v2 / vlib / v / tests / interfaces / interface_embedding_method_call_test.v
59 lines · 49 sloc · 814 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1fn test_interface_embedding_method_call() {
2 mut window := &Window{}
3 btn := &Button{}
4 window.initables << btn
5 window.run()
6}
7
8@[heap]
9pub struct Window {
10mut:
11 initables []Initable
12 popview PopView = DummyPopup{}
13}
14
15interface Initable {
16mut:
17 init(&Window)
18}
19
20interface Drawable {
21 draw()
22}
23
24pub fn (mut window Window) run() {
25 for mut i in window.initables {
26 i.init(window)
27 }
28 for wd in window.initables {
29 if wd is Drawable {
30 d := wd as Drawable
31 d.draw()
32 }
33 }
34 window.popview.draw()
35}
36
37struct DummyPopup {}
38
39fn (d DummyPopup) draw() {}
40
41interface PopView {
42 Drawable
43}
44
45@[heap]
46pub struct Button {
47mut:
48 window &Window = unsafe { nil }
49}
50
51pub fn (mut b Button) init(window &Window) {
52 b.window = window
53}
54
55pub fn (b Button) draw() {
56 g := b.window.initables
57 println(g.len)
58 assert g.len == 1
59}
60