1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Tests for chunks_to_lines."""
20
from bzrlib import tests
23
def load_tests(standard_tests, module, loader):
24
suite, _ = tests.permute_tests_for_extension(
25
standard_tests, loader, 'bzrlib._chunks_to_lines_py',
26
'bzrlib._chunks_to_lines_pyx')
29
# test_osutils depends on this feature being around. We can't just use the one
30
# generated by load_tests, because if we only load osutils but not this module,
31
# then that code never gets run
32
compiled_chunkstolines_feature = tests.ModuleAvailableFeature(
33
'bzrlib._chunks_to_lines_pyx')
36
class TestChunksToLines(tests.TestCase):
38
module = None # Filled in by test parameterization
40
def assertChunksToLines(self, lines, chunks, alreadly_lines=False):
41
result = self.module.chunks_to_lines(chunks)
42
self.assertEqual(lines, result)
44
self.assertIs(chunks, result)
46
def test_fulltext_chunk_to_lines(self):
47
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
48
['foo\nbar\r\nba\rz\n'])
49
self.assertChunksToLines(['foobarbaz\n'], ['foobarbaz\n'],
51
self.assertChunksToLines(['foo\n', 'bar\n', '\n', 'baz\n', '\n', '\n'],
52
['foo\nbar\n\nbaz\n\n\n'])
53
self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
55
self.assertChunksToLines(['foobarbaz'], ['foo', 'bar', 'baz'])
57
def test_newlines(self):
58
self.assertChunksToLines(['\n'], ['\n'], alreadly_lines=True)
59
self.assertChunksToLines(['\n'], ['', '\n', ''])
60
self.assertChunksToLines(['\n'], ['\n', ''])
61
self.assertChunksToLines(['\n'], ['', '\n'])
62
self.assertChunksToLines(['\n', '\n', '\n'], ['\n\n\n'])
63
self.assertChunksToLines(['\n', '\n', '\n'], ['\n', '\n', '\n'],
66
def test_lines_to_lines(self):
67
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
68
['foo\n', 'bar\r\n', 'ba\rz\n'],
71
def test_no_final_newline(self):
72
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
73
['foo\nbar\r\nba\rz'])
74
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
75
['foo\n', 'bar\r\n', 'ba\rz'],
77
self.assertChunksToLines(('foo\n', 'bar\r\n', 'ba\rz'),
78
('foo\n', 'bar\r\n', 'ba\rz'),
80
self.assertChunksToLines([], [], alreadly_lines=True)
81
self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
83
self.assertChunksToLines([], [''])
86
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
87
['foo\n', 'bar\r\nba\r', 'z'])
88
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
89
['foo\nb', 'a', 'r\r\nba\r', 'z'])
90
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
91
['foo\nbar\r\nba', '\r', 'z'])
93
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
94
['foo\n', '', 'bar\r\nba', '\r', 'z'])
95
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
96
['foo\n', 'bar\r\n', 'ba\rz\n', ''])
97
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
98
['foo\n', 'bar', '\r\n', 'ba\rz\n'])
100
def test_not_lines(self):
101
# We should raise a TypeError, not crash
102
self.assertRaises(TypeError, self.module.chunks_to_lines,
104
self.assertRaises(TypeError, self.module.chunks_to_lines,
106
self.assertRaises(TypeError, self.module.chunks_to_lines,