v2 / vlib / v / gen / js / tests / alias.v
65 lines · 58 sloc · 1.5 KB · 0181d38a0fe81782e0cff43ada14a302aea88cb6
Raw
1type Type0 = string
2type Type1 = int | string
3type Type2 = string | int
4type Type3 = Type0 | int
5type Type4 = Type3 | Type1 | f32
6type Type5 = Type4 | bool
7
8struct Foo {
9 field_0 Type0
10 field_1 Type1
11 field_2 Type2
12 field_3 Type3
13 field_4 Type4
14 field_5 Type5
15}
16
17fn basic_assertion() {
18 foo := Type1('')
19 assert foo == Type1('')
20}
21
22fn struct_with_default_values() {
23 foo := Foo{}
24 assert foo.field_0 == ''
25 assert foo.field_1 is int && foo.field_1 == 0
26 assert foo.field_2 is string && foo.field_2 == ''
27 assert foo.field_3 is Type0 && foo.field_3 == ''
28 assert foo.field_3 is Type0 && foo.field_3 == Type0('')
29
30 // TODO: uncomment until the C backend is improved
31 // assert foo.field_4 is Type3 && foo.field_4 == Type3(Type0(''))
32 // assert foo.field_5 is Type4 && foo.field_4 == Type4(Type3(Type0('')))
33}
34
35fn struct_with_values() {
36 // test 0
37 f0 := Foo{
38 field_0: 'hello'
39 field_1: 'world'
40 field_2: 100
41 field_3: 200
42 field_4: f32(3.14)
43 field_5: true
44 }
45 assert f0.field_0 == 'hello'
46 assert f0.field_1 is string && f0.field_1 == 'world'
47 assert f0.field_2 is int && f0.field_2 == 100
48 assert f0.field_3 is int && f0.field_3 == 200
49 assert f0.field_4 is f32 && f0.field_4 == 3.14
50 assert f0.field_5 is bool && f0.field_5
51
52 // test 1
53 f1 := Foo{
54 field_4: Type3(100)
55 field_5: Type4(Type3(Type0('hello')))
56 }
57 assert f1.field_4 is Type3 && f1.field_4 == Type3(100)
58 assert f1.field_5 is Type4 && f1.field_5 == Type4(Type3(Type0('hello')))
59}
60
61fn main() {
62 basic_assertion()
63 struct_with_default_values()
64 struct_with_values()
65}
66