~bzr-pqm/bzr/bzr.dev

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})]
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
27
    if CompiledChunksToLinesFeature.available():
28
        import bzrlib._chunks_to_lines_pyx as c_module
4084.5.1 by Robert Collins
Bulk update all test adaptation into a single approach, using multiply_tests rather than test adapters.
29
        scenarios.append(('C', {'module': c_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):
34
                self.requireFeature(CompiledChunksToLinesFeature)
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
39
class _CompiledChunksToLinesFeature(tests.Feature):
40
41
    def _probe(self):
42
        try:
43
            import bzrlib._chunks_to_lines_pyx
44
        except ImportError:
45
            return False
46
        return True
47
48
    def feature_name(self):
49
        return 'bzrlib._chunks_to_lines_pyx'
50
51
CompiledChunksToLinesFeature = _CompiledChunksToLinesFeature()
52
53
54
class TestChunksToLines(tests.TestCase):
55
56
    module = None # Filled in by test parameterization
57
58
    def assertChunksToLines(self, lines, chunks, alreadly_lines=False):
59
        result = self.module.chunks_to_lines(chunks)
60
        self.assertEqual(lines, result)
61
        if alreadly_lines:
62
            self.assertIs(chunks, result)
63
64
    def test_fulltext_chunk_to_lines(self):
65
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
66
                                 ['foo\nbar\r\nba\rz\n'])
67
        self.assertChunksToLines(['foobarbaz\n'], ['foobarbaz\n'],
68
                                 alreadly_lines=True)
3890.2.15 by John Arbash Meinel
Update to do a single iteration over the chunks.
69
        self.assertChunksToLines(['foo\n', 'bar\n', '\n', 'baz\n', '\n', '\n'],
70
                                 ['foo\nbar\n\nbaz\n\n\n'])
3890.2.17 by John Arbash Meinel
Add a few more corner cases, some suggested by Robert.
71
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
72
                                 alreadly_lines=True)
73
        self.assertChunksToLines(['foobarbaz'], ['foo', 'bar', 'baz'])
74
75
    def test_newlines(self):
76
        self.assertChunksToLines(['\n'], ['\n'], alreadly_lines=True)
77
        self.assertChunksToLines(['\n'], ['', '\n', ''])
78
        self.assertChunksToLines(['\n'], ['\n', ''])
79
        self.assertChunksToLines(['\n'], ['', '\n'])
80
        self.assertChunksToLines(['\n', '\n', '\n'], ['\n\n\n'])
81
        self.assertChunksToLines(['\n', '\n', '\n'], ['\n', '\n', '\n'],
82
                                 alreadly_lines=True)
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
83
84
    def test_lines_to_lines(self):
85
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
86
                                 ['foo\n', 'bar\r\n', 'ba\rz\n'],
87
                                 alreadly_lines=True)
88
89
    def test_no_final_newline(self):
90
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
91
                                 ['foo\nbar\r\nba\rz'])
92
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
93
                                 ['foo\n', 'bar\r\n', 'ba\rz'],
94
                                 alreadly_lines=True)
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([], [], alreadly_lines=True)
99
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
100
                                 alreadly_lines=True)
101
        self.assertChunksToLines([], [''])
102
103
    def test_mixed(self):
104
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
105
                                 ['foo\n', 'bar\r\nba\r', 'z'])
106
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
107
                                 ['foo\nb', 'a', 'r\r\nba\r', 'z'])
108
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
109
                                 ['foo\nbar\r\nba', '\r', 'z'])
110
111
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
112
                                 ['foo\n', '', 'bar\r\nba', '\r', 'z'])
113
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
114
                                 ['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
115
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
116
                                 ['foo\n', 'bar', '\r\n', 'ba\rz\n'])
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
117
118
    def test_not_lines(self):
119
        # We should raise a TypeError, not crash
120
        self.assertRaises(TypeError, self.module.chunks_to_lines,
121
                          object())
122
        self.assertRaises(TypeError, self.module.chunks_to_lines,
123
                          [object()])
124
        self.assertRaises(TypeError, self.module.chunks_to_lines,
125
                          ['foo', object()])