7
7
from src .ir .kotlin_types import *
8
8
9
9
10
- def assert_abstract_funcs (actual , expected ):
10
+ def assert_declarations (actual , expected ):
11
11
assert len (actual ) == len (expected )
12
12
table = {f .name : f for f in expected }
13
13
for f in actual :
@@ -183,8 +183,8 @@ def test_get_abstract_functions():
183
183
functions = [func2 ])
184
184
185
185
assert cls1 .get_abstract_functions ([cls1 , cls2 ]) == {func1 }
186
- assert_abstract_funcs (cls2 .get_abstract_functions ([cls1 , cls2 ]),
187
- {func1 , func2 })
186
+ assert_declarations (cls2 .get_abstract_functions ([cls1 , cls2 ]),
187
+ {func1 , func2 })
188
188
189
189
190
190
def test_get_abstract_functions_chain ():
@@ -199,18 +199,17 @@ def test_get_abstract_functions_chain():
199
199
functions = [func2 ])
200
200
201
201
assert cls1 .get_abstract_functions ([cls1 , cls2 , cls3 ]) == {func1 }
202
- assert_abstract_funcs (cls2 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
203
- {func1 })
204
- assert_abstract_funcs (cls3 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
205
- {func1 , func2 })
202
+ assert_declarations (cls2 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
203
+ {func1 })
204
+ assert_declarations (cls3 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
205
+ {func1 , func2 })
206
206
207
207
override_func = deepcopy (func1 )
208
208
func1 .body = IntegerConstant (1 , Integer )
209
209
cls2 .functions = [func1 ]
210
- assert_abstract_funcs (cls2 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
211
- [])
212
- assert_abstract_funcs (cls3 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
213
- {func2 })
210
+ assert_declarations (cls2 .get_abstract_functions ([cls1 , cls2 , cls3 ]), [])
211
+ assert_declarations (cls3 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
212
+ {func2 })
214
213
215
214
216
215
def test_get_abstract_functions_parameterized ():
@@ -232,8 +231,8 @@ def test_get_abstract_functions_parameterized():
232
231
exp_func1 .params [0 ].param_type = String
233
232
exp_func1 .ret_type = String
234
233
exp_func1 .inferred_type = String
235
- assert_abstract_funcs (cls2 .get_abstract_functions ([cls1 , cls2 ]),
236
- [exp_func1 ])
234
+ assert_declarations (cls2 .get_abstract_functions ([cls1 , cls2 ]),
235
+ [exp_func1 ])
237
236
238
237
239
238
def test_get_abstract_functions_parameterized_chain ():
@@ -262,5 +261,79 @@ def test_get_abstract_functions_parameterized_chain():
262
261
exp_func1 .params [0 ].param_type = actual_t
263
262
exp_func1 .ret_type = actual_t
264
263
exp_func1 .inferred_type = actual_t
265
- assert_abstract_funcs (cls2 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
266
- [exp_func1 ])
264
+ assert_declarations (cls2 .get_abstract_functions ([cls1 , cls2 , cls3 ]),
265
+ [exp_func1 ])
266
+
267
+
268
+ def test_get_fields ():
269
+ field1 = FieldDeclaration ("foo" , String )
270
+ field2 = FieldDeclaration ("bar" , Any )
271
+ cls1 = ClassDeclaration ("A" , [], 0 , fields = [field1 ])
272
+ cls2 = ClassDeclaration ("B" ,
273
+ [SuperClassInstantiation (cls1 .get_type (), [])],
274
+ fields = [field2 ])
275
+
276
+ assert cls1 .get_all_fields ([cls1 , cls2 ]) == {field1 }
277
+ assert_declarations (cls2 .get_all_fields ([cls1 , cls2 ]),
278
+ {field1 , field2 })
279
+
280
+ cls1 = ClassDeclaration ("A" , [], fields = [field1 ])
281
+ cls2 = ClassDeclaration ("B" ,
282
+ [SuperClassInstantiation (cls1 .get_type (), [])],
283
+ fields = [])
284
+ cls3 = ClassDeclaration ("C" ,
285
+ [SuperClassInstantiation (cls2 .get_type (), [])],
286
+ fields = [field2 ])
287
+
288
+ assert cls1 .get_all_fields ([cls1 , cls2 , cls3 ]) == {field1 }
289
+ assert_declarations (cls2 .get_all_fields ([cls1 , cls2 , cls3 ]),
290
+ {field1 })
291
+ assert_declarations (cls3 .get_all_fields ([cls1 , cls2 , cls3 ]),
292
+ {field1 , field2 })
293
+
294
+
295
+ def test_get_fields_parameterized ():
296
+ type_param1 = TypeParameter ("T" )
297
+ field1 = FieldDeclaration ("foo" , type_param1 )
298
+ field2 = FieldDeclaration ("bar" , type_param1 )
299
+ cls1 = ClassDeclaration ("A" , [],
300
+ type_parameters = [type_param1 ],
301
+ fields = [field1 , field2 ])
302
+ cls2 = ClassDeclaration (
303
+ "B" , [SuperClassInstantiation (cls1 .get_type ().new ([String ]), [])],
304
+ fields = []
305
+ )
306
+
307
+ assert cls1 .get_all_fields ([cls1 , cls2 ]) == {field1 , field2 }
308
+ exp_field1 = deepcopy (field1 )
309
+ exp_field2 = deepcopy (field2 )
310
+ exp_field1 .field_type = String
311
+ exp_field2 .field_type = String
312
+ assert_declarations (cls2 .get_all_fields ([cls1 , cls2 ]),
313
+ [field1 , field2 ])
314
+
315
+
316
+ cls1 = ClassDeclaration ("A" , [],
317
+ type_parameters = [type_param1 ],
318
+ fields = [field1 ])
319
+
320
+ t_con = TypeConstructor ("Foo" , [TypeParameter ("T" )])
321
+ type_param2 = TypeParameter ("T" )
322
+ field2 = FieldDeclaration ("bar" , type_param2 )
323
+ t = t_con .new ([type_param2 ])
324
+ cls2 = ClassDeclaration (
325
+ "B" , [SuperClassInstantiation (cls1 .get_type ().new ([t ]), [])],
326
+ type_parameters = [type_param2 ],
327
+ fields = [field2 ]
328
+ )
329
+ field3 = FieldDeclaration ("bar" , String )
330
+ cls3 = ClassDeclaration (
331
+ "C" , [SuperClassInstantiation (cls2 .get_type ().new ([String ]), [])],
332
+ fields = [field3 ]
333
+ )
334
+ actual_t = t_con .new ([String ])
335
+ exp_field = deepcopy (field1 )
336
+ exp_field .field_type = actual_t
337
+ assert_declarations (cls2 .get_all_fields ([cls1 , cls2 , cls3 ]),
338
+ [exp_field , field3 ])
339
+
0 commit comments