v4 / vlib / v / tests / comptime / comptime_map_test.v
209 lines · 189 sloc · 3.26 KB · 04501847a8255570ea06ec003cab3dc86b7f60b8
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[string]StructType {
61 gg := data.$(field.name).clone()
62 for k, v in gg {
63 keys << k.str()
64 vals << v.str()
65 }
66 } $else $if field.typ is map[string]map[string]int {
67 gg := data.$(field.name).clone()
68 for k, v in gg {
69 keys << k.str()
70 vals << v.str()
71 }
72 }
73 }
74 assert keys == ['a', 'b']
75 assert vals[0] == "StructType{
76 val: 'string from StructType'
77}"
78 assert vals[1] == "{'c': 10}"
79}
80
81fn test_comptime_with_dump() {
82 data := Data{
83 users: {
84 'a': StructType{}
85 }
86 extra: {
87 'b': {
88 'c': 10
89 }
90 }
91 }
92
93 $for field in Data.fields {
94 $if field.typ is $map {
95 for k, v in data.$(field.name) {
96 dump(k)
97 dump(v)
98 }
99 }
100 }
101 assert true
102}
103
104fn test_comptime_dump_for_key_and_value() {
105 data := Data{
106 users: {
107 'a': StructType{}
108 }
109 extra: {
110 'b': {
111 'c': 10
112 }
113 }
114 }
115
116 mut key_types := []string{}
117 mut val_types := []string{}
118
119 $for field in Data.fields {
120 $if field.typ is $map {
121 for k, v in data.$(field.name) {
122 key_types << typeof(k).name
123 val_types << typeof(v).name
124 }
125 }
126 }
127 assert val_types[0] == 'StructType'
128 assert val_types[1] == 'map[string]int'
129
130 assert key_types[0] == 'string'
131 assert key_types[1] == 'string'
132}
133
134fn test_comptime_key_value_var() {
135 data := Data{
136 users: {
137 'a': StructType{}
138 }
139 extra: {
140 'b': {
141 'c': 10
142 }
143 }
144 }
145
146 mut keys := []string{}
147 mut vals := []string{}
148
149 $for field in Data.fields {
150 $if field.typ is $map {
151 for k, v in data.$(field.name) {
152 if k == 'a' {
153 keys << dump(k)
154 vals << dump(v.str())
155 }
156 if k == 'b' {
157 keys << dump(k)
158 vals << dump(v.str())
159 }
160 }
161 }
162 }
163 assert keys[0] == 'a'
164 assert keys[1] == 'b'
165
166 assert vals[0] == "StructType{
167 val: 'string from StructType'
168}"
169 assert vals[1] == "{'c': 10}"
170}
171
172fn test_comptime_generic_argument() {
173 data := Data{
174 users: {
175 'a': StructType{}
176 }
177 extra: {
178 'b': {
179 'c': 10
180 }
181 }
182 }
183
184 $for field in Data.fields {
185 $if field.typ is $map {
186 for k, v in data.$(field.name) {
187 process_value_from_map(v)
188 assert k in ['a', 'b']
189 }
190 }
191 }
192}
193
194fn process_value_from_map[T](v T) {
195 $if T is $map {
196 assert typeof(v).name == 'map[string]int'
197 assert v.str() == "{'c': 10}"
198 assert true
199 return
200 } $else $if T is $struct {
201 assert typeof(v).name == 'StructType'
202 assert v.str() == "StructType{
203 val: 'string from StructType'
204}"
205 assert true
206 return
207 }
208 assert false
209}
210