fix the way tabs are used

The previous code just assumed a tab was 4 spaces wide.
This is not true, because a tab can be between 1 and 4 spaces wide.
This commit is contained in:
2021-12-20 19:44:46 +01:00
parent 3c27913e75
commit ffea831e2d
4 changed files with 75 additions and 20 deletions

View File

@@ -6,25 +6,56 @@ from line import Line
class MyTestCase(unittest.TestCase):
def test_column_to_char(self):
byte_offset = 123
text = "\tabc" # will be rendered as: ........abc where . represents a whitespace column
text = "\tabc\td\tef\tg" # will be rendered as: ....abc.d...ef..g where . represents a whitespace column
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
self.assertEqual(0, line.column_to_char(0)) # the tab
self.assertEqual(0, line.column_to_char(1)) # the tab
self.assertEqual(0, line.column_to_char(2)) # the tab
self.assertEqual(0, line.column_to_char(3)) # last column of the tab
self.assertEqual(1, line.column_to_char(4)) # a
self.assertEqual(2, line.column_to_char(5)) # b
self.assertEqual(3, line.column_to_char(6)) # c
self.assertEqual(4, line.column_to_char(7)) # tab
self.assertEqual(5, line.column_to_char(8)) # d
self.assertEqual(6, line.column_to_char(9)) # tab
self.assertEqual(6, line.column_to_char(10)) # tab
self.assertEqual(6, line.column_to_char(11)) # tab
self.assertEqual(7, line.column_to_char(12)) # e
self.assertEqual(8, line.column_to_char(13)) # f
self.assertEqual(9, line.column_to_char(14)) # tab
self.assertEqual(9, line.column_to_char(15)) # tab
self.assertEqual(10, line.column_to_char(16)) # g
def test_char_to_column(self):
byte_offset = 123
text = "\tabc" # will be rendered as: ........abc where . represents a whitespace column
text = "\tabc\td\tef\tg" # will be rendered as: ....abc.d...ef..g where . represents a whitespace column
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
self.assertEqual(0, line.char_to_column(0))
self.assertEqual(4, line.char_to_column(1))
self.assertEqual(5, line.char_to_column(2))
self.assertEqual(6, line.char_to_column(3))
self.assertEqual(0, line.char_to_column(0)) # tab
self.assertEqual(4, line.char_to_column(1)) # a
self.assertEqual(5, line.char_to_column(2)) # b
self.assertEqual(6, line.char_to_column(3)) # c
self.assertEqual(7, line.char_to_column(4)) # tab
self.assertEqual(8, line.char_to_column(5)) # d
self.assertEqual(9, line.char_to_column(6)) # tab
self.assertEqual(12, line.char_to_column(7)) # e
self.assertEqual(13, line.char_to_column(8)) # f
self.assertEqual(14, line.char_to_column(9)) # tab
self.assertEqual(16, line.char_to_column(10)) # g
def test_line_tabs_replaced(self):
byte_offset = 123
text = "\ta\tb" # will be rendered as: ....abc where . represents a whitespace column
expected = " a b"
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
self.assertEqual(expected, line.line_tabs_replaced())
def test_line_tabs_replaced_performance(self):
byte_offset = 123
text = "a\t" * 10000
expected = "a " * 10000
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
self.assertEqual(expected, line.line_tabs_replaced())
if __name__ == '__main__':
unittest.main()