Source

rope_py3k / ropetest / codeanalyzetest.py

Full commit
Ali Gholami Rudi 54dc337 

Ali Gholami Rudi 242ba74 
Ali Gholami Rudi 7cb7dac 
Ali Gholami Rudi 42cef02 
Ali Gholami Rudi d48ccd2 
Ali Gholami Rudi 242ba74 
Ali Gholami Rudi 54dc337 
Ali Gholami Rudi 3cda6a3 
Ali Gholami Rudi 54dc337 







Ali Gholami Rudi 5b8e016 



Ali Gholami Rudi 54dc337 
Ali Gholami Rudi 5b8e016 






Ali Gholami Rudi 13ad66a 
Ali Gholami Rudi 5b8e016 
Ali Gholami Rudi 13ad66a 
Ali Gholami Rudi 5b8e016 



Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 5b8e016 


Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 5b8e016 


Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 5b8e016 


Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 97101a9 









Ali Gholami Rudi 473365a 
Ali Gholami Rudi 97101a9 
Ali Gholami Rudi 473365a 
Ali Gholami Rudi 54dc337 
Ali Gholami Rudi d2da3ff 











Ali Gholami Rudi bb2e89d 



Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 5494649 









Ali Gholami Rudi 523f341 
Ali Gholami Rudi 5494649 
Ali Gholami Rudi 3d46bb8 

Ali Gholami Rudi 523f341 
Ali Gholami Rudi 3d46bb8 


Ali Gholami Rudi d872e20 
Ali Gholami Rudi b43a593 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 3d46bb8 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 108b904 



Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 108b904 



Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 108b904 



Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 108b904 



Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 7f89c0f 



Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 3d46bb8 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 3d46bb8 
Ali Gholami Rudi b43a593 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 


Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 


Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 



Ali Gholami Rudi 523f341 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 2f80abf 

Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 


Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 


Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi e7d9688 



Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 2f80abf 
Ali Gholami Rudi 32b2d76 
Ali Gholami Rudi 523f341 
Ali Gholami Rudi 3d46bb8 
Ali Gholami Rudi af773ee 




Ali Gholami Rudi 4ec5b1b 


Ali Gholami Rudi 523f341 
Ali Gholami Rudi 4ec5b1b 

Ali Gholami Rudi 33471e4 

Ali Gholami Rudi 523f341 
Ali Gholami Rudi 4ec5b1b 
Ali Gholami Rudi 77d5018 





Ali Gholami Rudi 2dd7bef 

Ali Gholami Rudi f3a7935 

Ali Gholami Rudi 523f341 
Ali Gholami Rudi 2dd7bef 



Ali Gholami Rudi 523f341 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 3cda6a3 




Ali Gholami Rudi 2dd7bef 
Ali Gholami Rudi ef841e6 




Ali Gholami Rudi b331f2e 





Ali Gholami Rudi 5494649 
Ali Gholami Rudi ef71fd8 



Ali Gholami Rudi 495804b 
Ali Gholami Rudi ef71fd8 


Ali Gholami Rudi 495804b 
Ali Gholami Rudi ef71fd8 

Ali Gholami Rudi 5575ac1 
Ali Gholami Rudi 242ba74 
Ali Gholami Rudi 5575ac1 
Ali Gholami Rudi ef71fd8 

Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 644af29 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi ef71fd8 


Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 644af29 
Ali Gholami Rudi ef71fd8 




Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 644af29 
Ali Gholami Rudi ef71fd8 




Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 644af29 
Ali Gholami Rudi ef71fd8 





Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 644af29 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi ef71fd8 

Ali Gholami Rudi 2309064 


Ali Gholami Rudi 644af29 
Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 2309064 




Ali Gholami Rudi 644af29 
Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 2309064 

Ali Gholami Rudi 8fea03c 
Ali Gholami Rudi 5416e09 
Ali Gholami Rudi 8fea03c 



Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 8fea03c 



Ali Gholami Rudi 614375d 

Ali Gholami Rudi 5416e09 
Ali Gholami Rudi 614375d 






