~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
16
#
17
18
"""Tests for chunks_to_lines."""
19
20
from bzrlib import tests
21
22
23
def load_tests(standard_tests, module, loader):
4913.3.5 by John Arbash Meinel
Handle the fact that osutils requires the feature to be available.
24
    suite, _ = tests.permute_tests_for_extension(
25
        standard_tests, loader, 'bzrlib._chunks_to_lines_py',
26
        'bzrlib._chunks_to_lines_pyx')
4913.3.1 by John Arbash Meinel
Implement a permute_for_extension helper.
27
    return suite
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
28
4913.3.5 by John Arbash Meinel
Handle the fact that osutils requires the feature to be available.
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')
34
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
35
36
class TestChunksToLines(tests.TestCase):
37
38
    module = None # Filled in by test parameterization
39
40
    def assertChunksToLines(self, lines, chunks, alreadly_lines=False):
41
        result = self.module.chunks_to_lines(chunks)
42
        self.assertEqual(lines, result)
43
        if alreadly_lines:
44
            self.assertIs(chunks, result)
45
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'],
50
                                 alreadly_lines=True)
3890.2.15 by John Arbash Meinel
Update to do a single iteration over the chunks.
51
        self.assertChunksToLines(['foo\n', 'bar\n', '\n', 'baz\n', '\n', '\n'],
52
                                 ['foo\nbar\n\nbaz\n\n\n'])
3890.2.17 by John Arbash Meinel
Add a few more corner cases, some suggested by Robert.
53
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
54
                                 alreadly_lines=True)
55
        self.assertChunksToLines(['foobarbaz'], ['foo', 'bar', 'baz'])
56
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'],
64
                                 alreadly_lines=True)
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
65
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'],
69
                                 alreadly_lines=True)
70
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'],
76
                                 alreadly_lines=True)
77
        self.assertChunksToLines(('foo\n', 'bar\r\n', 'ba\rz'),
78
                                 ('foo\n', 'bar\r\n', 'ba\rz'),
79
                                 alreadly_lines=True)
80
        self.assertChunksToLines([], [], alreadly_lines=True)
81
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
82
                                 alreadly_lines=True)
83
        self.assertChunksToLines([], [''])
84
85
    def test_mixed(self):
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'])
92
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', ''])
3890.2.11 by John Arbash Meinel
A bit more tweaking of the pyrex version. Shave off another 10% by
97
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
98
                                 ['foo\n', 'bar', '\r\n', 'ba\rz\n'])
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
99
100
    def test_not_lines(self):
101
        # We should raise a TypeError, not crash
102
        self.assertRaises(TypeError, self.module.chunks_to_lines,
103
                          object())
104
        self.assertRaises(TypeError, self.module.chunks_to_lines,
105
                          [object()])
106
        self.assertRaises(TypeError, self.module.chunks_to_lines,
107
                          ['foo', object()])