/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
1
# Copyright (C) 2008 Canonical Ltd
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):
24
    # parameterize all tests in this module
25
    import bzrlib._chunks_to_lines_py as py_module
4084.5.1 by Robert Collins
Bulk update all test adaptation into a single approach, using multiply_tests rather than test adapters.
26
    scenarios = [('python', {'module': py_module})]
4913.2.3 by John Arbash Meinel
Switch CompiledChecksToLines to ModuleAvailableFeature
27
    if compiled_chunkstolines.available():
28
        scenarios.append(('C', {'module': compiled_chunkstolines.module}))
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
29
    else:
30
        # the compiled module isn't available, so we add a failing test
31
        class FailWithoutFeature(tests.TestCase):
32
            def test_fail(self):
4913.2.3 by John Arbash Meinel
Switch CompiledChecksToLines to ModuleAvailableFeature
33
                self.requireFeature(compiled_chunkstolines)
4084.5.1 by Robert Collins
Bulk update all test adaptation into a single approach, using multiply_tests rather than test adapters.
34
        standard_tests.addTest(FailWithoutFeature("test_fail"))
35
    return tests.multiply_tests(standard_tests, scenarios, loader.suiteClass())
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
36
37
4913.2.3 by John Arbash Meinel
Switch CompiledChecksToLines to ModuleAvailableFeature
38
compiled_chunkstolines = tests.ModuleAvailableFeature(
39
                            'bzrlib._chunks_to_lines_pyx')
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
40
41
42
class TestChunksToLines(tests.TestCase):
43
44
    module = None # Filled in by test parameterization
45
46
    def assertChunksToLines(self, lines, chunks, alreadly_lines=False):
47
        result = self.module.chunks_to_lines(chunks)
48
        self.assertEqual(lines, result)
49
        if alreadly_lines:
50
            self.assertIs(chunks, result)
51
52
    def test_fulltext_chunk_to_lines(self):
53
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
54
                                 ['foo\nbar\r\nba\rz\n'])
55
        self.assertChunksToLines(['foobarbaz\n'], ['foobarbaz\n'],
56
                                 alreadly_lines=True)
3890.2.15 by John Arbash Meinel
Update to do a single iteration over the chunks.
57
        self.assertChunksToLines(['foo\n', 'bar\n', '\n', 'baz\n', '\n', '\n'],
58
                                 ['foo\nbar\n\nbaz\n\n\n'])
3890.2.17 by John Arbash Meinel
Add a few more corner cases, some suggested by Robert.
59
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
60
                                 alreadly_lines=True)
61
        self.assertChunksToLines(['foobarbaz'], ['foo', 'bar', 'baz'])
62
63
    def test_newlines(self):
64
        self.assertChunksToLines(['\n'], ['\n'], alreadly_lines=True)
65
        self.assertChunksToLines(['\n'], ['', '\n', ''])
66
        self.assertChunksToLines(['\n'], ['\n', ''])
67
        self.assertChunksToLines(['\n'], ['', '\n'])
68
        self.assertChunksToLines(['\n', '\n', '\n'], ['\n\n\n'])
69
        self.assertChunksToLines(['\n', '\n', '\n'], ['\n', '\n', '\n'],
70
                                 alreadly_lines=True)
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
71
72
    def test_lines_to_lines(self):
73
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
74
                                 ['foo\n', 'bar\r\n', 'ba\rz\n'],
75
                                 alreadly_lines=True)
76
77
    def test_no_final_newline(self):
78
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
79
                                 ['foo\nbar\r\nba\rz'])
80
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
81
                                 ['foo\n', 'bar\r\n', 'ba\rz'],
82
                                 alreadly_lines=True)
83
        self.assertChunksToLines(('foo\n', 'bar\r\n', 'ba\rz'),
84
                                 ('foo\n', 'bar\r\n', 'ba\rz'),
85
                                 alreadly_lines=True)
86
        self.assertChunksToLines([], [], alreadly_lines=True)
87
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
88
                                 alreadly_lines=True)
89
        self.assertChunksToLines([], [''])
90
91
    def test_mixed(self):
92
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
93
                                 ['foo\n', 'bar\r\nba\r', 'z'])
94
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
95
                                 ['foo\nb', 'a', 'r\r\nba\r', 'z'])
96
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
97
                                 ['foo\nbar\r\nba', '\r', 'z'])
98
99
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
100
                                 ['foo\n', '', 'bar\r\nba', '\r', 'z'])
101
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
102
                                 ['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
103
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
104
                                 ['foo\n', 'bar', '\r\n', 'ba\rz\n'])
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
105
106
    def test_not_lines(self):
107
        # We should raise a TypeError, not crash
108
        self.assertRaises(TypeError, self.module.chunks_to_lines,
109
                          object())
110
        self.assertRaises(TypeError, self.module.chunks_to_lines,
111
                          [object()])
112
        self.assertRaises(TypeError, self.module.chunks_to_lines,
113
                          ['foo', object()])