Ali Gholami Rudi a903dc2 
Ali Gholami Rudi 614375d 



Ali Gholami Rudi b43a593 
Ali Gholami Rudi 5416e09 

Ali Gholami Rudi e009c98 




Ali Gholami Rudi a903dc2 
Ali Gholami Rudi e009c98 



Ali Gholami Rudi 242ba74 






Ali Gholami Rudi ef71fd8 
Ali Gholami Rudi d872e20 






Ali Gholami Rudi 41c4490 
Ali Gholami Rudi d872e20 

Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi d872e20 



Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi d872e20 




Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi d872e20 



Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi d872e20 



Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi d872e20 

Ali Gholami Rudi abae011 

Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi abae011 

Ali Gholami Rudi 242ba74 

Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi 242ba74 

Ali Gholami Rudi 7bddb54 

Ali Gholami Rudi f1e9f00 
Ali Gholami Rudi 7bddb54 

Ali Gholami Rudi 242ba74 

Ali Gholami Rudi f1e9f00 












Ali Gholami Rudi 242ba74 
Ali Gholami Rudi d872e20 
Ali Gholami Rudi 5494649 



Ali Gholami Rudi ef71fd8 
Ali Gholami Rudi d872e20 
Ali Gholami Rudi 5494649 
Ali Gholami Rudi 54dc337 

  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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import unittest

from rope.base import exceptions
from rope.base.codeanalyze import (StatementRangeFinder, ArrayLinesAdapter,
                                   SourceLinesAdapter, WordRangeFinder,
                                   ScopeNameFinder, LogicalLineFinder, get_block_start)
from ropetest import testutils


class StatementRangeFinderTest(unittest.TestCase):

    def setUp(self):
        super(StatementRangeFinderTest, self).setUp()

    def tearDown(self):
        super(StatementRangeFinderTest, self).tearDown()

    def get_range_finder(self, code, line):
        result = StatementRangeFinder(ArrayLinesAdapter(code.split('\n')), line)
        return result

    def test_simple_statement_finding(self):
        finder = self.get_range_finder('a = 10', 1)
        self.assertEquals(1,  finder.get_statement_start())

    def test_get_start(self):
        finder = self.get_range_finder('a = 10\nb = 12\nc = 14', 1)
        self.assertEquals(1,  finder.get_statement_start())

    def test_get_block_end(self):
        finder = self.get_range_finder('a = 10\nb = 12\nc = 14', 1)
        self.assertEquals(3,  finder.get_block_end())

    def test_get_last_open_parens(self):
        finder = self.get_range_finder('a = 10', 1)
        self.assertTrue(finder.last_open_parens() is None)

    def test_get_last_open_parens2(self):
        finder = self.get_range_finder('a = (10 +', 1)
        self.assertEquals((1, 4), finder.last_open_parens())

    def test_is_line_continued(self):
        finder = self.get_range_finder('a = 10', 1)
        self.assertFalse(finder.is_line_continued())

    def test_is_line_continued2(self):
        finder = self.get_range_finder('a = (10 +', 1)
        self.assertTrue(finder.is_line_continued())

    def test_source_lines_simple(self):
        to_lines = SourceLinesAdapter('line1\nline2\n')
        self.assertEquals('line1', to_lines.get_line(1))
        self.assertEquals('line2', to_lines.get_line(2))
        self.assertEquals('', to_lines.get_line(3))
        self.assertEquals(3, to_lines.length())

    def test_source_lines_get_line_number(self):
        to_lines = SourceLinesAdapter('line1\nline2\n')
        self.assertEquals(1, to_lines.get_line_number(0))
        self.assertEquals(1, to_lines.get_line_number(5))
        self.assertEquals(2, to_lines.get_line_number(7))
        self.assertEquals(3, to_lines.get_line_number(12))

    def test_source_lines_get_line_start(self):
        to_lines = SourceLinesAdapter('line1\nline2\n')
        self.assertEquals(0, to_lines.get_line_start(1))
        self.assertEquals(6, to_lines.get_line_start(2))
        self.assertEquals(12, to_lines.get_line_start(3))

    def test_source_lines_get_line_end(self):
        to_lines = SourceLinesAdapter('line1\nline2\n')
        self.assertEquals(5, to_lines.get_line_end(1))
        self.assertEquals(11, to_lines.get_line_end(2))
        self.assertEquals(12, to_lines.get_line_end(3))

    def test_source_lines_last_line_with_no_new_line(self):
        to_lines = SourceLinesAdapter('line1')
        self.assertEquals(1, to_lines.get_line_number(5))


