v2 / vlib / v / tests / structs / struct_init_with_update_test.v
62 lines · 56 sloc · 901 bytes · 52f078580e47fdfc83ce0fe3c9cffb967b77cfba
Raw
1struct Author {
2 username string
3 name string
4 pass string
5 height int
6 age int
7}
8
9fn cool_author() Author {
10 return Author{
11 username: 'Terisback'
12 name: 'Bob'
13 pass: '123456'
14 height: 175
15 age: 18
16 }
17}
18
19fn test_struct_init_with_update_expr() {
20 mut o := Author{
21 ...cool_author()
22 age: 21
23 }
24 println(o)
25 assert o.username == 'Terisback'
26 assert o.name == 'Bob'
27 assert o.pass == '123456'
28 assert o.height == 175
29 assert o.age == 21
30}
31
32struct Foo {
33 s string
34 n int
35}
36
37fn test_struct_init_with_update_expr2() {
38 f := &Foo{
39 s: 'AA'
40 }
41 b := Foo{
42 ...*f
43 n: 3
44 }
45 println(b)
46 assert b.s == 'AA'
47 assert b.n == 3
48}
49
50struct SharedSpreadAbc {
51 a string
52}
53
54fn test_struct_init_with_update_expr_from_rlock_shared_value() {
55 shared abc := SharedSpreadAbc{'a'}
56 abc_clone := rlock abc {
57 SharedSpreadAbc{
58 ...abc
59 }
60 }
61 assert abc_clone == SharedSpreadAbc{'a'}
62}
63