v2 / vlib / v / tests / structs / struct_init_with_embed_update_test.v
32 lines · 26 sloc · 493 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Button {
2 width int
3 height int
4}
5
6enum Color {
7 red
8 blue
9 yellow
10}
11
12struct ColoredButton {
13 Button
14 color Color
15}
16
17fn change_color(cb ColoredButton, color Color) ColoredButton {
18 return ColoredButton{
19 ...cb
20 color: color
21 }
22}
23
24fn test_struct_update_with_embed_field() {
25 red_button := ColoredButton{Button{100, 100}, .red}
26 blue_button := change_color(red_button, .blue)
27
28 println(red_button)
29 println(blue_button)
30
31 assert blue_button == ColoredButton{Button{100, 100}, .blue}
32}
33