@@ -77,12 +77,11 @@ def get_big_decimal_type(self):
77
77
def get_null_type (self ):
78
78
return NullType (primitive = False )
79
79
80
- def get_non_nothing_types (self , gen_object ): # Overwriting Parent method to add TS-specific types
81
- types = super ().get_non_nothing_types (gen_object )
80
+ def get_non_nothing_types (self ): # Overwriting Parent method to add TS-specific types
81
+ types = super ().get_non_nothing_types ()
82
82
types .extend ([
83
83
self .get_null_type (),
84
84
UndefinedType (primitive = False ),
85
- union_types .get_union_type (gen_object ),
86
85
] + literal_types .get_literal_types ())
87
86
return types
88
87
@@ -94,7 +93,12 @@ def update_add_node_to_parent(self):
94
93
ts_ast .TypeAliasDeclaration : add_type_alias ,
95
94
}
96
95
97
- def get_constant_candidates (self , gen_object , constants ):
96
+ def get_dynamic_types (self , gen_object ):
97
+ return [
98
+ union_types .get_union_type (gen_object ),
99
+ ]
100
+
101
+ def get_constant_candidates (self , constants ):
98
102
""" Updates the constant candidates of the generator
99
103
with the type-constant pairs for language-specific features.
100
104
@@ -432,6 +436,11 @@ def __init__(self, types, name="UnionType", primitive=False):
432
436
def get_types (self ):
433
437
return self .types
434
438
439
+ def is_assignable (self , other ):
440
+ # TODO revisit this after implementing structural types
441
+ return (isinstance (other , UnionType ) and
442
+ set (other .types ) == set (self .types ))
443
+
435
444
def get_name (self ):
436
445
return self .name
437
446
@@ -452,21 +461,24 @@ def __init__(self, max_ut, max_in_union):
452
461
else len (self .candidates ))
453
462
454
463
def get_number_of_types (self ):
455
- # TODO Perhaps make this user configurable
456
464
return ut .random .integer (2 , self .max_in_union )
457
465
458
- def gen_union_type (self ):
466
+ def gen_union_type (self , gen ):
459
467
""" Generates a union type that consists of
460
468
N types (where N is num_of_types).
461
469
462
470
Args:
463
471
num_of_types - Number of types to be unionized
472
+ gen - Instance of Hephaestus' generator
464
473
"""
465
- # TODO | generate union types with previously
466
- # TODO | generated types (ie. classes, type aliases)
467
474
num_of_types = self .get_number_of_types ()
468
475
assert num_of_types < len (self .candidates )
469
476
types = self .candidates .copy ()
477
+ usr_types = [
478
+ c .get_type ()
479
+ for c in gen .context .get_classes (gen .namespace ).values ()
480
+ ]
481
+ types .extend (usr_types )
470
482
ut .random .shuffle (types )
471
483
types = types [0 :num_of_types ]
472
484
gen_union = UnionType (types )
@@ -485,21 +497,27 @@ def get_union_type(self, gen_object):
485
497
"""
486
498
generated = len (self .unions )
487
499
if generated == 0 :
488
- return self .gen_union_type ()
500
+ return self .gen_union_type (gen_object )
489
501
if generated >= self .max_ut or ut .random .bool ():
490
502
return ut .random .choice (self .unions )
491
- return self .gen_union_type ()
503
+ return self .gen_union_type (gen_object )
492
504
493
505
def get_union_constant (self , utype , constants ):
494
- type_candidates = [t for t in utype .types if t .name in constants ]
495
- """ A union type can have types like 'Object' or 'undefined'
506
+ """ This method randomly chooses one of the types in a type's
507
+ union and then assigns the union a constant value that matches
508
+ the randomly selected type.
509
+
510
+ A union type can have types like 'Object' or 'undefined'
496
511
as part of its union, which however do not have a respective
497
512
constant equivalent.
498
513
499
514
Hence, we only consider types that we can generate a constant
500
515
from. If there is none, we revert to a bottom constant.
501
516
517
+ TODO revisit this after implementing structural types.
518
+
502
519
"""
520
+ type_candidates = [t for t in utype .types if t .name in constants ]
503
521
if len (type_candidates ) == 0 :
504
522
return ast .BottomConstant (utype .types [0 ])
505
523
t = ut .random .choice (type_candidates )
0 commit comments