class WordRangeFinderTest(unittest.TestCase):

    def setUp(self):
        super(WordRangeFinderTest, self).setUp()

    def tearDown(self):
        super(WordRangeFinderTest, self).tearDown()

    def test_inside_parans(self):
        word_finder = WordRangeFinder('a_func(a_var)')
        self.assertEquals('a_var', word_finder.get_primary_at(10))

    def test_simple_names(self):
        word_finder = WordRangeFinder('a_var = 10')
        self.assertEquals('a_var', word_finder.get_primary_at(3))

    def test_function_calls(self):
        word_finder = WordRangeFinder('sample_function()')
        self.assertEquals('sample_function', word_finder.get_primary_at(10))

    def test_attribute_accesses(self):
        word_finder = WordRangeFinder('a_var.an_attr')
        self.assertEquals('a_var.an_attr', word_finder.get_primary_at(10))

    def test_word_finder_on_word_beginning(self):
        code = 'print a_var\n'
        word_finder = WordRangeFinder(code)
        self.assertEquals('a_var', word_finder.get_word_at(code.index('a_var')))

    def test_word_finder_on_primary_beginning(self):
        code = 'print a_var\n'
        word_finder = WordRangeFinder(code)
        self.assertEquals('a_var', word_finder.get_primary_at(code.index('a_var')))

    def test_word_finder_on_word_ending(self):
        code = 'print a_var\n'
        word_finder = WordRangeFinder(code)
        self.assertEquals('a_var', word_finder.get_word_at(code.index('a_var') + 5))

    def test_word_finder_on_primary_ending(self):
        code = 'print a_var\n'
        word_finder = WordRangeFinder(code)
        self.assertEquals('a_var', word_finder.get_primary_at(code.index('a_var') + 5))

    def test_word_finder_on_primaries_with_dots_inside_parens(self):
        code = '(a_var.\nattr)'
        word_finder = WordRangeFinder(code)
        self.assertEquals('a_var.\nattr', word_finder.get_primary_at(code.index('attr') + 1))

    def test_strings(self):
        word_finder = WordRangeFinder('"a string".split()')
        self.assertEquals('"a string".split', word_finder.get_primary_at(14))

    def test_function_calls2(self):
        word_finder = WordRangeFinder('file("afile.txt").read()')
        self.assertEquals('file("afile.txt").read',
                          word_finder.get_primary_at(18))

    def test_parens(self):
        word_finder = WordRangeFinder('("afile.txt").split()')
        self.assertEquals('("afile.txt").split',
                          word_finder.get_primary_at(18))

    def test_function_with_no_param(self):
        word_finder = WordRangeFinder('AClass().a_func()')
        self.assertEquals('AClass().a_func', word_finder.get_primary_at(12))

    def test_function_with_multiple_param(self):
        word_finder = WordRangeFinder('AClass(a_param, another_param, "a string").a_func()')
        self.assertEquals('AClass(a_param, another_param, "a string").a_func',
                          word_finder.get_primary_at(44))

    def test_param_expressions(self):
        word_finder = WordRangeFinder('AClass(an_object.an_attr).a_func()')
        self.assertEquals('an_object.an_attr',
                          word_finder.get_primary_at(20))

    def test_string_parens(self):
        word_finder = WordRangeFinder('a_func("(").an_attr')
        self.assertEquals('a_func("(").an_attr',
                          word_finder.get_primary_at(16))

    def test_extra_spaces(self):
        word_finder = WordRangeFinder('a_func  (  "(" ) .   an_attr')
        self.assertEquals('a_func  (  "(" ) .   an_attr',
                          word_finder.get_primary_at(26))

    def test_functions_on_ending_parens(self):
        word_finder = WordRangeFinder('A()')
        self.assertEquals('A()', word_finder.get_primary_at(2))

    def test_splitted_statement(self):
        word_finder = WordRangeFinder('an_object.an_attr')
        self.assertEquals(('an_object', 'an_at', 10),
                          word_finder.get_splitted_primary_before(15))

    def test_empty_splitted_statement(self):
        word_finder = WordRangeFinder('an_attr')
        self.assertEquals(('', 'an_at', 0),
                          word_finder.get_splitted_primary_before(5))

    def test_empty_splitted_statement2(self):
        word_finder = WordRangeFinder('an_object.')
        self.assertEquals(('an_object', '', 10),
                          word_finder.get_splitted_primary_before(10))

    def test_empty_splitted_statement3(self):
        word_finder = WordRangeFinder('')
        self.assertEquals(('', '', 0),
                          word_finder.get_splitted_primary_before(0))

    def test_empty_splitted_statement4(self):
        word_finder = WordRangeFinder('a_var = ')
        self.assertEquals(('', '', 8),
                          word_finder.get_splitted_primary_before(8))

    def test_empty_splitted_statement5(self):
        word_finder = WordRangeFinder('a.')
        self.assertEquals(('a', '', 2),
                          word_finder.get_splitted_primary_before(2))

    def test_operators_inside_parens(self):
        word_finder = WordRangeFinder('(a_var + another_var).reverse()')
        self.assertEquals('(a_var + another_var).reverse',
                          word_finder.get_primary_at(25))

    def test_dictionaries(self):
        word_finder = WordRangeFinder('print {1: "one", 2: "two"}.keys()')
        self.assertEquals('print {1: "one", 2: "two"}.keys',
                          word_finder.get_primary_at(29))

    def test_following_parens(self):
        code = 'a_var = a_func()()'
        word_finder = WordRangeFinder(code)
        self.assertEquals('a_func()()',
                          word_finder.get_primary_at(code.index(')(') + 3))

    # TODO: eliminating comments
    def xxx_test_comments_for_finding_statements(self):
        word_finder = WordRangeFinder('# var2 . \n  var3')
        self.assertEquals('var3',
                          word_finder.get_primary_at(14))

    def test_comments_for_finding_statements2(self):
        word_finder = WordRangeFinder('var1 + "# var2".\n  var3')
        self.assertEquals('"# var2".\n  var3',
                          word_finder.get_primary_at(21))

    def test_import_statement_finding(self):
        code = 'import mod\na_var = 10\n'
        word_finder = WordRangeFinder(code)
        self.assertTrue(word_finder.is_import_statement(code.index('mod') + 1))
        self.assertFalse(word_finder.is_import_statement(code.index('a_var') + 1))

    def test_import_statement_finding2(self):
        code = 'import a.b.c.d\nresult = a.b.c.d.f()\n'
        word_finder = WordRangeFinder(code)
        self.assertFalse(word_finder.is_import_statement(code.rindex('d') + 1))

    def test_word_parens_range(self):
        code = 's = str()\ns.title()\n'
        word_finder = WordRangeFinder(code)
        result = word_finder.get_word_parens_range(code.rindex('()') - 1)
        self.assertEquals((len(code) - 3, len(code) - 1), result)


