Source

rope_py3k / ropetest / objectinfertest.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import unittest

from rope.base import project, builtins
from rope.base.oi import memorydb, shelvedb
from ropetest import testutils


class ObjectInferTest(unittest.TestCase):

    def setUp(self):
        super(ObjectInferTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.get_pycore()

    def tearDown(self):
        testutils.remove_project(self.project)
        super(ObjectInferTest, self).tearDown()

    def test_simple_type_inferencing(self):
        scope = self.pycore.get_string_scope(
            'class Sample(object):\n    pass\na_var = Sample()\n')
        sample_class = scope.get_name('Sample').get_object()
        a_var = scope.get_name('a_var').get_object()
        self.assertEquals(sample_class, a_var.get_type())

    def test_simple_type_inferencing_classes_defined_in_holding_scope(self):
        scope = self.pycore.get_string_scope('class Sample(object):\n    pass\n' +
                                             'def a_func():\n    a_var = Sample()\n')
        sample_class = scope.get_name('Sample').get_object()
        a_var = scope.get_name('a_func').get_object().\
                get_scope().get_name('a_var').get_object()
        self.assertEquals(sample_class, a_var.get_type())

    def test_simple_type_inferencing_classes_in_class_methods(self):
        code = 'class Sample(object):\n    pass\n' \
               'class Another(object):\n' \
               '    def a_method():\n        a_var = Sample()\n'
        scope = self.pycore.get_string_scope(code)
        sample_class = scope.get_name('Sample').get_object()
        another_class = scope.get_name('Another').get_object()
        a_var = another_class.get_attribute('a_method').\
                get_object().get_scope().get_name('a_var').get_object()
        self.assertEquals(sample_class, a_var.get_type())

    def test_simple_type_inferencing_class_attributes(self):
        code = 'class Sample(object):\n    pass\n' \
               'class Another(object):\n' \
               '    def __init__(self):\n        self.a_var = Sample()\n'
        scope = self.pycore.get_string_scope(code)
        sample_class = scope.get_name('Sample').get_object()
        another_class = scope.get_name('Another').get_object()
        a_var = another_class.get_attribute('a_var').get_object()
        self.assertEquals(sample_class, a_var.get_type())

    def test_simple_type_inferencing_for_in_class_assignments(self):
        scope = self.pycore.get_string_scope('class Sample(object):\n    pass\n' +
                                             'class Another(object):\n    an_attr = Sample()\n')
        sample_class = scope.get_name('Sample').get_object()
        another_class = scope.get_name('Another').get_object()
        an_attr = another_class.get_attribute('an_attr').get_object()
        self.assertEquals(sample_class, an_attr.get_type())

    def test_simple_type_inferencing_for_chained_assignments(self):
        mod = 'class Sample(object):\n    pass\n' \
              'copied_sample = Sample'
        mod_scope = self.project.get_pycore().get_string_scope(mod)
        sample_class = mod_scope.get_name('Sample')
        copied_sample = mod_scope.get_name('copied_sample')
        self.assertEquals(sample_class.get_object(),
                          copied_sample.get_object())

    def test_following_chained_assignments_avoiding_circles(self):
        mod = 'class Sample(object):\n    pass\n' \
              'sample_class = Sample\n' \
              'sample_class = sample_class\n'
        mod_scope = self.project.get_pycore().get_string_scope(mod)
        sample_class = mod_scope.get_name('Sample')
        sample_class_var = mod_scope.get_name('sample_class')
        self.assertEquals(sample_class.get_object(),
                          sample_class_var.get_object())

    def test_function_returned_object_static_type_inference1(self):
        src = 'class Sample(object):\n    pass\n' \
              'def a_func():\n    return Sample\n' \
              'a_var = a_func()\n'
        scope = self.project.get_pycore().get_string_scope(src)
        sample_class = scope.get_name('Sample')
        a_var = scope.get_name('a_var')
        self.assertEquals(sample_class.get_object(), a_var.get_object())

    def test_function_returned_object_static_type_inference2(self):
        src = 'class Sample(object):\n    pass\n' \
              'def a_func():\n    return Sample()\n' \
              'a_var = a_func()\n'
        scope = self.project.get_pycore().get_string_scope(src)
        sample_class = scope.get_name('Sample').get_object()
        a_var = scope.get_name('a_var').get_object()
        self.assertEquals(sample_class, a_var.get_type())

    def test_recursive_function_returned_object_static_type_inference(self):
        src = 'class Sample(object):\n    pass\n' \
              'def a_func():\n' \
              '    if True:\n        return Sample()\n' \
              '    else:\n        return a_func()\n' \
              'a_var = a_func()\n'
        scope = self.project.get_pycore().get_string_scope(src)
        sample_class = scope.get_name('Sample').get_object()
        a_var = scope.get_name('a_var').get_object()
        self.assertEquals(sample_class, a_var.get_type())

    def test_function_returned_object_using_call_special_function_static_type_inference(self):
        src = 'class Sample(object):\n' \
              '    def __call__(self):\n        return Sample\n' \
              'sample = Sample()\na_var = sample()'
        scope = self.project.get_pycore().get_string_scope(src)
        sample_class = scope.get_name('Sample')
        a_var = scope.get_name('a_var')
        self.assertEquals(sample_class.get_object(), a_var.get_object())

    def test_list_type_inferencing(self):
        src = 'class Sample(object):\n    pass\na_var = [Sample()]\n'
        scope = self.pycore.get_string_scope(src)
        sample_class = scope.get_name('Sample').get_object()
        a_var = scope.get_name('a_var').get_object()
        self.assertNotEquals(sample_class, a_var.get_type())

    def test_attributed_object_inference(self):
        src = 'class Sample(object):\n' \
              '    def __init__(self):\n        self.a_var = None\n' \
              '    def set(self):\n        self.a_var = Sample()\n'
        scope = self.pycore.get_string_scope(src)
        sample_class = scope.get_name('Sample').get_object()
        a_var = sample_class.get_attribute('a_var').get_object()
        self.assertEquals(sample_class, a_var.get_type())

    def test_getting_property_attributes(self):
        src = 'class A(object):\n    pass\n' \
              'def f(*args):\n    return A()\n' \
              'class B(object):\n    p = property(f)\n' \
              'a_var = B().p\n'
        pymod = self.pycore.get_string_module(src)
        a_class = pymod.get_attribute('A').get_object()
        a_var = pymod.get_attribute('a_var').get_object()
        self.assertEquals(a_class, a_var.get_type())

    def test_getting_property_attributes_with_method_getters(self):
        src = 'class A(object):\n    pass\n' \
              'class B(object):\n    def p_get(self):\n        return A()\n' \
              '    p = property(p_get)\n' \
              'a_var = B().p\n'
        pymod = self.pycore.get_string_module(src)
        a_class = pymod.get_attribute('A').get_object()
        a_var = pymod.get_attribute('a_var').get_object()
        self.assertEquals(a_class, a_var.get_type())

    def test_lambda_functions(self):
        mod = self.pycore.get_string_module(
            'class C(object):\n    pass\n'
            'l = lambda: C()\na_var = l()')
        c_class = mod.get_attribute('C').get_object()
        a_var = mod.get_attribute('a_var').get_object()
        self.assertEquals(c_class, a_var.get_type())

    def test_mixing_subscript_with_tuple_assigns(self):
        mod = self.pycore.get_string_module(
            'class C(object):\n    attr = 0\n'
            'd = {}\nd[0], b = (0, C())\n')
        c_class = mod.get_attribute('C').get_object()
        a_var = mod.get_attribute('b').get_object()
        self.assertEquals(c_class, a_var.get_type())

    def test_mixing_ass_attr_with_tuple_assignment(self):
        mod = self.pycore.get_string_module(
            'class C(object):\n    attr = 0\n'
            'c = C()\nc.attr, b = (0, C())\n')
        c_class = mod.get_attribute('C').get_object()
        a_var = mod.get_attribute('b').get_object()
        self.assertEquals(c_class, a_var.get_type())

    def test_mixing_slice_with_tuple_assigns(self):
        mod = self.pycore.get_string_module(
            'class C(object):\n    attr = 0\n'
            'd = [None] * 3\nd[0:2], b = ((0,), C())\n')
        c_class = mod.get_attribute('C').get_object()
        a_var = mod.get_attribute('b').get_object()
        self.assertEquals(c_class, a_var.get_type())

    def test_nested_tuple_assignments(self):
        mod = self.pycore.get_string_module(
            'class C1(object):\n    pass\nclass C2(object):\n    pass\n'
            'a, (b, c) = (C1(), (C2(), C1()))\n')
        c1_class = mod.get_attribute('C1').get_object()
        c2_class = mod.get_attribute('C2').get_object()
        a_var = mod.get_attribute('a').get_object()
        b_var = mod.get_attribute('b').get_object()
        c_var = mod.get_attribute('c').get_object()
        self.assertEquals(c1_class, a_var.get_type())
        self.assertEquals(c2_class, b_var.get_type())
        self.assertEquals(c1_class, c_var.get_type())

    def test_handling_generator_functions(self):
        mod = self.pycore.get_string_module(
            'class C(object):\n    pass\ndef f():\n    yield C()\n'
            'for c in f():\n    a_var = c\n')
        c_class = mod.get_attribute('C').get_object()
        a_var = mod.get_attribute('a_var').get_object()
        self.assertEquals(c_class, a_var.get_type())

    def test_handling_generator_functions_for_strs(self):
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write('def f():\n    yield ""\n'
                  'for s in f():\n    a_var = s\n')
        pymod = self.pycore.resource_to_pyobject(mod)
        a_var = pymod.get_attribute('a_var').get_object()
        self.assertTrue(isinstance(a_var.get_type(), builtins.Str))

    def test_considering_nones_to_be_unknowns(self):
        mod = self.pycore.get_string_module(
            'class C(object):\n    pass\n'
            'a_var = None\na_var = C()\na_var = None\n')
        c_class = mod.get_attribute('C').get_object()
        a_var = mod.get_attribute('a_var').get_object()
        self.assertEquals(c_class, a_var.get_type())


def _do_for_all_dbs(function):
    def called(self):
        for db in self.dbs:
            function(self, db)
    return called


class _MockValidation(object):

    def is_value_valid(self, value):
        return value != -1

    def is_more_valid(self, new, old):
        return new != -1

    def is_file_valid(self, path):
        return path != 'invalid'

    def is_scope_valid(self, path, key):
        return path != 'invalid' and key != 'invalid'


class _MockFileListObserver(object):

    log = ''

    def added(self, path):
        self.log += 'added %s ' % path

    def removed(self, path):
        self.log += 'removed %s ' % path

class ObjectDBTest(unittest.TestCase):

    def setUp(self):
        super(ObjectDBTest, self).setUp()
        self.project = testutils.sample_project()
        validation = _MockValidation()
        self.dbs = [memorydb.MemoryObjectDB(validation),
                    shelvedb.ShelveObjectDB(self.project, validation)]

    def tearDown(self):
        for db in self.dbs:
            db.sync()
        testutils.remove_project(self.project)
        super(ObjectDBTest, self).tearDown()

    @_do_for_all_dbs
    def test_simple_per_name(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.save_per_name('name', 1)

        scope_info = db.get_scope_info('file', 'key')
        self.assertEqual(1, scope_info.get_per_name('name'))

    @_do_for_all_dbs
    def test_simple_per_name_does_not_exist(self, db):
        scope_info = db.get_scope_info('file', 'key')
        self.assertEquals(None, scope_info.get_per_name('name'))

    @_do_for_all_dbs
    def test_simple_per_name_after_syncing(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.save_per_name('name', 1)
        db.sync()

        scope_info = db.get_scope_info('file', 'key')
        self.assertEquals(1, scope_info.get_per_name('name'))

    @_do_for_all_dbs
    def test_getting_returned(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)

        scope_info = db.get_scope_info('file', 'key')
        self.assertEquals(3, scope_info.get_returned((1, 2)))

    @_do_for_all_dbs
    def test_getting_returned_when_does_not_match(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)

        scope_info = db.get_scope_info('file', 'key')
        self.assertEquals(None, scope_info.get_returned((1, 1)))

    @_do_for_all_dbs
    def test_getting_call_info(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)

        scope_info = db.get_scope_info('file', 'key')
        call_infos = list(scope_info.get_call_infos())
        self.assertEquals(1, len(call_infos))
        self.assertEquals((1, 2), call_infos[0].get_parameters())
        self.assertEquals(3, call_infos[0].get_returned())

    @_do_for_all_dbs
    def test_invalid_per_name(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.save_per_name('name', -1)

        self.assertEquals(None, scope_info.get_per_name('name'))

    @_do_for_all_dbs
    def test_overwriting_per_name(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.save_per_name('name', 1)
        scope_info.save_per_name('name', 2)
        self.assertEquals(2, scope_info.get_per_name('name'))

    @_do_for_all_dbs
    def test_not_overwriting_with_invalid_per_name(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.save_per_name('name', 1)
        scope_info.save_per_name('name', -1)
        self.assertEquals(1, scope_info.get_per_name('name'))

    @_do_for_all_dbs
    def test_getting_invalid_returned(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.add_call((1, 2), -1)

        self.assertEquals(None, scope_info.get_returned((1, 2)))

    @_do_for_all_dbs
    def test_not_overwriting_with_invalid_returned(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)
        scope_info.add_call((1, 2), -1)

        self.assertEquals(3, scope_info.get_returned((1, 2)))

    @_do_for_all_dbs
    def test_get_files(self, db):
        scope_info = db.get_scope_info('file1', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)
        scope_info = db.get_scope_info('file2', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)

        self.assertEquals(set(['file1', 'file2']), set(db.get_files()))

    @_do_for_all_dbs
    def test_validating_files(self, db):
        scope_info = db.get_scope_info('invalid', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)
        db.validate_files()
        self.assertEquals(0, len(db.get_files()))

    @_do_for_all_dbs
    def test_validating_file_for_scopes(self, db):
        scope_info = db.get_scope_info('file', 'invalid', readonly=False)
        scope_info.add_call((1, 2), 3)
        db.validate_file('file')
        self.assertEquals(1, len(db.get_files()))
        scope_info = db.get_scope_info('file', 'invalid', readonly=True)
        self.assertEquals(0, len(list(scope_info.get_call_infos())))

    @_do_for_all_dbs
    def test_validating_file_moved(self, db):
        scope_info = db.get_scope_info('file', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)

        db.file_moved('file', 'newfile')
        self.assertEquals(1, len(db.get_files()))
        scope_info = db.get_scope_info('newfile', 'key', readonly=True)
        self.assertEquals(1, len(list(scope_info.get_call_infos())))

    @_do_for_all_dbs
    def test_using_file_list_observer(self, db):
        scope_info = db.get_scope_info('invalid', 'key', readonly=False)
        scope_info.add_call((1, 2), 3)
        observer = _MockFileListObserver()
        db.add_file_list_observer(observer)
        db.validate_files()
        self.assertEquals('removed invalid ', observer.log)


def suite():
    result = unittest.TestSuite()
    result.addTests(unittest.makeSuite(ObjectInferTest))
    result.addTests(unittest.makeSuite(ObjectDBTest))
    return result


if __name__ == '__main__':
    unittest.main()