vq / vlib / v / tests / interfaces / interface_nested_field_test.v
75 lines · 58 sloc · 1.01 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Base {
2}
3
4interface Foo {
5 parent Foo2
6 thing(mut b Base, value i64) string
7}
8
9interface Foo2 {
10 thing(mut b Base, value i64) string
11}
12
13struct Bar {
14 parent Foo2
15}
16
17struct Bar2 {}
18
19fn (f Bar) thing(mut b Base, value i64) string {
20 return 'bar'
21}
22
23fn (f Bar2) thing(mut b Base, value i64) string {
24 return 'bar2'
25}
26
27struct SubBar {
28 parent Foo2 = Bar2{}
29}
30
31fn (f SubBar) thing(mut b Base, value i64) string {
32 return 'subbar'
33}
34
35fn test_interface_nested_field() {
36 mut foo_group := []Foo2{}
37 foo_group << Bar2{}
38 foo_group << SubBar{}
39
40 mut b := Base{}
41 mut ret := []string{}
42 for foo in foo_group {
43 println(foo.thing(mut b, 22))
44 ret << foo.thing(mut b, 22)
45 }
46 assert ret.len == 2
47 assert ret[0] == 'bar2'
48 assert ret[1] == 'subbar'
49}
50
51interface Root {
52 v View
53}
54
55struct MyRoot {
56 v View
57}
58
59interface View {
60 render() int
61}
62
63struct MyView {}
64
65fn (m MyView) render() int {
66 return 24
67}
68
69fn receive_root(r Root) int {
70 return r.v.render()
71}
72
73fn test_nested_interface_fields() {
74 assert receive_root(MyRoot{MyView{}}) == 24
75}
76