OLD | NEW |
(Empty) | |
| 1 import unittest |
| 2 |
| 3 import rope.base.project |
| 4 import rope.base.builtins |
| 5 from rope.base import libutils |
| 6 from ropetest import testutils |
| 7 |
| 8 |
| 9 class ObjectInferTest(unittest.TestCase): |
| 10 |
| 11 def setUp(self): |
| 12 super(ObjectInferTest, self).setUp() |
| 13 self.project = testutils.sample_project() |
| 14 |
| 15 def tearDown(self): |
| 16 testutils.remove_project(self.project) |
| 17 super(ObjectInferTest, self).tearDown() |
| 18 |
| 19 def test_simple_type_inferencing(self): |
| 20 code = 'class Sample(object):\n pass\na_var = Sample()\n' |
| 21 scope = libutils.get_string_scope(self.project, code) |
| 22 sample_class = scope['Sample'].get_object() |
| 23 a_var = scope['a_var'].get_object() |
| 24 self.assertEquals(sample_class, a_var.get_type()) |
| 25 |
| 26 def test_simple_type_inferencing_classes_defined_in_holding_scope(self): |
| 27 code = 'class Sample(object):\n pass\n' \ |
| 28 'def a_func():\n a_var = Sample()\n' |
| 29 scope = libutils.get_string_scope(self.project, code) |
| 30 sample_class = scope['Sample'].get_object() |
| 31 a_var = scope['a_func'].get_object().\ |
| 32 get_scope()['a_var'].get_object() |
| 33 self.assertEquals(sample_class, a_var.get_type()) |
| 34 |
| 35 def test_simple_type_inferencing_classes_in_class_methods(self): |
| 36 code = 'class Sample(object):\n pass\n' \ |
| 37 'class Another(object):\n' \ |
| 38 ' def a_method():\n a_var = Sample()\n' |
| 39 scope = libutils.get_string_scope(self.project, code) |
| 40 sample_class = scope['Sample'].get_object() |
| 41 another_class = scope['Another'].get_object() |
| 42 a_var = another_class['a_method'].\ |
| 43 get_object().get_scope()['a_var'].get_object() |
| 44 self.assertEquals(sample_class, a_var.get_type()) |
| 45 |
| 46 def test_simple_type_inferencing_class_attributes(self): |
| 47 code = 'class Sample(object):\n pass\n' \ |
| 48 'class Another(object):\n' \ |
| 49 ' def __init__(self):\n self.a_var = Sample()\n' |
| 50 scope = libutils.get_string_scope(self.project, code) |
| 51 sample_class = scope['Sample'].get_object() |
| 52 another_class = scope['Another'].get_object() |
| 53 a_var = another_class['a_var'].get_object() |
| 54 self.assertEquals(sample_class, a_var.get_type()) |
| 55 |
| 56 def test_simple_type_inferencing_for_in_class_assignments(self): |
| 57 code = 'class Sample(object):\n pass\n' \ |
| 58 'class Another(object):\n an_attr = Sample()\n' |
| 59 scope = libutils.get_string_scope(self.project, code) |
| 60 sample_class = scope['Sample'].get_object() |
| 61 another_class = scope['Another'].get_object() |
| 62 an_attr = another_class['an_attr'].get_object() |
| 63 self.assertEquals(sample_class, an_attr.get_type()) |
| 64 |
| 65 def test_simple_type_inferencing_for_chained_assignments(self): |
| 66 mod = 'class Sample(object):\n pass\n' \ |
| 67 'copied_sample = Sample' |
| 68 mod_scope = libutils.get_string_scope(self.project, mod) |
| 69 sample_class = mod_scope['Sample'] |
| 70 copied_sample = mod_scope['copied_sample'] |
| 71 self.assertEquals(sample_class.get_object(), |
| 72 copied_sample.get_object()) |
| 73 |
| 74 def test_following_chained_assignments_avoiding_circles(self): |
| 75 mod = 'class Sample(object):\n pass\n' \ |
| 76 'sample_class = Sample\n' \ |
| 77 'sample_class = sample_class\n' |
| 78 mod_scope = libutils.get_string_scope(self.project, mod) |
| 79 sample_class = mod_scope['Sample'] |
| 80 sample_class_var = mod_scope['sample_class'] |
| 81 self.assertEquals(sample_class.get_object(), |
| 82 sample_class_var.get_object()) |
| 83 |
| 84 def test_function_returned_object_static_type_inference1(self): |
| 85 src = 'class Sample(object):\n pass\n' \ |
| 86 'def a_func():\n return Sample\n' \ |
| 87 'a_var = a_func()\n' |
| 88 scope = libutils.get_string_scope(self.project, src) |
| 89 sample_class = scope['Sample'] |
| 90 a_var = scope['a_var'] |
| 91 self.assertEquals(sample_class.get_object(), a_var.get_object()) |
| 92 |
| 93 def test_function_returned_object_static_type_inference2(self): |
| 94 src = 'class Sample(object):\n pass\n' \ |
| 95 'def a_func():\n return Sample()\n' \ |
| 96 'a_var = a_func()\n' |
| 97 scope = libutils.get_string_scope(self.project, src) |
| 98 sample_class = scope['Sample'].get_object() |
| 99 a_var = scope['a_var'].get_object() |
| 100 self.assertEquals(sample_class, a_var.get_type()) |
| 101 |
| 102 def test_recursive_function_returned_object_static_type_inference(self): |
| 103 src = 'class Sample(object):\n pass\n' \ |
| 104 'def a_func():\n' \ |
| 105 ' if True:\n return Sample()\n' \ |
| 106 ' else:\n return a_func()\n' \ |
| 107 'a_var = a_func()\n' |
| 108 scope = libutils.get_string_scope(self.project, src) |
| 109 sample_class = scope['Sample'].get_object() |
| 110 a_var = scope['a_var'].get_object() |
| 111 self.assertEquals(sample_class, a_var.get_type()) |
| 112 |
| 113 def test_func_returned_obj_using_call_spec_func_static_type_infer(self): |
| 114 src = 'class Sample(object):\n' \ |
| 115 ' def __call__(self):\n return Sample\n' \ |
| 116 'sample = Sample()\na_var = sample()' |
| 117 scope = libutils.get_string_scope(self.project, src) |
| 118 sample_class = scope['Sample'] |
| 119 a_var = scope['a_var'] |
| 120 self.assertEquals(sample_class.get_object(), a_var.get_object()) |
| 121 |
| 122 def test_list_type_inferencing(self): |
| 123 src = 'class Sample(object):\n pass\na_var = [Sample()]\n' |
| 124 scope = libutils.get_string_scope(self.project, src) |
| 125 sample_class = scope['Sample'].get_object() |
| 126 a_var = scope['a_var'].get_object() |
| 127 self.assertNotEquals(sample_class, a_var.get_type()) |
| 128 |
| 129 def test_attributed_object_inference(self): |
| 130 src = 'class Sample(object):\n' \ |
| 131 ' def __init__(self):\n self.a_var = None\n' \ |
| 132 ' def set(self):\n self.a_var = Sample()\n' |
| 133 scope = libutils.get_string_scope(self.project, src) |
| 134 sample_class = scope['Sample'].get_object() |
| 135 a_var = sample_class['a_var'].get_object() |
| 136 self.assertEquals(sample_class, a_var.get_type()) |
| 137 |
| 138 def test_getting_property_attributes(self): |
| 139 src = 'class A(object):\n pass\n' \ |
| 140 'def f(*args):\n return A()\n' \ |
| 141 'class B(object):\n p = property(f)\n' \ |
| 142 'a_var = B().p\n' |
| 143 pymod = libutils.get_string_module(self.project, src) |
| 144 a_class = pymod['A'].get_object() |
| 145 a_var = pymod['a_var'].get_object() |
| 146 self.assertEquals(a_class, a_var.get_type()) |
| 147 |
| 148 def test_getting_property_attributes_with_method_getters(self): |
| 149 src = 'class A(object):\n pass\n' \ |
| 150 'class B(object):\n def p_get(self):\n return A()\n' \ |
| 151 ' p = property(p_get)\n' \ |
| 152 'a_var = B().p\n' |
| 153 pymod = libutils.get_string_module(self.project, src) |
| 154 a_class = pymod['A'].get_object() |
| 155 a_var = pymod['a_var'].get_object() |
| 156 self.assertEquals(a_class, a_var.get_type()) |
| 157 |
| 158 def test_lambda_functions(self): |
| 159 code = 'class C(object):\n pass\n' \ |
| 160 'l = lambda: C()\na_var = l()' |
| 161 mod = libutils.get_string_module(self.project, code) |
| 162 c_class = mod['C'].get_object() |
| 163 a_var = mod['a_var'].get_object() |
| 164 self.assertEquals(c_class, a_var.get_type()) |
| 165 |
| 166 def test_mixing_subscript_with_tuple_assigns(self): |
| 167 code = 'class C(object):\n attr = 0\n' \ |
| 168 'd = {}\nd[0], b = (0, C())\n' |
| 169 mod = libutils.get_string_module(self.project, code) |
| 170 c_class = mod['C'].get_object() |
| 171 a_var = mod['b'].get_object() |
| 172 self.assertEquals(c_class, a_var.get_type()) |
| 173 |
| 174 def test_mixing_ass_attr_with_tuple_assignment(self): |
| 175 code = 'class C(object):\n attr = 0\n' \ |
| 176 'c = C()\nc.attr, b = (0, C())\n' |
| 177 mod = libutils.get_string_module(self.project, code) |
| 178 c_class = mod['C'].get_object() |
| 179 a_var = mod['b'].get_object() |
| 180 self.assertEquals(c_class, a_var.get_type()) |
| 181 |
| 182 def test_mixing_slice_with_tuple_assigns(self): |
| 183 mod = libutils.get_string_module( |
| 184 self.project, |
| 185 'class C(object):\n attr = 0\n' |
| 186 'd = [None] * 3\nd[0:2], b = ((0,), C())\n') |
| 187 c_class = mod['C'].get_object() |
| 188 a_var = mod['b'].get_object() |
| 189 self.assertEquals(c_class, a_var.get_type()) |
| 190 |
| 191 def test_nested_tuple_assignments(self): |
| 192 mod = libutils.get_string_module( |
| 193 self.project, |
| 194 'class C1(object):\n pass\nclass C2(object):\n pass\n' |
| 195 'a, (b, c) = (C1(), (C2(), C1()))\n') |
| 196 c1_class = mod['C1'].get_object() |
| 197 c2_class = mod['C2'].get_object() |
| 198 a_var = mod['a'].get_object() |
| 199 b_var = mod['b'].get_object() |
| 200 c_var = mod['c'].get_object() |
| 201 self.assertEquals(c1_class, a_var.get_type()) |
| 202 self.assertEquals(c2_class, b_var.get_type()) |
| 203 self.assertEquals(c1_class, c_var.get_type()) |
| 204 |
| 205 def test_empty_tuples(self): |
| 206 mod = libutils.get_string_module( |
| 207 self.project, 't = ()\na, b = t\n') |
| 208 a = mod['a'].get_object() # noqa |
| 209 |
| 210 def test_handling_generator_functions(self): |
| 211 code = 'class C(object):\n pass\n' \ |
| 212 'def f():\n yield C()\n' \ |
| 213 'for c in f():\n a_var = c\n' |
| 214 mod = libutils.get_string_module(self.project, code) |
| 215 c_class = mod['C'].get_object() |
| 216 a_var = mod['a_var'].get_object() |
| 217 self.assertEquals(c_class, a_var.get_type()) |
| 218 |
| 219 def test_handling_generator_functions_for_strs(self): |
| 220 mod = testutils.create_module(self.project, 'mod') |
| 221 mod.write('def f():\n yield ""\n' |
| 222 'for s in f():\n a_var = s\n') |
| 223 pymod = self.project.get_pymodule(mod) |
| 224 a_var = pymod['a_var'].get_object() |
| 225 self.assertTrue(isinstance(a_var.get_type(), rope.base.builtins.Str)) |
| 226 |
| 227 def test_considering_nones_to_be_unknowns(self): |
| 228 code = 'class C(object):\n pass\n' \ |
| 229 'a_var = None\na_var = C()\na_var = None\n' |
| 230 mod = libutils.get_string_module(self.project, code) |
| 231 c_class = mod['C'].get_object() |
| 232 a_var = mod['a_var'].get_object() |
| 233 self.assertEquals(c_class, a_var.get_type()) |
| 234 |
| 235 def test_basic_list_comprehensions(self): |
| 236 code = 'class C(object):\n pass\n' \ |
| 237 'l = [C() for i in range(1)]\na_var = l[0]\n' |
| 238 mod = libutils.get_string_module(self.project, code) |
| 239 c_class = mod['C'].get_object() |
| 240 a_var = mod['a_var'].get_object() |
| 241 self.assertEquals(c_class, a_var.get_type()) |
| 242 |
| 243 def test_basic_generator_expressions(self): |
| 244 code = 'class C(object):\n pass\n' \ |
| 245 'l = (C() for i in range(1))\na_var = list(l)[0]\n' |
| 246 mod = libutils.get_string_module(self.project, code) |
| 247 c_class = mod['C'].get_object() |
| 248 a_var = mod['a_var'].get_object() |
| 249 self.assertEquals(c_class, a_var.get_type()) |
| 250 |
| 251 def test_list_comprehensions_and_loop_var(self): |
| 252 code = 'class C(object):\n pass\n' \ |
| 253 'c_objects = [C(), C()]\n' \ |
| 254 'l = [c for c in c_objects]\na_var = l[0]\n' |
| 255 mod = libutils.get_string_module(self.project, code) |
| 256 c_class = mod['C'].get_object() |
| 257 a_var = mod['a_var'].get_object() |
| 258 self.assertEquals(c_class, a_var.get_type()) |
| 259 |
| 260 def test_list_comprehensions_and_multiple_loop_var(self): |
| 261 code = 'class C1(object):\n pass\n' \ |
| 262 'class C2(object):\n pass\n' \ |
| 263 'l = [(c1, c2) for c1 in [C1()] for c2 in [C2()]]\n' \ |
| 264 'a, b = l[0]\n' |
| 265 mod = libutils.get_string_module(self.project, code) |
| 266 c1_class = mod['C1'].get_object() |
| 267 c2_class = mod['C2'].get_object() |
| 268 a_var = mod['a'].get_object() |
| 269 b_var = mod['b'].get_object() |
| 270 self.assertEquals(c1_class, a_var.get_type()) |
| 271 self.assertEquals(c2_class, b_var.get_type()) |
| 272 |
| 273 def test_list_comprehensions_and_multiple_iters(self): |
| 274 mod = libutils.get_string_module( |
| 275 self.project, |
| 276 'class C1(object):\n pass\nclass C2(object):\n pass\n' |
| 277 'l = [(c1, c2) for c1, c2 in [(C1(), C2())]]\n' |
| 278 'a, b = l[0]\n') |
| 279 c1_class = mod['C1'].get_object() |
| 280 c2_class = mod['C2'].get_object() |
| 281 a_var = mod['a'].get_object() |
| 282 b_var = mod['b'].get_object() |
| 283 self.assertEquals(c1_class, a_var.get_type()) |
| 284 self.assertEquals(c2_class, b_var.get_type()) |
| 285 |
| 286 def test_we_know_the_type_of_catched_exceptions(self): |
| 287 code = 'class MyError(Exception):\n pass\n' \ |
| 288 'try:\n raise MyError()\n' \ |
| 289 'except MyError, e:\n pass\n' |
| 290 mod = libutils.get_string_module(self.project, code) |
| 291 my_error = mod['MyError'].get_object() |
| 292 e_var = mod['e'].get_object() |
| 293 self.assertEquals(my_error, e_var.get_type()) |
| 294 |
| 295 def test_we_know_the_type_of_catched_multiple_excepts(self): |
| 296 code = 'class MyError(Exception):\n pass\n' \ |
| 297 'try:\n raise MyError()\n' \ |
| 298 'except (MyError, Exception), e:\n pass\n' |
| 299 mod = libutils.get_string_module(self.project, code) |
| 300 my_error = mod['MyError'].get_object() |
| 301 e_var = mod['e'].get_object() |
| 302 self.assertEquals(my_error, e_var.get_type()) |
| 303 |
| 304 def test_using_property_as_decorators(self): |
| 305 code = 'class A(object):\n pass\n' \ |
| 306 'class B(object):\n' \ |
| 307 ' @property\n def f(self):\n return A()\n' \ |
| 308 'b = B()\nvar = b.f\n' |
| 309 mod = libutils.get_string_module(self.project, code) |
| 310 var = mod['var'].get_object() |
| 311 a = mod['A'].get_object() |
| 312 self.assertEquals(a, var.get_type()) |
| 313 |
| 314 def test_using_property_as_decorators_and_passing_parameter(self): |
| 315 code = 'class B(object):\n' \ |
| 316 ' @property\n def f(self):\n return self\n' \ |
| 317 'b = B()\nvar = b.f\n' |
| 318 mod = libutils.get_string_module(self.project, code) |
| 319 var = mod['var'].get_object() |
| 320 a = mod['B'].get_object() |
| 321 self.assertEquals(a, var.get_type()) |
| 322 |
| 323 |
| 324 def suite(): |
| 325 result = unittest.TestSuite() |
| 326 result.addTests(unittest.makeSuite(ObjectInferTest)) |
| 327 return result |
| 328 |
| 329 |
| 330 if __name__ == '__main__': |
| 331 unittest.main() |
OLD | NEW |