Test added newline module end_pos as well.

This commit is contained in:
Dave Halter
2015-01-26 22:02:11 +01:00
parent cdae250b36
commit 39e869d146
2 changed files with 17 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ complexity of the ``Parser`` (there's another parser sitting inside
``Statement``, which produces ``Array`` and ``Call``).
"""
import os
import re
from jedi.parser import tree as pt
from jedi.parser import tokenize
@@ -136,7 +137,7 @@ class Parser(object):
self.module = p.parse(self._tokenize(tokenizer))
if added_newline:
self._remove_last_newline(source)
self.remove_last_newline()
self.module.used_names = self.used_names
self.module.path = module_path
self.module.set_global_names(self.global_names)
@@ -309,13 +310,15 @@ class Parser(object):
def __repr__(self):
return "<%s: %s>" % (type(self).__name__, self.module)
def _remove_last_newline(self, source):
def remove_last_newline(self):
endmarker = self.module.children[-1]
print('ENDNL', self.module.children, repr(endmarker.prefix))
# The newline is either in the endmarker as a prefix or the previous
# leaf as a newline token.
if endmarker.prefix.endswith('\n'):
endmarker.prefix = endmarker.prefix[:-1]
last_line = re.sub('.*\n', '', endmarker.prefix)
endmarker.start_pos = endmarker.start_pos[0] - 1, len(last_line)
else:
newline = endmarker.get_previous()
while True:
@@ -325,4 +328,5 @@ class Parser(object):
else:
assert newline.value == '\n'
newline.value = ''
endmarker.start_pos = newline.start_pos
break

View File

@@ -93,12 +93,15 @@ def test_end_newlines():
wants to be able, to return the exact same code without the additional new
line the parser needs.
"""
def test(source):
assert Parser(load_grammar(), source).module.get_code() == source
def test(source, end_pos):
module = Parser(load_grammar(), source).module
assert module.get_code() == source
assert module.end_pos == end_pos
test('a')
test('a\nb')
test('a\n')
test('a\n\n')
test('a\n#comment')
test('def a():\n pass')
test('a', (1, 1))
test('a\n', (2, 0))
test('a\nb', (2, 1))
test('a\n#comment\n', (3, 0))
test('a\n#comment', (2, 8))
test('a#comment', (1, 9))
test('def a():\n pass', (2, 5))