v2 / vlib / v / tests / structs / struct_embed_test.v
226 lines · 181 sloc · 2.7 KB · 8e35f4d9848f7ad35d857a187dddbfd2eca5e19d
Raw
1import flag
2import v.tests.field_publicity
3
4struct Foo {
5 x int
6 y int = 5
7}
8
9fn (f Foo) foo() {}
10
11struct Bar {
12 Foo
13}
14
15fn test_embed() {
16 b := Bar{}
17 assert b.x == 0
18 b.foo()
19}
20
21fn test_embed_direct_access() {
22 b := Bar{
23 Foo: Foo{}
24 }
25 assert b.Foo.y == 5
26}
27
28fn test_default_value() {
29 b := Bar{
30 Foo: Foo{}
31 }
32 assert b.y == 5
33}
34
35fn test_default_value_without_init() {
36 b := Bar{}
37 assert b.y == 5
38}
39
40fn test_initialize() {
41 b := Bar{
42 x: 1
43 y: 2
44 }
45 assert b.x == 1
46 assert b.y == 2
47}
48
49struct Bar3 {
50 Foo
51 y string = 'test'
52}
53
54fn test_overwrite_field() {
55 b := Bar3{}
56 assert b.y == 'test'
57}
58
59struct TestEmbedFromModule {
60 flag.Flag
61}
62
63struct BarGeneric[T] {
64pub:
65 foo T
66}
67
68struct BarGenericContainer {
69 BarGeneric[int]
70}
71
72fn test_generic_embed() {
73 b := BarGenericContainer{}
74 assert b.BarGeneric.foo == 0
75 assert b.foo == 0
76}
77
78struct Upper {
79mut:
80 x int
81}
82
83struct UpperHolder {
84 Upper
85}
86
87fn test_assign() {
88 mut h := UpperHolder{}
89 h.x = 5
90 assert h.x == 5
91}
92
93fn test_embed_is_public() {
94 a := field_publicity.App{}
95 assert a.Context.name == ''
96}
97
98struct Eggs {
99 name string
100}
101
102fn (f &Eggs) test(x int) int {
103 return x
104}
105
106struct Breakfast {
107 Eggs
108}
109
110fn (b &Breakfast) name() string {
111 return b.name
112}
113
114fn test_embed_method_receiver_ptr() {
115 b := Breakfast{}
116 assert b.test(5) == 5
117}
118
119fn test_embed_field_receiver_ptr() {
120 b := Breakfast{}
121 assert b.name() == ''
122}
123
124fn test_embed_mutable() {
125 mut a := field_publicity.App{}
126 a.Context = field_publicity.Context{}
127}
128
129struct Context {
130 static_files string
131}
132
133fn (c Context) test() bool {
134 return true
135}
136
137struct App {
138 Context
139}
140
141fn embed_field_access_generic[T](mut app T) {
142 app.Context = Context{
143 static_files: app.static_files
144 }
145}
146
147fn test_embed_field_access_generic() {
148 mut app := App{}
149 embed_field_access_generic(mut app)
150}
151
152fn embed_method_generic[T](app T) bool {
153 return app.test()
154}
155
156fn test_embed_method_generic() {
157 mut app := App{}
158 assert embed_method_generic(app)
159}
160
161type Piece = King | Queen
162
163struct Pos {
164 x u8
165 y u8
166}
167
168enum TeamEnum {
169 black
170 white
171}
172
173struct PieceCommonFields {
174 pos Pos
175 team TeamEnum
176}
177
178fn (p PieceCommonFields) get_pos() Pos {
179 return p.pos
180}
181
182struct King {
183 PieceCommonFields
184}
185
186struct Queen {
187 PieceCommonFields
188}
189
190fn (piece Piece) pos() Pos {
191 mut pos := Pos{}
192 match piece {
193 King, Queen { pos = piece.pos }
194 }
195
196 return pos
197}
198
199fn (piece Piece) get_pos() Pos {
200 mut pos := Pos{}
201 match piece {
202 King, Queen { pos = piece.get_pos() }
203 }
204
205 return pos
206}
207
208fn test_match_aggregate_field() {
209 piece := Piece(King{
210 pos: Pos{1, 8}
211 team: .black
212 })
213 pos := piece.pos()
214 assert pos.x == 1
215 assert pos.y == 8
216}
217
218fn test_match_aggregate_method() {
219 piece := Piece(King{
220 pos: Pos{1, 8}
221 team: .black
222 })
223 pos := piece.get_pos()
224 assert pos.x == 1
225 assert pos.y == 8
226}
227