/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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
    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}))
32
    else:
33
        # the compiled module isn't available, so we add a failing test
34
        class FailWithoutFeature(tests.TestCase):
35
            def test_fail(self):
36
                self.requireFeature(CompiledChunksToLinesFeature)
37
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
38
    tests.adapt_tests(standard_tests, applier, suite)
39
    return suite
40
41
42
class _CompiledChunksToLinesFeature(tests.Feature):
43
44
    def _probe(self):
45
        try:
46
            import bzrlib._chunks_to_lines_pyx
47
        except ImportError:
48
            return False
49
        return True
50
51
    def feature_name(self):
52
        return 'bzrlib._chunks_to_lines_pyx'
53
54
CompiledChunksToLinesFeature = _CompiledChunksToLinesFeature()
55
56
57
class TestChunksToLines(tests.TestCase):
58
59
    module = None # Filled in by test parameterization
60
61
    def assertChunksToLines(self, lines, chunks, alreadly_lines=False):
62
        result = self.module.chunks_to_lines(chunks)
63
        self.assertEqual(lines, result)
64
        if alreadly_lines:
65
            self.assertIs(chunks, result)
66
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'],
71
                                 alreadly_lines=True)
3890.2.15 by John Arbash Meinel
Update to do a single iteration over the chunks.
72
        self.assertChunksToLines(['foo\n', 'bar\n', '\n', 'baz\n', '\n', '\n'],
73
                                 ['foo\nbar\n\nbaz\n\n\n'])
3890.2.17 by John Arbash Meinel
Add a few more corner cases, some suggested by Robert.
74
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
75
                                 alreadly_lines=True)
76
        self.assertChunksToLines(['foobarbaz'], ['foo', 'bar', 'baz'])
77
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'],
85
                                 alreadly_lines=True)
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
86
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'],
90
                                 alreadly_lines=True)
91
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'],
97
                                 alreadly_lines=True)
98
        self.assertChunksToLines(('foo\n', 'bar\r\n', 'ba\rz'),
99
                                 ('foo\n', 'bar\r\n', 'ba\rz'),
100
                                 alreadly_lines=True)
101
        self.assertChunksToLines([], [], alreadly_lines=True)
102
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
103
                                 alreadly_lines=True)
104
        self.assertChunksToLines([], [''])
105
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'])
113
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', ''])
3890.2.11 by John Arbash Meinel
A bit more tweaking of the pyrex version. Shave off another 10% by
118
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
119
                                 ['foo\n', 'bar', '\r\n', 'ba\rz\n'])
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
120
121
    def test_not_lines(self):
122
        # We should raise a TypeError, not crash
123
        self.assertRaises(TypeError, self.module.chunks_to_lines,
124
                          object())
125
        self.assertRaises(TypeError, self.module.chunks_to_lines,
126
                          [object()])
127
        self.assertRaises(TypeError, self.module.chunks_to_lines,
128
                          ['foo', object()])