1
# Copyright (C) 2008 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for chunks_to_lines."""
20
from bzrlib import tests
23
def load_tests(standard_tests, module, loader):
24
# parameterize all tests in this module
25
suite = loader.suiteClass()
26
applier = tests.TestScenarioApplier()
27
import bzrlib._chunks_to_lines_py as py_module
28
applier.scenarios = [('python', {'module': py_module})]
29
if CompiledChunksToLinesFeature.available():
30
import bzrlib._chunks_to_lines_pyx as c_module
31
applier.scenarios.append(('C', {'module': c_module}))
33
# the compiled module isn't available, so we add a failing test
34
class FailWithoutFeature(tests.TestCase):
36
self.requireFeature(CompiledChunksToLinesFeature)
37
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
38
tests.adapt_tests(standard_tests, applier, suite)
42
class _CompiledChunksToLinesFeature(tests.Feature):
46
import bzrlib._chunks_to_lines_pyx
51
def feature_name(self):
52
return 'bzrlib._chunks_to_lines_pyx'
54
CompiledChunksToLinesFeature = _CompiledChunksToLinesFeature()
57
class TestChunksToLines(tests.TestCase):
59
module = None # Filled in by test parameterization
61
def assertChunksToLines(self, lines, chunks, alreadly_lines=False):
62
result = self.module.chunks_to_lines(chunks)
63
self.assertEqual(lines, result)
65
self.assertIs(chunks, result)
67
def test_fulltext_chunk_to_lines(self):
68
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
69
['foo\nbar\r\nba\rz\n'])
70
self.assertChunksToLines(['foobarbaz\n'], ['foobarbaz\n'],
72
self.assertChunksToLines(['foo\n', 'bar\n', '\n', 'baz\n', '\n', '\n'],
73
['foo\nbar\n\nbaz\n\n\n'])
74
self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
76
self.assertChunksToLines(['foobarbaz'], ['foo', 'bar', 'baz'])
78
def test_newlines(self):
79
self.assertChunksToLines(['\n'], ['\n'], alreadly_lines=True)
80
self.assertChunksToLines(['\n'], ['', '\n', ''])
81
self.assertChunksToLines(['\n'], ['\n', ''])
82
self.assertChunksToLines(['\n'], ['', '\n'])
83
self.assertChunksToLines(['\n', '\n', '\n'], ['\n\n\n'])
84
self.assertChunksToLines(['\n', '\n', '\n'], ['\n', '\n', '\n'],
87
def test_lines_to_lines(self):
88
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
89
['foo\n', 'bar\r\n', 'ba\rz\n'],
92
def test_no_final_newline(self):
93
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
94
['foo\nbar\r\nba\rz'])
95
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
96
['foo\n', 'bar\r\n', 'ba\rz'],
98
self.assertChunksToLines(('foo\n', 'bar\r\n', 'ba\rz'),
99
('foo\n', 'bar\r\n', 'ba\rz'),
101
self.assertChunksToLines([], [], alreadly_lines=True)
102
self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
104
self.assertChunksToLines([], [''])
106
def test_mixed(self):
107
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
108
['foo\n', 'bar\r\nba\r', 'z'])
109
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
110
['foo\nb', 'a', 'r\r\nba\r', 'z'])
111
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
112
['foo\nbar\r\nba', '\r', 'z'])
114
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
115
['foo\n', '', 'bar\r\nba', '\r', 'z'])
116
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
117
['foo\n', 'bar\r\n', 'ba\rz\n', ''])
118
self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
119
['foo\n', 'bar', '\r\n', 'ba\rz\n'])
121
def test_not_lines(self):
122
# We should raise a TypeError, not crash
123
self.assertRaises(TypeError, self.module.chunks_to_lines,
125
self.assertRaises(TypeError, self.module.chunks_to_lines,
127
self.assertRaises(TypeError, self.module.chunks_to_lines,