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