class TestResolve(InferTestCase): #{{{ def setUp(self): num_class = Typez(kind='classdef') num_class.scope['__add__'] = Typez(kind='funcdef') pr_num = Typez(kind='pr_num', __class__=num_class) pr_str = Typez(kind='pr_str') self.scope = Scope(is_root=True) self.scope.update({'xxx': pr_str}) scope1 = Scope(parent=self.scope) scope1.update({'a': pr_num, 'b': pr_str}) self.type1 = Typez(kind='obj', scope=scope1) scope2 = Scope(parent=self.scope) scope2.update({'c': pr_num, 'd': pr_str}) self.type2 = Typez(kind='obj', scope=scope2) self.type2 = scope2 scope12 = Scope(parent=self.scope) scope12.update({'a': self.type1, 'b': self.type2}) self.type12 = Typez(kind='obj', scope=scope12) self.scope.update({ 'type1': self.type1, 'type2': self.type2, 'type12': self.type12 }) def test_resolve_type1_in_scope(self): res = self.scope.resolve('type1', 'straight') self.assertEqual(res, self.type1) def test_resolve_in_type(self): res = self.type1.resolve('a', 'straight') self.assertEqual(res.kind, 'pr_num') self.assertEqual(self.scope.resolve('c'), None) def test_resolve_cascade(self): self.assertRaises(Exception, self.type1.resolve, 'xxx', 'cascade') res1 = self.type1.scope.resolve('xxx', 'cascade') res2 = self.scope.resolve('xxx', 'straight') self.assertEqual(res1, res2) def test_resolve_class(self): num = self.type1.resolve('a', 'straight') self.assertRaises(Exception, num.resolve, '__add__', 'cascade') add = num.resolve('__add__', 'straight') self.assertEqual(add, None) add = num.resolve('__add__', 'class') self.assertIsInstance(add, Typez) self.assertEqual(add.kind, 'funcdef')
class TestResolve(InferTestCase): #{{{ def setUp(self): num_class = Typez(kind = 'classdef') num_class.scope['__add__'] = Typez(kind = 'funcdef') pr_num = Typez(kind = 'pr_num', __class__ = num_class ) pr_str = Typez(kind = 'pr_str') self.scope = Scope(is_root = True) self.scope.update({'xxx':pr_str}) scope1 = Scope(parent = self.scope) scope1.update({'a': pr_num, 'b': pr_str}) self.type1 = Typez(kind = 'obj', scope = scope1) scope2 = Scope(parent = self.scope) scope2.update({'c': pr_num, 'd': pr_str}) self.type2 = Typez(kind = 'obj', scope = scope2) self.type2 = scope2 scope12 = Scope(parent = self.scope) scope12.update({'a':self.type1, 'b': self.type2}) self.type12 = Typez(kind = 'obj', scope = scope12) self.scope.update({'type1':self.type1, 'type2': self.type2, 'type12': self.type12}) def test_resolve_type1_in_scope(self): res = self.scope.resolve('type1', 'straight') self.assertEqual(res, self.type1) def test_resolve_in_type(self): res = self.type1.resolve('a', 'straight') self.assertEqual(res.kind,'pr_num') self.assertEqual(self.scope.resolve('c'), None) def test_resolve_cascade(self): self.assertRaises(Exception, self.type1.resolve, 'xxx','cascade') res1 = self.type1.scope.resolve('xxx','cascade') res2 = self.scope.resolve('xxx','straight') self.assertEqual(res1,res2) def test_resolve_class(self): num = self.type1.resolve('a', 'straight') self.assertRaises(Exception, num.resolve, '__add__', 'cascade') add = num.resolve('__add__', 'straight') self.assertEqual(add, None) add = num.resolve('__add__', 'class') self.assertIsInstance(add, Typez) self.assertEqual(add.kind, 'funcdef')
class TestResolve(unittest.TestCase): #{{{ def setUp(self): pr_num = Typez(kind = 'obj', node = ast.Num(), klass_name = 'num') pr_str = Typez(kind = 'obj', node = ast.Str(), klass_name = 'str') self.scope = Scope(is_root = True) self.scope.update({'xxx':pr_str}) scope1 = Scope(parent = self.scope) scope1.update({'a': pr_num, 'b': pr_str}) self.type1 = Typez(kind = 'obj', scope = scope1) scope2 = Scope(parent = self.scope) scope2.update({'c': pr_num, 'd': pr_str}) self.type2 = Typez(kind = 'obj', scope = scope2) self.type2 = scope2 scope12 = Scope(parent = self.scope) scope12.update({'a':self.type1, 'b': self.type2}) self.type12 = Typez(kind = 'obj', scope = scope12) self.scope.update({'type1':self.type1, 'type2': self.type2, 'type12': self.type12}) def test_resolve_type1_in_scope(self): res = self.scope.resolve('type1', 'straight') self.assertEqual(res, self.type1) def test_resolve_in_type(self): res = self.type1.resolve('a', 'straight') self.assertEqual(res.kind,'obj') self.assertIsInstance(res.node, ast.Num) self.assertEqual(self.scope.resolve('c'), None) def test_resolve_cascade(self): self.assertRaises(Exception, self.type1.resolve, 'xxx','cascade') res1 = self.type1.scope.resolve('xxx','cascade') res2 = self.scope.resolve('xxx','straight') self.assertEqual(res1,res2) def test_resolve_class(self): num = self.type1.resolve('a', 'straight') self.assertRaises(Exception, num.resolve, '__add__', 'cascade') add = num.resolve('__add__', 'straight') self.assertEqual(add, None) add = num.resolve('__add__', 'class') self.assertIsInstance(add, Typez) self.assertEqual(add.kind, 'func') self.assertIsInstance(add.node, ast.FunctionDef)
def setUp(self): num_class = Typez(kind='classdef') num_class.scope['__add__'] = Typez(kind='funcdef') pr_num = Typez(kind='pr_num', __class__=num_class) pr_str = Typez(kind='pr_str') self.scope = Scope(is_root=True) self.scope.update({'xxx': pr_str}) scope1 = Scope(parent=self.scope) scope1.update({'a': pr_num, 'b': pr_str}) self.type1 = Typez(kind='obj', scope=scope1) scope2 = Scope(parent=self.scope) scope2.update({'c': pr_num, 'd': pr_str}) self.type2 = Typez(kind='obj', scope=scope2) self.type2 = scope2 scope12 = Scope(parent=self.scope) scope12.update({'a': self.type1, 'b': self.type2}) self.type12 = Typez(kind='obj', scope=scope12) self.scope.update({ 'type1': self.type1, 'type2': self.type2, 'type12': self.type12 })
def setUp(self): pr_num = Typez(kind = 'obj', node = ast.Num(), klass_name = 'num') pr_str = Typez(kind = 'obj', node = ast.Str(), klass_name = 'str') self.scope = Scope(is_root = True) self.scope.update({'xxx':pr_str}) scope1 = Scope(parent = self.scope) scope1.update({'a': pr_num, 'b': pr_str}) self.type1 = Typez(kind = 'obj', scope = scope1) scope2 = Scope(parent = self.scope) scope2.update({'c': pr_num, 'd': pr_str}) self.type2 = Typez(kind = 'obj', scope = scope2) self.type2 = scope2 scope12 = Scope(parent = self.scope) scope12.update({'a':self.type1, 'b': self.type2}) self.type12 = Typez(kind = 'obj', scope = scope12) self.scope.update({'type1':self.type1, 'type2': self.type2, 'type12': self.type12})
def setUp(self): num_class = Typez(kind = 'classdef') num_class.scope['__add__'] = Typez(kind = 'funcdef') pr_num = Typez(kind = 'pr_num', __class__ = num_class ) pr_str = Typez(kind = 'pr_str') self.scope = Scope(is_root = True) self.scope.update({'xxx':pr_str}) scope1 = Scope(parent = self.scope) scope1.update({'a': pr_num, 'b': pr_str}) self.type1 = Typez(kind = 'obj', scope = scope1) scope2 = Scope(parent = self.scope) scope2.update({'c': pr_num, 'd': pr_str}) self.type2 = Typez(kind = 'obj', scope = scope2) self.type2 = scope2 scope12 = Scope(parent = self.scope) scope12.update({'a':self.type1, 'b': self.type2}) self.type12 = Typez(kind = 'obj', scope = scope12) self.scope.update({'type1':self.type1, 'type2': self.type2, 'type12': self.type12})