v2 / vlib / v / tests / conditions / ifs / check_in_is_consistency_test.v
86 lines · 77 sloc · 1.09 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1type Abc = f64 | int
2
3struct Test {
4 a int
5 b ?int
6}
7
8fn check[T](val T) string {
9 $if T in [?int, ?int] {
10 return 'option int'
11 }
12 $if T in [int, int] {
13 return 'int'
14 }
15 return ''
16}
17
18fn check2[T](val T) string {
19 mut str := string{}
20 $for field in T.fields {
21 $if field.typ in [?int, ?int] {
22 str += 'option int'
23 }
24 $if field.typ in [int, int] {
25 str += 'int'
26 }
27 }
28 return str
29}
30
31fn check_is[T](val T) string {
32 $if T is ?int {
33 return 'option int'
34 }
35 $if T is int {
36 return 'int'
37 }
38 return ''
39}
40
41fn check_is2[T](val T) string {
42 mut str := string{}
43 $for field in T.fields {
44 $if field.typ is ?int {
45 str += 'option int'
46 }
47 $if field.typ is int {
48 str += 'int'
49 }
50 }
51 return str
52}
53
54fn test_in() {
55 var := Test{
56 a: 1
57 b: 2
58 }
59 assert check(var.a) == 'int'
60 assert check(var.b?) == 'int'
61}
62
63fn test_in_2() {
64 var := Test{
65 a: 1
66 b: 2
67 }
68 assert check2(var) == 'intoption int'
69}
70
71fn test_is() {
72 var := Test{
73 a: 1
74 b: 2
75 }
76 assert check_is(var.a) == 'int'
77 assert check_is(var.b?) == 'int'
78}
79
80fn test_is_2() {
81 var := Test{
82 a: 1
83 b: 2
84 }
85 assert check_is2(var) == 'intoption int'
86}
87