| 1 |
"""Tests for distutils.text_file.""" |
| 2 |
import os |
| 3 |
import unittest |
| 4 |
from distutils.text_file import TextFile |
| 5 |
from distutils.tests import support |
| 6 |
|
| 7 |
TEST_DATA = """# test file |
| 8 |
|
| 9 |
line 3 \\ |
| 10 |
# intervening comment |
| 11 |
continues on next line |
| 12 |
""" |
| 13 |
|
| 14 |
class TextFileTestCase(support.TempdirManager, unittest.TestCase): |
| 15 |
|
| 16 |
def test_class(self): |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
result1 = ['# test file\n', '\n', 'line 3 \\\n', |
| 22 |
'# intervening comment\n', |
| 23 |
' continues on next line\n'] |
| 24 |
|
| 25 |
|
| 26 |
result2 = ["\n", |
| 27 |
"line 3 \\\n", |
| 28 |
" continues on next line\n"] |
| 29 |
|
| 30 |
|
| 31 |
result3 = ["# test file\n", |
| 32 |
"line 3 \\\n", |
| 33 |
"# intervening comment\n", |
| 34 |
" continues on next line\n"] |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
result4 = ["line 3 \\", |
| 39 |
" continues on next line"] |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
result5 = ["line 3 continues on next line"] |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
result6 = ["line 3 continues on next line"] |
| 48 |
|
| 49 |
def test_input(count, description, file, expected_result): |
| 50 |
result = file.readlines() |
| 51 |
self.assertEqual(result, expected_result) |
| 52 |
|
| 53 |
tmpdir = self.mkdtemp() |
| 54 |
filename = os.path.join(tmpdir, "test.txt") |
| 55 |
out_file = open(filename, "w") |
| 56 |
try: |
| 57 |
out_file.write(TEST_DATA) |
| 58 |
finally: |
| 59 |
out_file.close() |
| 60 |
|
| 61 |
in_file = TextFile(filename, strip_comments=0, skip_blanks=0, |
| 62 |
lstrip_ws=0, rstrip_ws=0) |
| 63 |
try: |
| 64 |
test_input(1, "no processing", in_file, result1) |
| 65 |
finally: |
| 66 |
in_file.close() |
| 67 |
|
| 68 |
in_file = TextFile(filename, strip_comments=1, skip_blanks=0, |
| 69 |
lstrip_ws=0, rstrip_ws=0) |
| 70 |
try: |
| 71 |
test_input(2, "strip comments", in_file, result2) |
| 72 |
finally: |
| 73 |
in_file.close() |
| 74 |
|
| 75 |
in_file = TextFile(filename, strip_comments=0, skip_blanks=1, |
| 76 |
lstrip_ws=0, rstrip_ws=0) |
| 77 |
try: |
| 78 |
test_input(3, "strip blanks", in_file, result3) |
| 79 |
finally: |
| 80 |
in_file.close() |
| 81 |
|
| 82 |
in_file = TextFile(filename) |
| 83 |
try: |
| 84 |
test_input(4, "default processing", in_file, result4) |
| 85 |
finally: |
| 86 |
in_file.close() |
| 87 |
|
| 88 |
in_file = TextFile(filename, strip_comments=1, skip_blanks=1, |
| 89 |
join_lines=1, rstrip_ws=1) |
| 90 |
try: |
| 91 |
test_input(5, "join lines without collapsing", in_file, result5) |
| 92 |
finally: |
| 93 |
in_file.close() |
| 94 |
|
| 95 |
in_file = TextFile(filename, strip_comments=1, skip_blanks=1, |
| 96 |
join_lines=1, rstrip_ws=1, collapse_join=1) |
| 97 |
try: |
| 98 |
test_input(6, "join lines with collapsing", in_file, result6) |
| 99 |
finally: |
| 100 |
in_file.close() |
| 101 |
|
| 102 |
if __name__ == "__main__": |
| 103 |
unittest.main() |