def test_applies_generics_for_multiple_levels(self): rules = (Rules().specify(1, ('List', 11)).specify(11, 'Int').instance_of( 2, 1).instance_of(3, 1)) expected_types = { 1: ('List', 11), 2: ('List', 11), 3: ('List', 11), 11: 'Int', } self.assertEqual(Result(expected_types, {}), rules.infer())
def test_applies_generics_recursively(self): rules = (Rules().specify(1, ('Pair', 11, 12)).specify( 2, ('Pair', 21, 22)).specify(11, 'Int').specify( 22, 'String').instance_of(1, 2)) expected_types = { 1: ('Pair', 11, 12), 2: ('Pair', 21, 22), 11: 'Int', 12: 'String', 22: 'String', } self.assertEqual(Result(expected_types, {}), rules.infer())
def test_allows_multiple_generic_instantiations(self): rules = (Rules().specify(1, ('List', 11)).specify( 2, ('List', 21)).specify(3, ('List', 31)).specify(21, 'Int').specify( 31, 'String').instance_of(2, 1).instance_of(3, 1)) expected_types = { 1: ('List', 11), 2: ('List', 21), 3: ('List', 31), 21: 'Int', 31: 'String', } self.assertEqual(Result(expected_types, {}), rules.infer())
def test_literal(self): l = Literal('Int', 123) l_id = l.add_to_rules(self._rules, self._registry) self.assertEqual(Result({l_id: 'Int'}, {}), self._rules.infer())
def test_polymorphic_variable(self): self._registry.push_new_scope({'foo': ('var_foo_1', True)}) v = Variable('foo') v_id = v.add_to_rules(self._rules, self._registry) self.assertEqual(Result({}, {}), self._rules.infer())
def test_complicated_subs_work(self): rules = (Rules().specify(1, 'Int').equal(3, 4).equal(1, 5).equal( 1, 2).equal(5, 2).equal(4, 5)) expected = Result({1: 'Int'}, {2: 1, 3: 1, 4: 1, 5: 1}) self.assertEqual(expected, rules.infer())
def test_generics_with_no_types(self): rules = Rules() rules.instance_of(1, 2) self.assertEqual(Result({}, {}), rules.infer())
def test_applies_substitution(self): self.assertEqual(Result({1: 'Int'}, {2: 1}), Rules().specify(1, 'Int').equal(1, 2).infer()) self.assertEqual( Result({1: 'Int'}, {2: 1}), Rules().specify(1, 'Int').specify(2, 'Int').equal(1, 2).infer())