class ScopeNameFinderTest(unittest.TestCase):

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

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

    # FIXME: in normal scopes the interpreter raises `UnboundLocalName`
    # exception, but not in class bodies
    def xxx_test_global_name_in_class_body(self):
        code = 'a_var = 10\nclass Sample(object):\n    a_var = a_var\n'
        scope = self.pycore.get_string_scope(code)
        name_finder = ScopeNameFinder(scope.pyobject)
        self.assertEquals(scope.get_name('a_var'), name_finder.get_pyname_at(len(code) - 3))

    def test_class_variable_attribute_in_class_body(self):
        code = 'a_var = 10\nclass Sample(object):\n    a_var = a_var\n'
        scope = self.pycore.get_string_scope(code)
        name_finder = ScopeNameFinder(scope.pyobject)
        a_var_pyname = scope.get_name('Sample').get_object().get_attribute('a_var')
        self.assertEquals(a_var_pyname, name_finder.get_pyname_at(len(code) - 12))

    def test_class_variable_attribute_in_class_body2(self):
        code = 'a_var = 10\nclass Sample(object):\n    a_var \\\n= a_var\n'
        scope = self.pycore.get_string_scope(code)
        name_finder = ScopeNameFinder(scope.pyobject)
        a_var_pyname = scope.get_name('Sample').get_object().get_attribute('a_var')
        self.assertEquals(a_var_pyname, name_finder.get_pyname_at(len(code) - 12))

    def test_class_method_attribute_in_class_body(self):
        code = 'class Sample(object):\n    def a_method(self):\n        pass\n'
        scope = self.pycore.get_string_scope(code)
        name_finder = ScopeNameFinder(scope.pyobject)
        a_method_pyname = scope.get_name('Sample').get_object().get_attribute('a_method')
        self.assertEquals(a_method_pyname,
                          name_finder.get_pyname_at(code.index('a_method') + 2))

    def test_inner_class_attribute_in_class_body(self):
        code = 'class Sample(object):\n    class AClass(object):\n        pass\n'
        scope = self.pycore.get_string_scope(code)
        name_finder = ScopeNameFinder(scope.pyobject)
        a_class_pyname = scope.get_name('Sample').get_object().get_attribute('AClass')
        self.assertEquals(a_class_pyname,
                          name_finder.get_pyname_at(code.index('AClass') + 2))

    def test_class_method_in_class_body_but_not_indexed(self):
        code = 'class Sample(object):\n    def a_func(self, a_func):\n        pass\n'
        scope = self.pycore.get_string_scope(code)
        a_func_pyname = scope.get_scopes()[0].get_scopes()[0].get_name('a_func')
        name_finder = ScopeNameFinder(scope.pyobject)
        self.assertEquals(a_func_pyname, name_finder.get_pyname_at(code.index(', a_func') + 3))

    def test_function_but_not_indexed(self):
        code = 'def a_func(a_func):\n    pass\n'
        scope = self.pycore.get_string_scope(code)
        a_func_pyname = scope.get_name('a_func')
        name_finder = ScopeNameFinder(scope.pyobject)
        self.assertEquals(a_func_pyname, name_finder.get_pyname_at(code.index('a_func') + 3))

    def test_modules_after_from_statements(self):
        root_folder = self.project.root
        mod = self.pycore.create_module(root_folder, 'mod')
        mod.write('def a_func():\n    pass\n')
        code = 'from mod import a_func\n'
        scope = self.pycore.get_string_scope(code)
        name_finder = ScopeNameFinder(scope.pyobject)
        mod_pyobject = self.pycore.resource_to_pyobject(mod)
        found_pyname = name_finder.get_pyname_at(code.index('mod') + 1)
        self.assertEquals(mod_pyobject, found_pyname.get_object())

    @testutils.run_only_for_25
    def test_relative_modules_after_from_statements(self):
        pkg1 = self.pycore.create_package(self.project.root, 'pkg1')
        pkg2 = self.pycore.create_package(pkg1, 'pkg2')
        mod1 = self.pycore.create_module(pkg1, 'mod1')
        mod2 = self.pycore.create_module(pkg2, 'mod2')
        mod1.write('def a_func():\n    pass\n')
        code = 'from ..mod1 import a_func\n'
        mod2.write(code)
        mod2_scope = self.pycore.resource_to_pyobject(mod2).get_scope()
        name_finder = ScopeNameFinder(mod2_scope.pyobject)
        mod1_pyobject = self.pycore.resource_to_pyobject(mod1)
        found_pyname = name_finder.get_pyname_at(code.index('mod1') + 1)
        self.assertEquals(mod1_pyobject, found_pyname.get_object())

    def test_relative_modules_after_from_statements2(self):
        mod1 = self.pycore.create_module(self.project.root, 'mod1')
        pkg1 = self.pycore.create_package(self.project.root, 'pkg1')
        pkg2 = self.pycore.create_package(pkg1, 'pkg2')
        mod2 = self.pycore.create_module(pkg2, 'mod2')
        mod1.write('import pkg1.pkg2.mod2')

        mod1_scope = self.pycore.resource_to_pyobject(mod1).get_scope()
        name_finder = ScopeNameFinder(mod1_scope.pyobject)
        pkg2_pyobject = self.pycore.resource_to_pyobject(pkg2)
        found_pyname = name_finder.get_pyname_at(mod1.read().index('pkg2') + 1)
        self.assertEquals(pkg2_pyobject, found_pyname.get_object())

    @testutils.assert_raises(exceptions.RopeError)
    def test_get_pyname_at_on_language_keywords(self):
        code = 'def a_func(a_func):\n    pass\n'
        pymod = self.pycore.get_string_module(code)
        name_finder = ScopeNameFinder(pymod)
        name_finder.get_pyname_at(code.index('pass'))


