Best Python code snippet using tempest_python
advanced_oi_test.py
Source:advanced_oi_test.py
...21 'a_var = a_func(a_func)\n'22 mod.write(code)23 self.pycore.run_module(mod).wait_process()24 pymod = self.project.get_pymodule(mod)25 self.assertEqual(pymod['a_func'].get_object(),26 pymod['a_var'].get_object())27 def test_module_dti(self):28 mod1 = testutils.create_module(self.project, 'mod1')29 mod2 = testutils.create_module(self.project, 'mod2')30 code = 'import mod1\ndef a_func(arg):\n return eval("arg")\n' \31 'a_var = a_func(mod1)\n'32 mod2.write(code)33 self.pycore.run_module(mod2).wait_process()34 pymod2 = self.project.get_pymodule(mod2)35 self.assertEqual(self.project.get_pymodule(mod1),36 pymod2['a_var'].get_object())37 def test_class_from_another_module_dti(self):38 mod1 = testutils.create_module(self.project, 'mod1')39 mod2 = testutils.create_module(self.project, 'mod2')40 code1 = 'class AClass(object):\n pass\n'41 code2 = 'from mod1 import AClass\n' \42 '\ndef a_func(arg):\n return eval("arg")\n' \43 'a_var = a_func(AClass)\n'44 mod1.write(code1)45 mod2.write(code2)46 self.pycore.run_module(mod2).wait_process()47 #pymod1 = self.project.get_pymodule(mod1)48 pymod2 = self.project.get_pymodule(mod2)49 self.assertEqual(pymod2['AClass'].get_object(),50 pymod2['a_var'].get_object())51 def test_class_dti(self):52 mod = testutils.create_module(self.project, 'mod')53 code = 'class AClass(object):\n pass\n' \54 '\ndef a_func(arg):\n return eval("arg")\n' \55 'a_var = a_func(AClass)\n'56 mod.write(code)57 self.pycore.run_module(mod).wait_process()58 pymod = self.project.get_pymodule(mod)59 self.assertEqual(pymod['AClass'].get_object(),60 pymod['a_var'].get_object())61 def test_instance_dti(self):62 mod = testutils.create_module(self.project, 'mod')63 code = 'class AClass(object):\n pass\n' \64 '\ndef a_func(arg):\n return eval("arg()")\n' \65 'a_var = a_func(AClass)\n'66 mod.write(code)67 self.pycore.run_module(mod).wait_process()68 pymod = self.project.get_pymodule(mod)69 self.assertEqual(pymod['AClass'].get_object(),70 pymod['a_var'].get_object().get_type())71 def test_method_dti(self):72 mod = testutils.create_module(self.project, 'mod')73 code = 'class AClass(object):\n def a_method(self, arg):\n' \74 ' return eval("arg()")\n' \75 'an_instance = AClass()\n' \76 'a_var = an_instance.a_method(AClass)\n'77 mod.write(code)78 self.pycore.run_module(mod).wait_process()79 pymod = self.project.get_pymodule(mod)80 self.assertEqual(pymod['AClass'].get_object(),81 pymod['a_var'].get_object().get_type())82 def test_function_argument_dti(self):83 mod = testutils.create_module(self.project, 'mod')84 code = 'def a_func(arg):\n pass\n' \85 'a_func(a_func)\n'86 mod.write(code)87 self.pycore.run_module(mod).wait_process()88 pyscope = self.project.get_pymodule(mod).get_scope()89 self.assertEqual(pyscope['a_func'].get_object(),90 pyscope.get_scopes()[0]['arg'].get_object())91 def test_classes_with_the_same_name(self):92 mod = testutils.create_module(self.project, 'mod')93 code = 'def a_func(arg):\n class AClass(object):\n' \94 ' pass\n return eval("arg")\n' \95 'class AClass(object):\n pass\n' \96 'a_var = a_func(AClass)\n'97 mod.write(code)98 self.pycore.run_module(mod).wait_process()99 pymod = self.project.get_pymodule(mod)100 self.assertEqual(pymod['AClass'].get_object(),101 pymod['a_var'].get_object())102 def test_nested_classes(self):103 mod = testutils.create_module(self.project, 'mod')104 code = 'def a_func():\n class AClass(object):\n' \105 ' pass\n return AClass\n' \106 'def another_func(arg):\n return eval("arg")\n' \107 'a_var = another_func(a_func())\n'108 mod.write(code)109 self.pycore.run_module(mod).wait_process()110 pyscope = self.project.get_pymodule(mod).get_scope()111 self.assertEqual(pyscope.get_scopes()[0]['AClass'].get_object(),112 pyscope['a_var'].get_object())113 def test_function_argument_dti2(self):114 mod = testutils.create_module(self.project, 'mod')115 code = 'def a_func(arg, a_builtin_type):\n pass\n' \116 'a_func(a_func, [])\n'117 mod.write(code)118 self.pycore.run_module(mod).wait_process()119 pyscope = self.project.get_pymodule(mod).get_scope()120 self.assertEqual(pyscope['a_func'].get_object(),121 pyscope.get_scopes()[0]['arg'].get_object())122 def test_dti_and_concluded_data_invalidation(self):123 mod = testutils.create_module(self.project, 'mod')124 code = 'def a_func(arg):\n return eval("arg")\n' \125 'a_var = a_func(a_func)\n'126 mod.write(code)127 pymod = self.project.get_pymodule(mod)128 pymod['a_var'].get_object()129 self.pycore.run_module(mod).wait_process()130 self.assertEqual(pymod['a_func'].get_object(),131 pymod['a_var'].get_object())132 def test_list_objects_and_dynamicoi(self):133 mod = testutils.create_module(self.project, 'mod')134 code = 'class C(object):\n pass\n' \135 'def a_func(arg):\n return eval("arg")\n' \136 'a_var = a_func([C()])[0]\n'137 mod.write(code)138 self.pycore.run_module(mod).wait_process()139 pymod = self.project.get_pymodule(mod)140 c_class = pymod['C'].get_object()141 a_var = pymod['a_var'].get_object()142 self.assertEqual(c_class, a_var.get_type())143 def test_for_loops_and_dynamicoi(self):144 mod = testutils.create_module(self.project, 'mod')145 code = 'class C(object):\n pass\n' \146 'def a_func(arg):\n return eval("arg")\n' \147 'for c in a_func([C()]):\n a_var = c\n'148 mod.write(code)149 self.pycore.run_module(mod).wait_process()150 pymod = self.project.get_pymodule(mod)151 c_class = pymod['C'].get_object()152 a_var = pymod['a_var'].get_object()153 self.assertEqual(c_class, a_var.get_type())154 def test_dict_objects_and_dynamicoi(self):155 mod = testutils.create_module(self.project, 'mod')156 code = 'class C(object):\n pass\n' \157 'def a_func(arg):\n return eval("arg")\n' \158 'a_var = a_func({1: C()})[1]\n'159 mod.write(code)160 self.pycore.run_module(mod).wait_process()161 pymod = self.project.get_pymodule(mod)162 c_class = pymod['C'].get_object()163 a_var = pymod['a_var'].get_object()164 self.assertEqual(c_class, a_var.get_type())165 def test_dict_keys_and_dynamicoi(self):166 mod = testutils.create_module(self.project, 'mod')167 if pycompat.PY3:168 code = 'class C(object):\n pass\n' \169 'def a_func(arg):\n return eval("arg")\n' \170 'a_var = list(a_func({C(): 1}))[0]\n'171 else:172 code = 'class C(object):\n pass\n' \173 'def a_func(arg):\n return eval("arg")\n' \174 'a_var = a_func({C(): 1}).keys()[0]\n'175 mod.write(code)176 self.pycore.run_module(mod).wait_process()177 pymod = self.project.get_pymodule(mod)178 c_class = pymod['C'].get_object()179 a_var = pymod['a_var'].get_object()180 self.assertEqual(c_class, a_var.get_type())181 def test_dict_keys_and_dynamicoi2(self):182 mod = testutils.create_module(self.project, 'mod')183 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \184 'def a_func(arg):\n return eval("arg")\n' \185 'a, b = a_func((C1(), C2()))\n'186 mod.write(code)187 self.pycore.run_module(mod).wait_process()188 pymod = self.project.get_pymodule(mod)189 c1_class = pymod['C1'].get_object()190 c2_class = pymod['C2'].get_object()191 a_var = pymod['a'].get_object()192 b_var = pymod['b'].get_object()193 self.assertEqual(c1_class, a_var.get_type())194 self.assertEqual(c2_class, b_var.get_type())195 def test_strs_and_dynamicoi(self):196 mod = testutils.create_module(self.project, 'mod')197 code = 'def a_func(arg):\n return eval("arg")\n' \198 'a_var = a_func("hey")\n'199 mod.write(code)200 self.pycore.run_module(mod).wait_process()201 pymod = self.project.get_pymodule(mod)202 a_var = pymod['a_var'].get_object()203 self.assertTrue(isinstance(a_var.get_type(), rope.base.builtins.Str))204 def test_textual_transformations(self):205 mod = testutils.create_module(self.project, 'mod')206 code = 'class C(object):\n pass\ndef f():' \207 '\n pass\na_var = C()\n' \208 'a_list = [C()]\na_str = "hey"\na_file = open("file.txt")\n'209 mod.write(code)210 to_pyobject = rope.base.oi.transform.TextualToPyObject(self.project)211 to_textual = rope.base.oi.transform.PyObjectToTextual(self.project)212 pymod = self.project.get_pymodule(mod)213 def complex_to_textual(pyobject):214 return to_textual.transform(215 to_pyobject.transform(to_textual.transform(pyobject)))216 test_variables = [217 ('C', ('defined', 'mod.py', 'C')),218 ('f', ('defined', 'mod.py', 'f')),219 ('a_var', ('instance', ('defined', 'mod.py', 'C'))),220 ('a_list',221 ('builtin', 'list', ('instance', ('defined', 'mod.py', 'C')))),222 ('a_str', ('builtin', 'str')),223 ('a_file', ('builtin', 'file')),224 ]225 test_cases = [(pymod[v].get_object(), r) for v, r in test_variables]226 test_cases += [227 (pymod, ('defined', 'mod.py')),228 (rope.base.builtins.builtins['enumerate'].get_object(),229 ('builtin', 'function', 'enumerate'))230 ]231 for var, result in test_cases:232 self.assertEqual(to_textual.transform(var), result)233 self.assertEqual(complex_to_textual(var), result)234 def test_arguments_with_keywords(self):235 mod = testutils.create_module(self.project, 'mod')236 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \237 'def a_func(arg):\n return eval("arg")\n' \238 'a = a_func(arg=C1())\nb = a_func(arg=C2())\n'239 mod.write(code)240 self.pycore.run_module(mod).wait_process()241 pymod = self.project.get_pymodule(mod)242 c1_class = pymod['C1'].get_object()243 c2_class = pymod['C2'].get_object()244 a_var = pymod['a'].get_object()245 b_var = pymod['b'].get_object()246 self.assertEqual(c1_class, a_var.get_type())247 self.assertEqual(c2_class, b_var.get_type())248 def test_a_function_with_different_returns(self):249 mod = testutils.create_module(self.project, 'mod')250 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \251 'def a_func(arg):\n return eval("arg")\n' \252 'a = a_func(C1())\nb = a_func(C2())\n'253 mod.write(code)254 self.pycore.run_module(mod).wait_process()255 pymod = self.project.get_pymodule(mod)256 c1_class = pymod['C1'].get_object()257 c2_class = pymod['C2'].get_object()258 a_var = pymod['a'].get_object()259 b_var = pymod['b'].get_object()260 self.assertEqual(c1_class, a_var.get_type())261 self.assertEqual(c2_class, b_var.get_type())262 def test_a_function_with_different_returns2(self):263 mod = testutils.create_module(self.project, 'mod')264 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \265 'def a_func(p):\n if p == C1:\n return C1()\n' \266 ' else:\n return C2()\n' \267 'a = a_func(C1)\nb = a_func(C2)\n'268 mod.write(code)269 self.pycore.run_module(mod).wait_process()270 pymod = self.project.get_pymodule(mod)271 c1_class = pymod['C1'].get_object()272 c2_class = pymod['C2'].get_object()273 a_var = pymod['a'].get_object()274 b_var = pymod['b'].get_object()275 self.assertEqual(c1_class, a_var.get_type())276 self.assertEqual(c2_class, b_var.get_type())277 def test_ignoring_star_args(self):278 mod = testutils.create_module(self.project, 'mod')279 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \280 'def a_func(p, *args):' \281 '\n if p == C1:\n return C1()\n' \282 ' else:\n return C2()\n' \283 'a = a_func(C1, 1)\nb = a_func(C2, 2)\n'284 mod.write(code)285 self.pycore.run_module(mod).wait_process()286 pymod = self.project.get_pymodule(mod)287 c1_class = pymod['C1'].get_object()288 c2_class = pymod['C2'].get_object()289 a_var = pymod['a'].get_object()290 b_var = pymod['b'].get_object()291 self.assertEqual(c1_class, a_var.get_type())292 self.assertEqual(c2_class, b_var.get_type())293 def test_ignoring_double_star_args(self):294 mod = testutils.create_module(self.project, 'mod')295 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \296 'def a_func(p, *kwds, **args):\n ' \297 'if p == C1:\n return C1()\n' \298 ' else:\n return C2()\n' \299 'a = a_func(C1, kwd=1)\nb = a_func(C2, kwd=2)\n'300 mod.write(code)301 self.pycore.run_module(mod).wait_process()302 pymod = self.project.get_pymodule(mod)303 c1_class = pymod['C1'].get_object()304 c2_class = pymod['C2'].get_object()305 a_var = pymod['a'].get_object()306 b_var = pymod['b'].get_object()307 self.assertEqual(c1_class, a_var.get_type())308 self.assertEqual(c2_class, b_var.get_type())309 def test_invalidating_data_after_changing(self):310 mod = testutils.create_module(self.project, 'mod')311 code = 'def a_func(arg):\n return eval("arg")\n' \312 'a_var = a_func(a_func)\n'313 mod.write(code)314 self.pycore.run_module(mod).wait_process()315 mod.write(code.replace('a_func', 'newfunc'))316 mod.write(code)317 pymod = self.project.get_pymodule(mod)318 self.assertNotEqual(pymod['a_func'].get_object(),319 pymod['a_var'].get_object())320 def test_invalidating_data_after_moving(self):321 mod2 = testutils.create_module(self.project, 'mod2')322 mod2.write('class C(object):\n pass\n')323 mod = testutils.create_module(self.project, 'mod')324 code = 'import mod2\ndef a_func(arg):\n return eval(arg)\n' \325 'a_var = a_func("mod2.C")\n'326 mod.write(code)327 self.pycore.run_module(mod).wait_process()328 mod.move('newmod.py')329 pymod = self.project.get_module('newmod')330 pymod2 = self.project.get_pymodule(mod2)331 self.assertEqual(pymod2['C'].get_object(),332 pymod['a_var'].get_object())333class NewStaticOITest(unittest.TestCase):334 def setUp(self):335 super(NewStaticOITest, self).setUp()336 self.project = testutils.sample_project(validate_objectdb=True)337 self.pycore = self.project.pycore338 self.mod = testutils.create_module(self.project, 'mod')339 def tearDown(self):340 testutils.remove_project(self.project)341 super(NewStaticOITest, self).tearDown()342 def test_static_oi_for_simple_function_calls(self):343 code = 'class C(object):\n pass\ndef f(p):\n pass\nf(C())\n'344 self.mod.write(code)345 self.pycore.analyze_module(self.mod)346 pymod = self.project.get_pymodule(self.mod)347 c_class = pymod['C'].get_object()348 f_scope = pymod['f'].get_object().get_scope()349 p_type = f_scope['p'].get_object().get_type()350 self.assertEqual(c_class, p_type)351 def test_static_oi_not_failing_when_callin_callables(self):352 code = 'class C(object):\n pass\nC()\n'353 self.mod.write(code)354 self.pycore.analyze_module(self.mod)355 def test_static_oi_for_nested_calls(self):356 code = 'class C(object):\n pass\ndef f(p):\n pass\n' \357 'def g(p):\n return p\nf(g(C()))\n'358 self.mod.write(code)359 self.pycore.analyze_module(self.mod)360 pymod = self.project.get_pymodule(self.mod)361 c_class = pymod['C'].get_object()362 f_scope = pymod['f'].get_object().get_scope()363 p_type = f_scope['p'].get_object().get_type()364 self.assertEqual(c_class, p_type)365 def test_static_oi_class_methods(self):366 code = 'class C(object):\n def f(self, p):\n pass\n' \367 'C().f(C())'368 self.mod.write(code)369 self.pycore.analyze_module(self.mod)370 pymod = self.project.get_pymodule(self.mod)371 c_class = pymod['C'].get_object()372 f_scope = c_class['f'].get_object().get_scope()373 p_type = f_scope['p'].get_object().get_type()374 self.assertEqual(c_class, p_type)375 def test_static_oi_preventing_soi_maximum_recursion_exceptions(self):376 code = 'item = {}\nfor item in item.keys():\n pass\n'377 self.mod.write(code)378 try:379 self.pycore.analyze_module(self.mod)380 except RuntimeError as e:381 self.fail(str(e))382 def test_static_oi_for_infer_return_typs_from_funcs_based_on_params(self):383 code = 'class C(object):\n pass\ndef func(p):\n return p\n' \384 'a_var = func(C())\n'385 self.mod.write(code)386 pymod = self.project.get_pymodule(self.mod)387 c_class = pymod['C'].get_object()388 a_var = pymod['a_var'].get_object()389 self.assertEqual(c_class, a_var.get_type())390 def test_a_function_with_different_returns(self):391 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \392 'def a_func(arg):\n return arg\n' \393 'a = a_func(C1())\nb = a_func(C2())\n'394 self.mod.write(code)395 pymod = self.project.get_pymodule(self.mod)396 c1_class = pymod['C1'].get_object()397 c2_class = pymod['C2'].get_object()398 a_var = pymod['a'].get_object()399 b_var = pymod['b'].get_object()400 self.assertEqual(c1_class, a_var.get_type())401 self.assertEqual(c2_class, b_var.get_type())402 def test_not_reporting_out_of_date_information(self):403 code = 'class C1(object):\n pass\n' \404 'def f(arg):\n return C1()\na_var = f('')\n'405 self.mod.write(code)406 pymod = self.project.get_pymodule(self.mod)407 c1_class = pymod['C1'].get_object()408 a_var = pymod['a_var'].get_object()409 self.assertEqual(c1_class, a_var.get_type())410 self.mod.write(code.replace('C1', 'C2'))411 pymod = self.project.get_pymodule(self.mod)412 c2_class = pymod['C2'].get_object()413 a_var = pymod['a_var'].get_object()414 self.assertEqual(c2_class, a_var.get_type())415 def test_invalidating_concluded_data_in_a_function(self):416 mod1 = testutils.create_module(self.project, 'mod1')417 mod2 = testutils.create_module(self.project, 'mod2')418 mod1.write('def func(arg):\n temp = arg\n return temp\n')419 mod2.write('import mod1\n'420 'class C1(object):\n pass\n'421 'class C2(object):\n pass\n'422 'a_var = mod1.func(C1())\n')423 pymod2 = self.project.get_pymodule(mod2)424 c1_class = pymod2['C1'].get_object()425 a_var = pymod2['a_var'].get_object()426 self.assertEqual(c1_class, a_var.get_type())427 mod2.write(mod2.read()[:mod2.read().rfind('C1()')] + 'C2())\n')428 pymod2 = self.project.get_pymodule(mod2)429 c2_class = pymod2['C2'].get_object()430 a_var = pymod2['a_var'].get_object()431 self.assertEqual(c2_class, a_var.get_type())432 def test_handling_generator_functions_for_strs(self):433 self.mod.write('class C(object):\n pass\ndef f(p):\n yield p()\n'434 'for c in f(C):\n a_var = c\n')435 pymod = self.project.get_pymodule(self.mod)436 c_class = pymod['C'].get_object()437 a_var = pymod['a_var'].get_object()438 self.assertEqual(c_class, a_var.get_type())439 # TODO: Returning a generator for functions that yield unknowns440 @unittest.skip("Returning a generator that yields unknowns")441 def xxx_test_handl_generator_functions_when_unknown_type_is_yielded(self):442 self.mod.write('class C(object):\n pass'443 '\ndef f():\n yield eval("C()")\n'444 'a_var = f()\n')445 pymod = self.project.get_pymodule(self.mod)446 a_var = pymod['a_var'].get_object()447 self.assertTrue(isinstance(a_var.get_type(),448 rope.base.builtins.Generator))449 def test_static_oi_for_lists_depending_on_append_function(self):450 code = 'class C(object):\n pass\nl = list()\n' \451 'l.append(C())\na_var = l.pop()\n'452 self.mod.write(code)453 self.pycore.analyze_module(self.mod)454 pymod = self.project.get_pymodule(self.mod)455 c_class = pymod['C'].get_object()456 a_var = pymod['a_var'].get_object()457 self.assertEqual(c_class, a_var.get_type())458 def test_static_oi_for_lists_per_object_for_get_item(self):459 code = 'class C(object):\n pass\nl = list()\n' \460 'l.append(C())\na_var = l[0]\n'461 self.mod.write(code)462 self.pycore.analyze_module(self.mod)463 pymod = self.project.get_pymodule(self.mod)464 c_class = pymod['C'].get_object()465 a_var = pymod['a_var'].get_object()466 self.assertEqual(c_class, a_var.get_type())467 def test_static_oi_for_lists_per_object_for_fields(self):468 code = 'class C(object):\n pass\n' \469 'class A(object):\n ' \470 'def __init__(self):\n self.l = []\n' \471 ' def set(self):\n self.l.append(C())\n' \472 'a = A()\na.set()\na_var = a.l[0]\n'473 self.mod.write(code)474 self.pycore.analyze_module(self.mod)475 pymod = self.project.get_pymodule(self.mod)476 c_class = pymod['C'].get_object()477 a_var = pymod['a_var'].get_object()478 self.assertEqual(c_class, a_var.get_type())479 def test_static_oi_for_lists_per_object_for_set_item(self):480 code = 'class C(object):\n pass\nl = [None]\n' \481 'l[0] = C()\na_var = l[0]\n'482 self.mod.write(code)483 self.pycore.analyze_module(self.mod)484 pymod = self.project.get_pymodule(self.mod)485 c_class = pymod['C'].get_object()486 a_var = pymod['a_var'].get_object()487 self.assertEqual(c_class, a_var.get_type())488 def test_static_oi_for_lists_per_object_for_extending_lists(self):489 code = 'class C(object):\n pass\nl = []\n' \490 'l.append(C())\nl2 = []\nl2.extend(l)\na_var = l2[0]\n'491 self.mod.write(code)492 self.pycore.analyze_module(self.mod)493 pymod = self.project.get_pymodule(self.mod)494 c_class = pymod['C'].get_object()495 a_var = pymod['a_var'].get_object()496 self.assertEqual(c_class, a_var.get_type())497 def test_static_oi_for_lists_per_object_for_iters(self):498 code = 'class C(object):\n pass\n' \499 'l = []\nl.append(C())\n' \500 'for c in l:\n a_var = c\n'501 self.mod.write(code)502 self.pycore.analyze_module(self.mod)503 pymod = self.project.get_pymodule(self.mod)504 c_class = pymod['C'].get_object()505 a_var = pymod['a_var'].get_object()506 self.assertEqual(c_class, a_var.get_type())507 def test_static_oi_for_dicts_depending_on_append_function(self):508 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \509 'd = {}\nd[C1()] = C2()\na, b = d.popitem()\n'510 self.mod.write(code)511 self.pycore.analyze_module(self.mod)512 pymod = self.project.get_pymodule(self.mod)513 c1_class = pymod['C1'].get_object()514 c2_class = pymod['C2'].get_object()515 a_var = pymod['a'].get_object()516 b_var = pymod['b'].get_object()517 self.assertEqual(c1_class, a_var.get_type())518 self.assertEqual(c2_class, b_var.get_type())519 def test_static_oi_for_dicts_depending_on_for_loops(self):520 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \521 'd = {}\nd[C1()] = C2()\n' \522 'for k, v in d.items():\n a = k\n b = v\n'523 self.mod.write(code)524 self.pycore.analyze_module(self.mod)525 pymod = self.project.get_pymodule(self.mod)526 c1_class = pymod['C1'].get_object()527 c2_class = pymod['C2'].get_object()528 a_var = pymod['a'].get_object()529 b_var = pymod['b'].get_object()530 self.assertEqual(c1_class, a_var.get_type())531 self.assertEqual(c2_class, b_var.get_type())532 def test_static_oi_for_dicts_depending_on_update(self):533 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \534 'd = {}\nd[C1()] = C2()\n' \535 'd2 = {}\nd2.update(d)\na, b = d2.popitem()\n'536 self.mod.write(code)537 self.pycore.analyze_module(self.mod)538 pymod = self.project.get_pymodule(self.mod)539 c1_class = pymod['C1'].get_object()540 c2_class = pymod['C2'].get_object()541 a_var = pymod['a'].get_object()542 b_var = pymod['b'].get_object()543 self.assertEqual(c1_class, a_var.get_type())544 self.assertEqual(c2_class, b_var.get_type())545 def test_static_oi_for_dicts_depending_on_update_on_seqs(self):546 code = 'class C1(object):\n pass\nclass C2(object):\n pass\n' \547 'd = {}\nd.update([(C1(), C2())])\na, b = d.popitem()\n'548 self.mod.write(code)549 self.pycore.analyze_module(self.mod)550 pymod = self.project.get_pymodule(self.mod)551 c1_class = pymod['C1'].get_object()552 c2_class = pymod['C2'].get_object()553 a_var = pymod['a'].get_object()554 b_var = pymod['b'].get_object()555 self.assertEqual(c1_class, a_var.get_type())556 self.assertEqual(c2_class, b_var.get_type())557 def test_static_oi_for_sets_per_object_for_set_item(self):558 code = 'class C(object):\n pass\ns = set()\n' \559 's.add(C())\na_var = s.pop() \n'560 self.mod.write(code)561 self.pycore.analyze_module(self.mod)562 pymod = self.project.get_pymodule(self.mod)563 c_class = pymod['C'].get_object()564 a_var = pymod['a_var'].get_object()565 self.assertEqual(c_class, a_var.get_type())566 def test_properties_and_calling_get_property(self):567 code = 'class C1(object):\n pass\n' \568 'class C2(object):\n c1 = C1()\n' \569 ' def get_c1(self):\n return self.c1\n' \570 ' p = property(get_c1)\nc2 = C2()\na_var = c2.p\n'571 self.mod.write(code)572 pymod = self.project.get_pymodule(self.mod)573 c1_class = pymod['C1'].get_object()574 a_var = pymod['a_var'].get_object()575 self.assertEqual(c1_class, a_var.get_type())576 def test_soi_on_constructors(self):577 code = 'class C1(object):\n pass\n' \578 'class C2(object):\n' \579 ' def __init__(self, arg):\n self.attr = arg\n' \580 'c2 = C2(C1())\na_var = c2.attr'581 self.mod.write(code)582 self.pycore.analyze_module(self.mod)583 pymod = self.project.get_pymodule(self.mod)584 c1_class = pymod['C1'].get_object()585 a_var = pymod['a_var'].get_object()586 self.assertEqual(c1_class, a_var.get_type())587 def test_soi_on_literal_assignment(self):588 code = 'a_var = ""'589 self.mod.write(code)590 self.pycore.analyze_module(self.mod)591 pymod = self.project.get_pymodule(self.mod)592 a_var = pymod['a_var'].get_object()593 self.assertEqual(Str, type(a_var.get_type()))594 @testutils.only_for_versions_higher('3.6')595 def test_soi_on_typed_assignment(self):596 code = 'a_var: str'597 self.mod.write(code)598 self.pycore.analyze_module(self.mod)599 pymod = self.project.get_pymodule(self.mod)600 a_var = pymod['a_var'].get_object()601 self.assertEqual(Str, type(a_var.get_type()))602 def test_not_saving_unknown_function_returns(self):603 mod2 = testutils.create_module(self.project, 'mod2')604 self.mod.write('class C(object):\n pass\nl = []\nl.append(C())\n')605 mod2.write('import mod\ndef f():\n '606 'return mod.l.pop()\na_var = f()\n')607 pymod = self.project.get_pymodule(self.mod)608 pymod2 = self.project.get_pymodule(mod2)609 c_class = pymod['C'].get_object()610 a_var = pymod2['a_var']611 self.pycore.analyze_module(mod2)612 self.assertNotEqual(c_class, a_var.get_object().get_type())613 self.pycore.analyze_module(self.mod)614 self.assertEqual(c_class, a_var.get_object().get_type())615 def test_using_the_best_callinfo(self):616 code = 'class C1(object):\n pass\n' \617 'def f(arg1, arg2, arg3):\n pass\n' \618 'f("", None, C1())\nf("", C1(), None)\n'619 self.mod.write(code)620 self.pycore.analyze_module(self.mod)621 pymod = self.project.get_pymodule(self.mod)622 c1_class = pymod['C1'].get_object()623 f_scope = pymod['f'].get_object().get_scope()624 arg2 = f_scope['arg2'].get_object()625 self.assertEqual(c1_class, arg2.get_type())626 def test_call_function_and_parameters(self):627 code = 'class A(object):\n def __call__(self, p):\n pass\n' \628 'A()("")\n'629 self.mod.write(code)630 self.pycore.analyze_module(self.mod)631 scope = self.project.get_pymodule(self.mod).get_scope()632 p_object = scope.get_scopes()[0].get_scopes()[0]['p'].get_object()633 self.assertTrue(isinstance(p_object.get_type(),634 rope.base.builtins.Str))635 def test_report_change_in_libutils(self):636 self.project.prefs['automatic_soa'] = True637 code = 'class C(object):\n pass\ndef f(p):\n pass\nf(C())\n'638 with open(self.mod.real_path, 'w') as mod_file:639 mod_file.write(code)640 rope.base.libutils.report_change(self.project, self.mod.real_path, '')641 pymod = self.project.get_pymodule(self.mod)642 c_class = pymod['C'].get_object()643 f_scope = pymod['f'].get_object().get_scope()644 p_type = f_scope['p'].get_object().get_type()645 self.assertEqual(c_class, p_type)646 def test_report_libutils_and_analyze_all_modules(self):647 code = 'class C(object):\n pass\ndef f(p):\n pass\nf(C())\n'648 self.mod.write(code)649 rope.base.libutils.analyze_modules(self.project)650 pymod = self.project.get_pymodule(self.mod)651 c_class = pymod['C'].get_object()652 f_scope = pymod['f'].get_object().get_scope()653 p_type = f_scope['p'].get_object().get_type()654 self.assertEqual(c_class, p_type)655 def test_validation_problems_for_objectdb_retrievals(self):656 mod1 = testutils.create_module(self.project, 'mod1')657 mod2 = testutils.create_module(self.project, 'mod2')658 mod1.write('l = []\nvar = l.pop()\n')659 mod2.write('import mod1\n\nclass C(object):\n pass\n'660 'mod1.l.append(C())\n')661 self.pycore.analyze_module(mod2)662 pymod2 = self.project.get_pymodule(mod2)663 c_class = pymod2['C'].get_object()664 pymod1 = self.project.get_pymodule(mod1)665 var_pyname = pymod1['var']666 self.assertEqual(c_class, var_pyname.get_object().get_type())667 mod2.write('import mod1\n\nmod1.l.append("")\n')668 self.assertNotEqual(c_class, var_pyname.get_object().get_type(),669 'Class `C` no more exists')670 def test_validation_problems_for_changing_builtin_types(self):671 mod1 = testutils.create_module(self.project, 'mod1')672 mod1.write('l = []\nl.append("")\n')673 self.pycore.analyze_module(mod1)674 mod1.write('l = {}\nv = l["key"]\n')675 pymod1 = self.project.get_pymodule(mod1) # noqa676 var = pymod1['v'].get_object() # noqa677 def test_always_returning_containing_class_for_selfs(self):678 code = 'class A(object):\n def f(p):\n return p\n' \679 'class B(object):\n pass\nb = B()\nb.f()\n'680 self.mod.write(code)681 self.pycore.analyze_module(self.mod)682 pymod = self.project.get_pymodule(self.mod)683 a_class = pymod['A'].get_object()684 f_scope = a_class.get_scope().get_scopes()[0]685 p_type = f_scope['p'].get_object().get_type()686 self.assertEqual(a_class, p_type)687 def test_following_function_calls_when_asked_to(self):688 code = 'class A(object):\n pass\n' \689 'class C(object):\n' \690 ' def __init__(self, arg):\n' \691 ' self.attr = arg\n' \692 'def f(p):\n return C(p)\n' \693 'c = f(A())\nx = c.attr\n'694 self.mod.write(code)695 self.pycore.analyze_module(self.mod, followed_calls=1)696 pymod = self.project.get_pymodule(self.mod)697 a_class = pymod['A'].get_object()698 x_var = pymod['x'].get_object().get_type()699 self.assertEqual(a_class, x_var)700def suite():701 result = unittest.TestSuite()702 result.addTests(unittest.makeSuite(DynamicOITest))703 result.addTests(unittest.makeSuite(NewStaticOITest))704 return result705if __name__ == '__main__':...
builtinstest.py
Source:builtinstest.py
...16 super(BuiltinTypesTest, self).tearDown()17 def test_simple_case(self):18 self.mod.write('l = []\n')19 pymod = self.project.get_pymodule(self.mod)20 self.assertTrue('append' in pymod['l'].get_object())21 def test_holding_type_information(self):22 self.mod.write('class C(object):\n pass\n'23 'l = [C()]\na_var = l.pop()\n')24 pymod = self.project.get_pymodule(self.mod)25 c_class = pymod['C'].get_object()26 a_var = pymod['a_var'].get_object()27 self.assertEqual(c_class, a_var.get_type())28 def test_get_items(self):29 self.mod.write('class C(object):'30 '\n def __getitem__(self, i):\n return C()\n'31 'c = C()\na_var = c[0]')32 pymod = self.project.get_pymodule(self.mod)33 c_class = pymod['C'].get_object()34 a_var = pymod['a_var'].get_object()35 self.assertEqual(c_class, a_var.get_type())36 def test_get_items_for_lists(self):37 self.mod.write('class C(object):\n pass\nl = [C()]\na_var = l[0]\n')38 pymod = self.project.get_pymodule(self.mod)39 c_class = pymod['C'].get_object()40 a_var = pymod['a_var'].get_object()41 self.assertEqual(c_class, a_var.get_type())42 def test_get_items_from_slices(self):43 self.mod.write('class C(object):\n pass'44 '\nl = [C()]\na_var = l[:].pop()\n')45 pymod = self.project.get_pymodule(self.mod)46 c_class = pymod['C'].get_object()47 a_var = pymod['a_var'].get_object()48 self.assertEqual(c_class, a_var.get_type())49 def test_simple_for_loops(self):50 self.mod.write('class C(object):\n pass\nl = [C()]\n'51 'for c in l:\n a_var = c\n')52 pymod = self.project.get_pymodule(self.mod)53 c_class = pymod['C'].get_object()54 a_var = pymod['a_var'].get_object()55 self.assertEqual(c_class, a_var.get_type())56 def test_definition_location_for_loop_variables(self):57 self.mod.write('class C(object):\n pass\nl = [C()]\n'58 'for c in l:\n pass\n')59 pymod = self.project.get_pymodule(self.mod)60 c_var = pymod['c']61 self.assertEqual((pymod, 4), c_var.get_definition_location())62 def test_simple_case_for_dicts(self):63 self.mod.write('d = {}\n')64 pymod = self.project.get_pymodule(self.mod)65 self.assertTrue('get' in pymod['d'].get_object())66 def test_get_item_for_dicts(self):67 self.mod.write('class C(object):\n pass\n'68 'd = {1: C()}\na_var = d[1]\n')69 pymod = self.project.get_pymodule(self.mod)70 c_class = pymod['C'].get_object()71 a_var = pymod['a_var'].get_object()72 self.assertEqual(c_class, a_var.get_type())73 def test_dict_function_parent(self):74 self.mod.write('d = {1: 2}\n'75 'a_var = d.keys()')76 pymod = self.project.get_pymodule(self.mod)77 a_var = pymod['d'].get_object()['keys'].get_object()78 self.assertEqual(type(a_var.parent), Dict)79 def test_popping_dicts(self):80 self.mod.write('class C(object):\n pass\n'81 'd = {1: C()}\na_var = d.pop(1)\n')82 pymod = self.project.get_pymodule(self.mod)83 c_class = pymod['C'].get_object()84 a_var = pymod['a_var'].get_object()85 self.assertEqual(c_class, a_var.get_type())86 def test_getting_keys_from_dicts(self):87 self.mod.write('class C1(object):\n pass\n'88 'class C2(object):\n pass\n'89 'd = {C1(): C2()}\nfor c in d.keys():\n a_var = c\n')90 pymod = self.project.get_pymodule(self.mod)91 c_class = pymod['C1'].get_object()92 a_var = pymod['a_var'].get_object()93 self.assertEqual(c_class, a_var.get_type())94 def test_getting_values_from_dicts(self):95 self.mod.write('class C1(object):\n pass\n'96 'class C2(object):\n pass\n'97 'd = {C1(): C2()}\nfor c in d.values():'98 '\n a_var = c\n')99 pymod = self.project.get_pymodule(self.mod)100 c_class = pymod['C2'].get_object()101 a_var = pymod['a_var'].get_object()102 self.assertEqual(c_class, a_var.get_type())103 def test_getting_iterkeys_from_dicts(self):104 self.mod.write('class C1(object):\n pass'105 '\nclass C2(object):\n pass\n'106 'd = {C1(): C2()}\nfor c in d.keys():\n a_var = c\n')107 pymod = self.project.get_pymodule(self.mod)108 c_class = pymod['C1'].get_object()109 a_var = pymod['a_var'].get_object()110 self.assertEqual(c_class, a_var.get_type())111 def test_getting_itervalues_from_dicts(self):112 self.mod.write('class C1(object):\n pass'113 '\nclass C2(object):\n pass\n'114 'd = {C1(): C2()}\nfor c in d.values():'115 '\n a_var = c\n')116 pymod = self.project.get_pymodule(self.mod)117 c_class = pymod['C2'].get_object()118 a_var = pymod['a_var'].get_object()119 self.assertEqual(c_class, a_var.get_type())120 def test_using_copy_for_dicts(self):121 self.mod.write('class C1(object):\n pass'122 '\nclass C2(object):\n pass\n'123 'd = {C1(): C2()}\nfor c in d.copy():\n a_var = c\n')124 pymod = self.project.get_pymodule(self.mod)125 c_class = pymod['C1'].get_object()126 a_var = pymod['a_var'].get_object()127 self.assertEqual(c_class, a_var.get_type())128 def test_tuple_assignments_for_items(self):129 self.mod.write('class C1(object):\n pass'130 '\nclass C2(object):\n pass\n'131 'd = {C1(): C2()}\nkey, value = d.items()[0]\n')132 pymod = self.project.get_pymodule(self.mod)133 c1_class = pymod['C1'].get_object()134 c2_class = pymod['C2'].get_object()135 key = pymod['key'].get_object()136 value = pymod['value'].get_object()137 self.assertEqual(c1_class, key.get_type())138 self.assertEqual(c2_class, value.get_type())139 def test_tuple_assignment_for_lists(self):140 self.mod.write('class C(object):\n pass\n'141 'l = [C(), C()]\na, b = l\n')142 pymod = self.project.get_pymodule(self.mod)143 c_class = pymod['C'].get_object()144 a_var = pymod['a'].get_object()145 b_var = pymod['b'].get_object()146 self.assertEqual(c_class, a_var.get_type())147 self.assertEqual(c_class, b_var.get_type())148 def test_tuple_assignments_for_iteritems_in_fors(self):149 self.mod.write('class C1(object):\n pass\n'150 'class C2(object):\n pass\n'151 'd = {C1(): C2()}\n'152 'for x, y in d.items():\n a = x;\n b = y\n')153 pymod = self.project.get_pymodule(self.mod)154 c1_class = pymod['C1'].get_object()155 c2_class = pymod['C2'].get_object()156 a_var = pymod['a'].get_object()157 b_var = pymod['b'].get_object()158 self.assertEqual(c1_class, a_var.get_type())159 self.assertEqual(c2_class, b_var.get_type())160 def test_simple_tuple_assignments(self):161 self.mod.write('class C1(object):'162 '\n pass\nclass C2(object):\n pass\n'163 'a, b = C1(), C2()\n')164 pymod = self.project.get_pymodule(self.mod)165 c1_class = pymod['C1'].get_object()166 c2_class = pymod['C2'].get_object()167 a_var = pymod['a'].get_object()168 b_var = pymod['b'].get_object()169 self.assertEqual(c1_class, a_var.get_type())170 self.assertEqual(c2_class, b_var.get_type())171 def test_overriding_builtin_names(self):172 self.mod.write('class C(object):\n pass\nlist = C\n')173 pymod = self.project.get_pymodule(self.mod)174 c_class = pymod['C'].get_object()175 list_var = pymod['list'].get_object()176 self.assertEqual(c_class, list_var)177 def test_simple_builtin_scope_test(self):178 self.mod.write('l = list()\n')179 pymod = self.project.get_pymodule(self.mod)180 self.assertTrue('append' in pymod['l'].get_object())181 def test_simple_sets(self):182 self.mod.write('s = set()\n')183 pymod = self.project.get_pymodule(self.mod)184 self.assertTrue('add' in pymod['s'].get_object())185 def test_making_lists_using_the_passed_argument_to_init(self):186 self.mod.write('class C(object):\n pass\nl1 = [C()]\n'187 'l2 = list(l1)\na_var = l2.pop()')188 pymod = self.project.get_pymodule(self.mod)189 c_class = pymod['C'].get_object()190 a_var = pymod['a_var'].get_object()191 self.assertEqual(c_class, a_var.get_type())192 def test_making_tuples_using_the_passed_argument_to_init(self):193 self.mod.write('class C(object):\n pass\nl1 = [C()]\n'194 'l2 = tuple(l1)\na_var = l2[0]')195 pymod = self.project.get_pymodule(self.mod)196 c_class = pymod['C'].get_object()197 a_var = pymod['a_var'].get_object()198 self.assertEqual(c_class, a_var.get_type())199 def test_making_sets_using_the_passed_argument_to_init(self):200 self.mod.write('class C(object):\n pass\nl1 = [C()]\n'201 'l2 = set(l1)\na_var = l2.pop()')202 pymod = self.project.get_pymodule(self.mod)203 c_class = pymod['C'].get_object()204 a_var = pymod['a_var'].get_object()205 self.assertEqual(c_class, a_var.get_type())206 def test_making_dicts_using_the_passed_argument_to_init(self):207 self.mod.write('class C1(object):\n pass\n'208 'class C2(object):\n pass\n'209 'l1 = [(C1(), C2())]\n'210 'l2 = dict(l1)\na, b = l2.items()[0]')211 pymod = self.project.get_pymodule(self.mod)212 c1_class = pymod['C1'].get_object()213 c2_class = pymod['C2'].get_object()214 a_var = pymod['a'].get_object()215 b_var = pymod['b'].get_object()216 self.assertEqual(c1_class, a_var.get_type())217 self.assertEqual(c2_class, b_var.get_type())218 def test_range_builtin_function(self):219 self.mod.write('l = range(1)\n')220 pymod = self.project.get_pymodule(self.mod)221 l = pymod['l'].get_object()222 self.assertTrue('append' in l)223 def test_reversed_builtin_function(self):224 self.mod.write('class C(object):\n pass\nl = [C()]\n'225 'for x in reversed(l):\n a_var = x\n')226 pymod = self.project.get_pymodule(self.mod)227 c_class = pymod['C'].get_object()228 a_var = pymod['a_var'].get_object()229 self.assertEqual(c_class, a_var.get_type())230 def test_sorted_builtin_function(self):231 self.mod.write('class C(object):\n pass\nl = [C()]\n'232 'a_var = sorted(l).pop()\n')233 pymod = self.project.get_pymodule(self.mod)234 c_class = pymod['C'].get_object()235 a_var = pymod['a_var'].get_object()236 self.assertEqual(c_class, a_var.get_type())237 def test_super_builtin_function(self):238 self.mod.write(239 'class C(object):\n pass\n'240 'class A(object):\n def a_f(self):\n return C()\n'241 'class B(A):\n def b_f(self):\n '242 'return super(B, self).a_f()\n'243 'a_var = B.b_f()\n')244 pymod = self.project.get_pymodule(self.mod)245 c_class = pymod['C'].get_object()246 a_var = pymod['a_var'].get_object()247 self.assertEqual(c_class, a_var.get_type())248 def test_file_builtin_type(self):249 self.mod.write('for line in open("file.txt"):\n a_var = line\n')250 pymod = self.project.get_pymodule(self.mod)251 a_var = pymod['a_var'].get_object()252 self.assertTrue(isinstance(a_var.get_type(), builtins.Str))253 def test_property_builtin_type(self):254 self.mod.write('p = property()\n')255 pymod = self.project.get_pymodule(self.mod)256 p_var = pymod['p'].get_object()257 self.assertTrue('fget' in p_var)258 def test_lambda_functions(self):259 self.mod.write('l = lambda: 1\n')260 pymod = self.project.get_pymodule(self.mod)261 l_var = pymod['l'].get_object()262 self.assertEqual(pyobjects.get_base_type('Function'),263 l_var.get_type())264 def test_lambda_function_definition(self):265 self.mod.write('l = lambda x, y = 2, *a, **b: x + y\n')266 pymod = self.project.get_pymodule(self.mod)267 l_var = pymod['l'].get_object()268 self.assertTrue(l_var.get_name() is not None)269 self.assertEqual(len(l_var.get_param_names()), 4)270 self.assertEqual((pymod, 1),271 pymod['l'].get_definition_location())272 def test_lambdas_that_return_unknown(self):273 self.mod.write('a_var = (lambda: None)()\n')274 pymod = self.project.get_pymodule(self.mod)275 a_var = pymod['a_var'].get_object()276 self.assertTrue(a_var is not None)277 def test_builtin_zip_function(self):278 self.mod.write(279 'class C1(object):\n pass\nclass C2(object):\n pass\n'280 'c1_list = [C1()]\nc2_list = [C2()]\n'281 'a, b = zip(c1_list, c2_list)[0]')282 pymod = self.project.get_pymodule(self.mod)283 c1_class = pymod['C1'].get_object()284 c2_class = pymod['C2'].get_object()285 a_var = pymod['a'].get_object()286 b_var = pymod['b'].get_object()287 self.assertEqual(c1_class, a_var.get_type())288 self.assertEqual(c2_class, b_var.get_type())289 def test_builtin_zip_function_with_more_than_two_args(self):290 self.mod.write(291 'class C1(object):\n pass\nclass C2(object):\n pass\n'292 'c1_list = [C1()]\nc2_list = [C2()]\n'293 'a, b, c = zip(c1_list, c2_list, c1_list)[0]')294 pymod = self.project.get_pymodule(self.mod)295 c1_class = pymod['C1'].get_object()296 c2_class = pymod['C2'].get_object()297 a_var = pymod['a'].get_object()298 b_var = pymod['b'].get_object()299 c_var = pymod['c'].get_object()300 self.assertEqual(c1_class, a_var.get_type())301 self.assertEqual(c2_class, b_var.get_type())302 self.assertEqual(c1_class, c_var.get_type())303 def test_wrong_arguments_to_zip_function(self):304 self.mod.write(305 'class C1(object):\n pass\nc1_list = [C1()]\n'306 'a, b = zip(c1_list, 1)[0]')307 pymod = self.project.get_pymodule(self.mod)308 c1_class = pymod['C1'].get_object()309 a_var = pymod['a'].get_object()310 b_var = pymod['b'].get_object() # noqa311 self.assertEqual(c1_class, a_var.get_type())312 def test_enumerate_builtin_function(self):313 self.mod.write('class C(object):\n pass\nl = [C()]\n'314 'for i, x in enumerate(l):\n a_var = x\n')315 pymod = self.project.get_pymodule(self.mod)316 c_class = pymod['C'].get_object()317 a_var = pymod['a_var'].get_object()318 self.assertEqual(c_class, a_var.get_type())319 def test_builtin_class_get_name(self):320 self.assertEqual('object',321 builtins.builtins['object'].get_object().get_name())322 self.assertEqual(323 'property', builtins.builtins['property'].get_object().get_name())324 def test_star_args_and_double_star_args(self):325 self.mod.write('def func(p, *args, **kwds):\n pass\n')326 pymod = self.project.get_pymodule(self.mod)327 func_scope = pymod['func'].get_object().get_scope()328 args = func_scope['args'].get_object()329 kwds = func_scope['kwds'].get_object()330 self.assertTrue(isinstance(args.get_type(), builtins.List))331 self.assertTrue(isinstance(kwds.get_type(), builtins.Dict))332 def test_simple_list_comprehension_test(self):333 self.mod.write('a_var = [i for i in range(10)]\n')334 pymod = self.project.get_pymodule(self.mod)335 a_var = pymod['a_var'].get_object()336 self.assertTrue(isinstance(a_var.get_type(), builtins.List))337 def test_simple_list_generator_expression(self):338 self.mod.write('a_var = (i for i in range(10))\n')339 pymod = self.project.get_pymodule(self.mod)340 a_var = pymod['a_var'].get_object()341 self.assertTrue(isinstance(a_var.get_type(), builtins.Iterator))342 def test_iter_builtin_function(self):343 self.mod.write('class C(object):\n pass\nl = [C()]\n'344 'for c in iter(l):\n a_var = c\n')345 pymod = self.project.get_pymodule(self.mod)346 c_class = pymod['C'].get_object()347 a_var = pymod['a_var'].get_object()348 self.assertEqual(c_class, a_var.get_type())349 def test_simple_int_type(self):350 self.mod.write('l = 1\n')351 pymod = self.project.get_pymodule(self.mod)352 self.assertEqual(builtins.builtins['int'].get_object(),353 pymod['l'].get_object().get_type())354 def test_simple_float_type(self):355 self.mod.write('l = 1.0\n')356 pymod = self.project.get_pymodule(self.mod)357 self.assertEqual(builtins.builtins['float'].get_object(),358 pymod['l'].get_object().get_type())359 def test_simple_float_type2(self):360 self.mod.write('l = 1e1\n')361 pymod = self.project.get_pymodule(self.mod)362 self.assertEqual(builtins.builtins['float'].get_object(),363 pymod['l'].get_object().get_type())364 def test_simple_complex_type(self):365 self.mod.write('l = 1.0j\n')366 pymod = self.project.get_pymodule(self.mod)367 self.assertEqual(builtins.builtins['complex'].get_object(),368 pymod['l'].get_object().get_type())369 def test_handling_unaryop_on_ints(self):370 self.mod.write('l = -(1)\n')371 pymod = self.project.get_pymodule(self.mod)372 self.assertEqual(builtins.builtins['int'].get_object(),373 pymod['l'].get_object().get_type())374 def test_handling_binop_on_ints(self):375 self.mod.write('l = 1 + 1\n')376 pymod = self.project.get_pymodule(self.mod)377 self.assertEqual(builtins.builtins['int'].get_object(),378 pymod['l'].get_object().get_type())379 def test_handling_compares(self):380 self.mod.write('l = 1 == 1\n')381 pymod = self.project.get_pymodule(self.mod)382 self.assertEqual(builtins.builtins['bool'].get_object(),383 pymod['l'].get_object().get_type())384 def test_handling_boolops(self):385 self.mod.write('l = 1 and 2\n')386 pymod = self.project.get_pymodule(self.mod)387 self.assertEqual(builtins.builtins['int'].get_object(),388 pymod['l'].get_object().get_type())389 def test_binary_or_left_value_unknown(self):390 code = 'var = (asdsd or 3)\n'391 pymod = libutils.get_string_module(self.project, code)392 self.assertEqual(builtins.builtins['int'].get_object(),393 pymod['var'].get_object().get_type())394 def test_unknown_return_object(self):395 src = 'import sys\n' \396 'def foo():\n' \397 ' res = set(sys.builtin_module_names)\n' \398 ' if foo: res.add(bar)\n'399 self.project.prefs['import_dynload_stdmods'] = True400 self.mod.write(src)401 self.project.pycore.analyze_module(self.mod)402 def test_abstractmethods_attribute(self):403 # see http://bugs.python.org/issue10006 for details404 src = 'class SubType(type): pass\nsubtype = SubType()\n'405 self.mod.write(src)406 self.project.pycore.analyze_module(self.mod)407class BuiltinModulesTest(unittest.TestCase):408 def setUp(self):409 super(BuiltinModulesTest, self).setUp()410 self.project = testutils.sample_project(411 extension_modules=['time', 'invalid', 'invalid.sub'])412 self.mod = testutils.create_module(self.project, 'mod')413 def tearDown(self):414 testutils.remove_project(self.project)415 super(BuiltinModulesTest, self).tearDown()416 def test_simple_case(self):417 self.mod.write('import time')418 pymod = self.project.get_pymodule(self.mod)419 self.assertTrue('time' in pymod['time'].get_object())420 def test_ignored_extensions(self):421 self.mod.write('import os')422 pymod = self.project.get_pymodule(self.mod)423 self.assertTrue('rename' not in pymod['os'].get_object())424 def test_ignored_extensions_2(self):425 self.mod.write('import os')426 pymod = self.project.get_pymodule(self.mod)427 self.assertTrue('rename' not in pymod['os'].get_object())428 def test_nonexistent_modules(self):429 self.mod.write('import invalid')430 pymod = self.project.get_pymodule(self.mod)431 pymod['invalid'].get_object()432 def test_nonexistent_modules_2(self):433 self.mod.write('import invalid\nimport invalid.sub')434 pymod = self.project.get_pymodule(self.mod)435 invalid = pymod['invalid'].get_object()436 self.assertTrue('sub' in invalid)437 def test_time_in_std_mods(self):438 import rope.base.stdmods439 self.assertTrue('time' in rope.base.stdmods.standard_modules())440 def test_timemodule_normalizes_to_time(self):441 import rope.base.stdmods442 self.assertEqual(443 rope.base.stdmods.normalize_so_name('timemodule.so'), 'time')444def suite():445 result = unittest.TestSuite()446 result.addTests(unittest.makeSuite(BuiltinTypesTest))447 result.addTests(unittest.makeSuite(BuiltinModulesTest))448 return result449if __name__ == '__main__':...
objectinfertest.py
Source:objectinfertest.py
...15 super(ObjectInferTest, self).tearDown()16 def test_simple_type_inferencing(self):17 code = 'class Sample(object):\n pass\na_var = Sample()\n'18 scope = libutils.get_string_scope(self.project, code)19 sample_class = scope['Sample'].get_object()20 a_var = scope['a_var'].get_object()21 self.assertEqual(sample_class, a_var.get_type())22 def test_simple_type_inferencing_classes_defined_in_holding_scope(self):23 code = 'class Sample(object):\n pass\n' \24 'def a_func():\n a_var = Sample()\n'25 scope = libutils.get_string_scope(self.project, code)26 sample_class = scope['Sample'].get_object()27 a_var = scope['a_func'].get_object().\28 get_scope()['a_var'].get_object()29 self.assertEqual(sample_class, a_var.get_type())30 def test_simple_type_inferencing_classes_in_class_methods(self):31 code = 'class Sample(object):\n pass\n' \32 'class Another(object):\n' \33 ' def a_method():\n a_var = Sample()\n'34 scope = libutils.get_string_scope(self.project, code)35 sample_class = scope['Sample'].get_object()36 another_class = scope['Another'].get_object()37 a_var = another_class['a_method'].\38 get_object().get_scope()['a_var'].get_object()39 self.assertEqual(sample_class, a_var.get_type())40 def test_simple_type_inferencing_class_attributes(self):41 code = 'class Sample(object):\n pass\n' \42 'class Another(object):\n' \43 ' def __init__(self):\n self.a_var = Sample()\n'44 scope = libutils.get_string_scope(self.project, code)45 sample_class = scope['Sample'].get_object()46 another_class = scope['Another'].get_object()47 a_var = another_class['a_var'].get_object()48 self.assertEqual(sample_class, a_var.get_type())49 def test_simple_type_inferencing_for_in_class_assignments(self):50 code = 'class Sample(object):\n pass\n' \51 'class Another(object):\n an_attr = Sample()\n'52 scope = libutils.get_string_scope(self.project, code)53 sample_class = scope['Sample'].get_object()54 another_class = scope['Another'].get_object()55 an_attr = another_class['an_attr'].get_object()56 self.assertEqual(sample_class, an_attr.get_type())57 def test_simple_type_inferencing_for_chained_assignments(self):58 mod = 'class Sample(object):\n pass\n' \59 'copied_sample = Sample'60 mod_scope = libutils.get_string_scope(self.project, mod)61 sample_class = mod_scope['Sample']62 copied_sample = mod_scope['copied_sample']63 self.assertEqual(sample_class.get_object(),64 copied_sample.get_object())65 def test_following_chained_assignments_avoiding_circles(self):66 mod = 'class Sample(object):\n pass\n' \67 'sample_class = Sample\n' \68 'sample_class = sample_class\n'69 mod_scope = libutils.get_string_scope(self.project, mod)70 sample_class = mod_scope['Sample']71 sample_class_var = mod_scope['sample_class']72 self.assertEqual(sample_class.get_object(),73 sample_class_var.get_object())74 def test_function_returned_object_static_type_inference1(self):75 src = 'class Sample(object):\n pass\n' \76 'def a_func():\n return Sample\n' \77 'a_var = a_func()\n'78 scope = libutils.get_string_scope(self.project, src)79 sample_class = scope['Sample']80 a_var = scope['a_var']81 self.assertEqual(sample_class.get_object(), a_var.get_object())82 def test_function_returned_object_static_type_inference2(self):83 src = 'class Sample(object):\n pass\n' \84 'def a_func():\n return Sample()\n' \85 'a_var = a_func()\n'86 scope = libutils.get_string_scope(self.project, src)87 sample_class = scope['Sample'].get_object()88 a_var = scope['a_var'].get_object()89 self.assertEqual(sample_class, a_var.get_type())90 def test_recursive_function_returned_object_static_type_inference(self):91 src = 'class Sample(object):\n pass\n' \92 'def a_func():\n' \93 ' if True:\n return Sample()\n' \94 ' else:\n return a_func()\n' \95 'a_var = a_func()\n'96 scope = libutils.get_string_scope(self.project, src)97 sample_class = scope['Sample'].get_object()98 a_var = scope['a_var'].get_object()99 self.assertEqual(sample_class, a_var.get_type())100 def test_func_returned_obj_using_call_spec_func_static_type_infer(self):101 src = 'class Sample(object):\n' \102 ' def __call__(self):\n return Sample\n' \103 'sample = Sample()\na_var = sample()'104 scope = libutils.get_string_scope(self.project, src)105 sample_class = scope['Sample']106 a_var = scope['a_var']107 self.assertEqual(sample_class.get_object(), a_var.get_object())108 def test_list_type_inferencing(self):109 src = 'class Sample(object):\n pass\na_var = [Sample()]\n'110 scope = libutils.get_string_scope(self.project, src)111 sample_class = scope['Sample'].get_object()112 a_var = scope['a_var'].get_object()113 self.assertNotEqual(sample_class, a_var.get_type())114 def test_attributed_object_inference(self):115 src = 'class Sample(object):\n' \116 ' def __init__(self):\n self.a_var = None\n' \117 ' def set(self):\n self.a_var = Sample()\n'118 scope = libutils.get_string_scope(self.project, src)119 sample_class = scope['Sample'].get_object()120 a_var = sample_class['a_var'].get_object()121 self.assertEqual(sample_class, a_var.get_type())122 def test_getting_property_attributes(self):123 src = 'class A(object):\n pass\n' \124 'def f(*args):\n return A()\n' \125 'class B(object):\n p = property(f)\n' \126 'a_var = B().p\n'127 pymod = libutils.get_string_module(self.project, src)128 a_class = pymod['A'].get_object()129 a_var = pymod['a_var'].get_object()130 self.assertEqual(a_class, a_var.get_type())131 def test_getting_property_attributes_with_method_getters(self):132 src = 'class A(object):\n pass\n' \133 'class B(object):\n def p_get(self):\n return A()\n' \134 ' p = property(p_get)\n' \135 'a_var = B().p\n'136 pymod = libutils.get_string_module(self.project, src)137 a_class = pymod['A'].get_object()138 a_var = pymod['a_var'].get_object()139 self.assertEqual(a_class, a_var.get_type())140 def test_lambda_functions(self):141 code = 'class C(object):\n pass\n' \142 'l = lambda: C()\na_var = l()'143 mod = libutils.get_string_module(self.project, code)144 c_class = mod['C'].get_object()145 a_var = mod['a_var'].get_object()146 self.assertEqual(c_class, a_var.get_type())147 def test_mixing_subscript_with_tuple_assigns(self):148 code = 'class C(object):\n attr = 0\n' \149 'd = {}\nd[0], b = (0, C())\n'150 mod = libutils.get_string_module(self.project, code)151 c_class = mod['C'].get_object()152 a_var = mod['b'].get_object()153 self.assertEqual(c_class, a_var.get_type())154 def test_mixing_ass_attr_with_tuple_assignment(self):155 code = 'class C(object):\n attr = 0\n' \156 'c = C()\nc.attr, b = (0, C())\n'157 mod = libutils.get_string_module(self.project, code)158 c_class = mod['C'].get_object()159 a_var = mod['b'].get_object()160 self.assertEqual(c_class, a_var.get_type())161 def test_mixing_slice_with_tuple_assigns(self):162 mod = libutils.get_string_module(163 self.project,164 'class C(object):\n attr = 0\n'165 'd = [None] * 3\nd[0:2], b = ((0,), C())\n')166 c_class = mod['C'].get_object()167 a_var = mod['b'].get_object()168 self.assertEqual(c_class, a_var.get_type())169 def test_nested_tuple_assignments(self):170 mod = libutils.get_string_module(171 self.project,172 'class C1(object):\n pass\nclass C2(object):\n pass\n'173 'a, (b, c) = (C1(), (C2(), C1()))\n')174 c1_class = mod['C1'].get_object()175 c2_class = mod['C2'].get_object()176 a_var = mod['a'].get_object()177 b_var = mod['b'].get_object()178 c_var = mod['c'].get_object()179 self.assertEqual(c1_class, a_var.get_type())180 self.assertEqual(c2_class, b_var.get_type())181 self.assertEqual(c1_class, c_var.get_type())182 def test_empty_tuples(self):183 mod = libutils.get_string_module(184 self.project, 't = ()\na, b = t\n')185 a = mod['a'].get_object() # noqa186 def test_handling_generator_functions(self):187 code = 'class C(object):\n pass\n' \188 'def f():\n yield C()\n' \189 'for c in f():\n a_var = c\n'190 mod = libutils.get_string_module(self.project, code)191 c_class = mod['C'].get_object()192 a_var = mod['a_var'].get_object()193 self.assertEqual(c_class, a_var.get_type())194 def test_handling_generator_functions_for_strs(self):195 mod = testutils.create_module(self.project, 'mod')196 mod.write('def f():\n yield ""\n'197 'for s in f():\n a_var = s\n')198 pymod = self.project.get_pymodule(mod)199 a_var = pymod['a_var'].get_object()200 self.assertTrue(isinstance(a_var.get_type(), rope.base.builtins.Str))201 def test_considering_nones_to_be_unknowns(self):202 code = 'class C(object):\n pass\n' \203 'a_var = None\na_var = C()\na_var = None\n'204 mod = libutils.get_string_module(self.project, code)205 c_class = mod['C'].get_object()206 a_var = mod['a_var'].get_object()207 self.assertEqual(c_class, a_var.get_type())208 def test_basic_list_comprehensions(self):209 code = 'class C(object):\n pass\n' \210 'l = [C() for i in range(1)]\na_var = l[0]\n'211 mod = libutils.get_string_module(self.project, code)212 c_class = mod['C'].get_object()213 a_var = mod['a_var'].get_object()214 self.assertEqual(c_class, a_var.get_type())215 def test_basic_generator_expressions(self):216 code = 'class C(object):\n pass\n' \217 'l = (C() for i in range(1))\na_var = list(l)[0]\n'218 mod = libutils.get_string_module(self.project, code)219 c_class = mod['C'].get_object()220 a_var = mod['a_var'].get_object()221 self.assertEqual(c_class, a_var.get_type())222 def test_list_comprehensions_and_loop_var(self):223 code = 'class C(object):\n pass\n' \224 'c_objects = [C(), C()]\n' \225 'l = [c for c in c_objects]\na_var = l[0]\n'226 mod = libutils.get_string_module(self.project, code)227 c_class = mod['C'].get_object()228 a_var = mod['a_var'].get_object()229 self.assertEqual(c_class, a_var.get_type())230 def test_list_comprehensions_and_multiple_loop_var(self):231 code = 'class C1(object):\n pass\n' \232 'class C2(object):\n pass\n' \233 'l = [(c1, c2) for c1 in [C1()] for c2 in [C2()]]\n' \234 'a, b = l[0]\n'235 mod = libutils.get_string_module(self.project, code)236 c1_class = mod['C1'].get_object()237 c2_class = mod['C2'].get_object()238 a_var = mod['a'].get_object()239 b_var = mod['b'].get_object()240 self.assertEqual(c1_class, a_var.get_type())241 self.assertEqual(c2_class, b_var.get_type())242 def test_list_comprehensions_and_multiple_iters(self):243 mod = libutils.get_string_module(244 self.project,245 'class C1(object):\n pass\nclass C2(object):\n pass\n'246 'l = [(c1, c2) for c1, c2 in [(C1(), C2())]]\n'247 'a, b = l[0]\n')248 c1_class = mod['C1'].get_object()249 c2_class = mod['C2'].get_object()250 a_var = mod['a'].get_object()251 b_var = mod['b'].get_object()252 self.assertEqual(c1_class, a_var.get_type())253 self.assertEqual(c2_class, b_var.get_type())254 def test_we_know_the_type_of_catched_exceptions(self):255 code = 'class MyError(Exception):\n pass\n' \256 'try:\n raise MyError()\n' \257 'except MyError as e:\n pass\n'258 mod = libutils.get_string_module(self.project, code)259 my_error = mod['MyError'].get_object()260 e_var = mod['e'].get_object()261 self.assertEqual(my_error, e_var.get_type())262 def test_we_know_the_type_of_catched_multiple_excepts(self):263 code = 'class MyError(Exception):\n pass\n' \264 'try:\n raise MyError()\n' \265 'except (MyError, Exception) as e:\n pass\n'266 mod = libutils.get_string_module(self.project, code)267 my_error = mod['MyError'].get_object()268 e_var = mod['e'].get_object()269 self.assertEqual(my_error, e_var.get_type())270 def test_using_property_as_decorators(self):271 code = 'class A(object):\n pass\n' \272 'class B(object):\n' \273 ' @property\n def f(self):\n return A()\n' \274 'b = B()\nvar = b.f\n'275 mod = libutils.get_string_module(self.project, code)276 var = mod['var'].get_object()277 a = mod['A'].get_object()278 self.assertEqual(a, var.get_type())279 def test_using_property_as_decorators_and_passing_parameter(self):280 code = 'class B(object):\n' \281 ' @property\n def f(self):\n return self\n' \282 'b = B()\nvar = b.f\n'283 mod = libutils.get_string_module(self.project, code)284 var = mod['var'].get_object()285 a = mod['B'].get_object()286 self.assertEqual(a, var.get_type())287def suite():288 result = unittest.TestSuite()289 result.addTests(unittest.makeSuite(ObjectInferTest))290 return result291if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!