v4 / vlib / v / tests / aliases / chained_alias_test.v
53 lines · 44 sloc · 900 bytes · 299887e3f23aa21cccf9db95f51bb63f6e7d113d
Raw
1// Chained aliases (`type B = A` where `A` is itself an alias, #27055) let
2// modules re-export types from their dependencies without leaking the original
3// module name to callers.
4
5type Id = int
6type UserId = Id
7type AdminId = UserId
8
9struct Box {
10 v int
11}
12
13type Wrapped = Box
14type DoubleWrapped = Wrapped
15
16fn test_primitive_chain() {
17 a := AdminId(42)
18 b := UserId(a)
19 c := Id(b)
20 assert int(c) == 42
21 assert int(a) == 42
22}
23
24fn test_struct_chain() {
25 dw := DoubleWrapped{
26 v: 7
27 }
28 w := Wrapped(dw)
29 b := Box(w)
30 assert b.v == 7
31 assert dw.v == 7
32}
33
34fn test_chain_assignment_compat() {
35 x := AdminId(1)
36 mut y := Id(0)
37 y = Id(x)
38 assert int(y) == 1
39}
40
41struct Item {
42 v int
43}
44
45type ItemPair = [2]Item
46type ItemPairAlias = ItemPair
47
48fn test_chain_through_fixed_array_of_non_builtin() {
49 a := ItemPair([Item{ v: 1 }, Item{ v: 2 }]!)
50 b := ItemPairAlias(a)
51 assert b[0].v == 1
52 assert b[1].v == 2
53}
54