v2 / vlib / v / tests / builtin_arrays / array_map_ref_it_test.v
35 lines · 30 sloc · 701 bytes · f59d34892a629e4c4f2080f329684633106acc30
Raw
1struct MapRefShapes {
2mut:
3 rectangles []MapRefRectangle
4}
5
6struct MapRefRectangle {
7mut:
8 id int
9 square bool
10}
11
12fn (shapes MapRefShapes) get_squares() []&MapRefRectangle {
13 return shapes.rectangles.filter(it.square).filter(it.id > 0).map(&it)
14}
15
16fn test_array_map_ref_it_after_filter_keeps_struct_fields() {
17 mut shapes := MapRefShapes{}
18 shapes.rectangles << MapRefRectangle{
19 id: 1
20 square: true
21 }
22 shapes.rectangles << MapRefRectangle{
23 id: 2
24 square: false
25 }
26
27 squares := shapes.get_squares()
28 rendered := '${squares}'
29
30 assert squares.len == 1
31 assert squares[0].id == 1
32 assert squares[0].square
33 assert rendered.contains('id: 1')
34 assert rendered.contains('square: true')
35}
36