class LogicalLineFinderTest(unittest.TestCase):

    def setUp(self):
        super(LogicalLineFinderTest, self).setUp()

    def tearDown(self):
        super(LogicalLineFinderTest, self).tearDown()

    def test_normal_lines(self):
        code = 'a_var = 10'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((1, 1), line_finder.get_logical_line_in(1))

    def test_normal_lines2(self):
        code = 'another = 10\na_var = 20\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((1, 1), line_finder.get_logical_line_in(1))
        self.assertEquals((2, 2), line_finder.get_logical_line_in(2))

    def test_implicit_continuation(self):
        code = 'a_var = 3 + \\\n    4 + \\\n    5'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((1, 3), line_finder.get_logical_line_in(2))

    def test_explicit_continuation(self):
        code = 'print 2\na_var = (3 + \n    4, \n    5)\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((2, 4), line_finder.get_logical_line_in(2))

    def test_explicit_continuation_comments(self):
        code = '#\na_var = 3\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((2, 2), line_finder.get_logical_line_in(2))

    def test_multiple_indented_ifs(self):
        code = 'if True:\n    if True:\n        if True:\n            pass\n    a = 10\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((5, 5), line_finder.get_logical_line_in(5))

    def test_list_comprehensions_and_fors(self):
        code = 'a_list = [i\n    for i in range(10)]\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((1, 2), line_finder.get_logical_line_in(2))

    def test_generator_expressions_and_fors(self):
        code = 'a_list = (i\n    for i in range(10))\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((1, 2), line_finder.get_logical_line_in(2))

    def test_fors_and_block_start(self):
        code = 'l = range(10)\nfor i in l:\n    print i\n'
        self.assertEquals(2, get_block_start(SourceLinesAdapter(code), 2))

    def test_problems_with_inner_indentations(self):
        code = 'if True:\n    if True:\n        if True:\n            pass\n' \
               '    a = \\\n        1\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((5, 6), line_finder.get_logical_line_in(6))

    def test_problems_with_inner_indentations2(self):
        code = 'if True:\n    if True:\n        pass\n' \
               'a = 1\n'
        line_finder = LogicalLineFinder(SourceLinesAdapter(code))
        self.assertEquals((4, 4), line_finder.get_logical_line_in(4))


def suite():
    result = unittest.TestSuite()
    result.addTests(unittest.makeSuite(StatementRangeFinderTest))
    result.addTests(unittest.makeSuite(WordRangeFinderTest))
    result.addTests(unittest.makeSuite(ScopeNameFinderTest))
    result.addTests(unittest.makeSuite(LogicalLineFinderTest))
    return result

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