v2 / vlib / v / tests / comptime / comptime_match_value_check_test.v
114 lines · 105 sloc · 1.21 KB · 603cd903374f915e72df23d559d648f5804ae770
Raw
1const version = 123
2const other = 456
3
4fn test_comptime_match_value_check() {
5 x := version
6 mut result := ''
7
8 $match x {
9 1 {
10 result += 'v1'
11 }
12 2 {
13 result += 'v2'
14 }
15 3 {
16 result += 'v3'
17 }
18 123 {
19 result += 'v123'
20 }
21 $else {
22 result += 'unknown'
23 }
24 }
25 assert result == 'v123'
26
27 result = ''
28 y := true
29 $match y {
30 true {
31 result += 'true'
32 }
33 false {
34 result += 'false'
35 }
36 }
37 assert result == 'true'
38
39 result = ''
40 z := 'abc'
41 $match z {
42 '123' {
43 result += 'a'
44 }
45 'abc' {
46 result += 'b'
47 }
48 $else {
49 result += 'c'
50 }
51 }
52 assert result == 'b'
53}
54
55fn test_comptime_match_value_check_reverse() {
56 x := version
57 y := 124
58 z := 125
59
60 mut result := ''
61 $match 124 {
62 x {
63 result += 'x'
64 }
65 y {
66 result += 'y'
67 }
68 z {
69 result += 'z'
70 }
71 }
72
73 assert result == 'y'
74
75 result = ''
76 a := true
77 b := true
78 c := false
79 $match true {
80 a {
81 result += 'a'
82 }
83 b {
84 result += 'b'
85 }
86 c {
87 result += 'c'
88 }
89 $else {
90 result += 'else'
91 }
92 }
93 assert result == 'a'
94
95 result = ''
96 s1 := '123'
97 s2 := 'abc'
98 s3 := 'kml'
99 $match 'abc' {
100 s1 {
101 result += 'a'
102 }
103 s2 {
104 result += 'b'
105 }
106 s3 {
107 result += 'c'
108 }
109 $else {
110 result += 'else'
111 }
112 }
113 assert result == 'b'
114}
115