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
|
import textwrap
from unittest import TestCase
from ..Code import _indent_chunk
class TestIndent(TestCase):
def _test_indentations(self, chunk, expected):
for indentation in range(16):
expected_indented = textwrap.indent(expected, ' ' * indentation)
for line in expected_indented.splitlines():
# Validate before the comparison that empty lines got stripped also by textwrap.indent().
self.assertTrue(line == '' or line.strip(), repr(line))
with self.subTest(indentation=indentation):
result = _indent_chunk(chunk, indentation_length=indentation)
self.assertEqual(expected_indented, result)
def test_indent_empty(self):
self._test_indentations('', '')
def test_indent_empty_lines(self):
self._test_indentations('\n', '\n')
self._test_indentations('\n'*2, '\n'*2)
self._test_indentations('\n'*3, '\n'*3)
self._test_indentations(' \n'*2, '\n'*2)
self._test_indentations('\n \n \n \n', '\n'*4)
def test_indent_one_line(self):
self._test_indentations('abc', 'abc')
def test_indent_chunk(self):
chunk = """
x = 1
if x == 2:
print("False")
else:
print("True")
"""
expected = """
x = 1
if x == 2:
print("False")
else:
print("True")
"""
self._test_indentations(chunk, expected)
def test_indent_empty_line(self):
chunk = """
x = 1
if x == 2:
print("False")
else:
print("True")
"""
expected = """
x = 1
if x == 2:
print("False")
else:
print("True")
"""
self._test_indentations(chunk, expected)
def test_indent_empty_line_unclean(self):
lines = """
x = 1
if x == 2:
print("False")
else:
print("True")
""".splitlines(keepends=True)
lines[2] = ' \n'
chunk = ''.join(lines)
expected = """
x = 1
if x == 2:
print("False")
else:
print("True")
"""
self._test_indentations(chunk, expected)
|