def test_apply_wildcard_match(): input_types = ['int', '+', '?'] output_type = 'int' type_rule = TypeRule(input_types, output_type) expression = ['int', '+', 'string'] assert type_rule.apply(expression) == output_type
def import_types(self, typerule_list, variable_types=[]): """ import_types is used to parse and prepare the type rules that will be used in calculating the types of expression trees. Params: - `list` *typerule_list* - A `list` of type rules, specified as: `[[input_types...], output_type]` Types should be specified as strings - `list` *variable_types* - A `list` of variable types, in the same format as the type rules. Returns: - `None` """ # For simplicity, variable types are treated exactly the same as type rules all_type_rules = variable_types + typerule_list # Sort all type rules by their input lengths into the _type_rules dict for type_rule in all_type_rules: self._type_rules[len(type_rule[0])].append( TypeRule(type_rule[0], type_rule[1])) # Add wildcard types as lowest priority for cleanup self._type_rules[1].append(TypeRule(['?'], '?')) self._type_rules[3].append(TypeRule(['(', '?', ')'], '?'))
def test_apply_no_match(): input_types = ['int', '-', 'int'] output_type = 'int' type_rule = TypeRule(input_types, output_type) expression = ['int', '+', 'int'] assert type_rule.apply(expression) is None
def test_init_simple_input(): input_types = ['int', '+', 'int'] output_type = 'int' type_rule = TypeRule(input_types, output_type) assert type_rule._input_types == input_types assert type_rule._output_type == output_type
def test_init_2_input_unary_negation_allowed(): input_types = ['-', 'int'] output_type = 'int' type_rule = TypeRule(input_types, output_type) assert type_rule._input_types == input_types assert type_rule._output_type == output_type
def test_init_wildcard(): input_types = ['?'] output_type = 'int' type_rule = TypeRule(input_types, output_type) assert type_rule._input_types == input_types assert type_rule._output_type == output_type assert type_rule._wildcard_index == 0
def test_apply_wildcard_constant_output(): type_rule = TypeRule(['int', '+', '?'], 'int') result = type_rule._apply_wildcard('string') assert result == 'int'
def test_wildcard_matches_with_wildcard_bad_structure(): type_rule = TypeRule(['int', '-', '?'], 'int') assert not type_rule._wildcard_matches(['int', '+', 'int'])
def test_wildcard_matches_with_wildcard(): type_rule = TypeRule(['int', '+', '?'], 'int') assert type_rule._wildcard_matches(['int', '+', 'int'])
def test_wildcard_matches_no_different_lengths(): type_rule = TypeRule(['?'], '?') result = type_rule._wildcard_matches(['int', '+', 'int']) assert not result
def test_init_2_input_not_unary_negation_not_allowed(): input_types = ['+', 'int'] output_type = 'int' with pytest.raises(AssertionError): type_rule = TypeRule(input_types, output_type)