v2 / vlib / v / tests / comptime / comptime_map_test.v
203 lines · 183 sloc · 3.09 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct StructType {
2mut:
3 val string = 'string from StructType'
4}
5
6struct Data {
7mut:
8 not_map string
9 empty_to_test map[string]string
10 users map[string]StructType
11 extra map[string]map[string]int
12}
13
14fn test_comptimeselector_map_different_types() {
15 data := Data{
16 users: {
17 'a': StructType{}
18 }
19 extra: {
20 'b': {
21 'c': 10
22 }
23 }
24 }
25
26 mut keys := []string{}
27 mut vals := []string{}
28
29 $for field in Data.fields {
30 $if field.typ is $map {
31 for k, v in data.$(field.name) {
32 keys << k.str()
33 vals << v.str()
34 }
35 }
36 }
37 assert keys == ['a', 'b']
38 assert vals[0] == "StructType{
39 val: 'string from StructType'
40}"
41 assert vals[1] == "{'c': 10}"
42}
43
44fn test_comptime_var_map_different_types() {
45 data := Data{
46 users: {
47 'a': StructType{}
48 }
49 extra: {
50 'b': {
51 'c': 10
52 }
53 }
54 }
55
56 mut keys := []string{}
57 mut vals := []string{}
58
59 $for field in Data.fields {
60 $if field.typ is $map {
61 gg := data.$(field.name)
62 for k, v in gg {
63 keys << k.str()
64 vals << v.str()
65 }
66 }
67 }
68 assert keys == ['a', 'b']
69 assert vals[0] == "StructType{
70 val: 'string from StructType'
71}"
72 assert vals[1] == "{'c': 10}"
73}
74
75fn test_comptime_with_dump() {
76 data := Data{
77 users: {
78 'a': StructType{}
79 }
80 extra: {
81 'b': {
82 'c': 10
83 }
84 }
85 }
86
87 $for field in Data.fields {
88 $if field.typ is $map {
89 for k, v in data.$(field.name) {
90 dump(k)
91 dump(v)
92 }
93 }
94 }
95 assert true
96}
97
98fn test_comptime_dump_for_key_and_value() {
99 data := Data{
100 users: {
101 'a': StructType{}
102 }
103 extra: {
104 'b': {
105 'c': 10
106 }
107 }
108 }
109
110 mut key_types := []string{}
111 mut val_types := []string{}
112
113 $for field in Data.fields {
114 $if field.typ is $map {
115 for k, v in data.$(field.name) {
116 key_types << typeof(k).name
117 val_types << typeof(v).name
118 }
119 }
120 }
121 assert val_types[0] == 'StructType'
122 assert val_types[1] == 'map[string]int'
123
124 assert key_types[0] == 'string'
125 assert key_types[1] == 'string'
126}
127
128fn test_comptime_key_value_var() {
129 data := Data{
130 users: {
131 'a': StructType{}
132 }
133 extra: {
134 'b': {
135 'c': 10
136 }
137 }
138 }
139
140 mut keys := []string{}
141 mut vals := []string{}
142
143 $for field in Data.fields {
144 $if field.typ is $map {
145 for k, v in data.$(field.name) {
146 if k == 'a' {
147 keys << dump(k)
148 vals << dump(v.str())
149 }
150 if k == 'b' {
151 keys << dump(k)
152 vals << dump(v.str())
153 }
154 }
155 }
156 }
157 assert keys[0] == 'a'
158 assert keys[1] == 'b'
159
160 assert vals[0] == "StructType{
161 val: 'string from StructType'
162}"
163 assert vals[1] == "{'c': 10}"
164}
165
166fn test_comptime_generic_argument() {
167 data := Data{
168 users: {
169 'a': StructType{}
170 }
171 extra: {
172 'b': {
173 'c': 10
174 }
175 }
176 }
177
178 $for field in Data.fields {
179 $if field.typ is $map {
180 for k, v in data.$(field.name) {
181 process_value_from_map(v)
182 assert k in ['a', 'b']
183 }
184 }
185 }
186}
187
188fn process_value_from_map[T](v T) {
189 $if T is $map {
190 assert typeof(v).name == 'map[string]int'
191 assert v.str() == "{'c': 10}"
192 assert true
193 return
194 } $else $if T is $struct {
195 assert typeof(v).name == 'StructType'
196 assert v.str() == "StructType{
197 val: 'string from StructType'
198}"
199 assert true
200 return
201 }
202 assert false
203}
204