~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
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
21
from bzrlib.tests import (
22
    features,
23
    )
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
24
25
26
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.
27
    suite, _ = tests.permute_tests_for_extension(
28
        standard_tests, loader, 'bzrlib._chunks_to_lines_py',
29
        'bzrlib._chunks_to_lines_pyx')
4913.3.1 by John Arbash Meinel
Implement a permute_for_extension helper.
30
    return suite
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
31
4913.3.5 by John Arbash Meinel
Handle the fact that osutils requires the feature to be available.
32
# test_osutils depends on this feature being around. We can't just use the one
33
# generated by load_tests, because if we only load osutils but not this module,
34
# then that code never gets run
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
35
compiled_chunkstolines_feature = features.ModuleAvailableFeature(
36
    'bzrlib._chunks_to_lines_pyx')
4913.3.5 by John Arbash Meinel
Handle the fact that osutils requires the feature to be available.
37
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
38
39
class TestChunksToLines(tests.TestCase):
40
41
    module = None # Filled in by test parameterization
42
43
    def assertChunksToLines(self, lines, chunks, alreadly_lines=False):
44
        result = self.module.chunks_to_lines(chunks)
45
        self.assertEqual(lines, result)
46
        if alreadly_lines:
47
            self.assertIs(chunks, result)
48
49
    def test_fulltext_chunk_to_lines(self):
50
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
51
                                 ['foo\nbar\r\nba\rz\n'])
52
        self.assertChunksToLines(['foobarbaz\n'], ['foobarbaz\n'],
53
                                 alreadly_lines=True)
3890.2.15 by John Arbash Meinel
Update to do a single iteration over the chunks.
54
        self.assertChunksToLines(['foo\n', 'bar\n', '\n', 'baz\n', '\n', '\n'],
55
                                 ['foo\nbar\n\nbaz\n\n\n'])
3890.2.17 by John Arbash Meinel
Add a few more corner cases, some suggested by Robert.
56
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
57
                                 alreadly_lines=True)
58
        self.assertChunksToLines(['foobarbaz'], ['foo', 'bar', 'baz'])
59
60
    def test_newlines(self):
61
        self.assertChunksToLines(['\n'], ['\n'], alreadly_lines=True)
62
        self.assertChunksToLines(['\n'], ['', '\n', ''])
63
        self.assertChunksToLines(['\n'], ['\n', ''])
64
        self.assertChunksToLines(['\n'], ['', '\n'])
65
        self.assertChunksToLines(['\n', '\n', '\n'], ['\n\n\n'])
66
        self.assertChunksToLines(['\n', '\n', '\n'], ['\n', '\n', '\n'],
67
                                 alreadly_lines=True)
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
68
69
    def test_lines_to_lines(self):
70
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
71
                                 ['foo\n', 'bar\r\n', 'ba\rz\n'],
72
                                 alreadly_lines=True)
73
74
    def test_no_final_newline(self):
75
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
76
                                 ['foo\nbar\r\nba\rz'])
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(('foo\n', 'bar\r\n', 'ba\rz'),
81
                                 ('foo\n', 'bar\r\n', 'ba\rz'),
82
                                 alreadly_lines=True)
83
        self.assertChunksToLines([], [], alreadly_lines=True)
84
        self.assertChunksToLines(['foobarbaz'], ['foobarbaz'],
85
                                 alreadly_lines=True)
86
        self.assertChunksToLines([], [''])
87
88
    def test_mixed(self):
89
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
90
                                 ['foo\n', 'bar\r\nba\r', 'z'])
91
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
92
                                 ['foo\nb', 'a', 'r\r\nba\r', 'z'])
93
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
94
                                 ['foo\nbar\r\nba', '\r', 'z'])
95
96
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz'],
97
                                 ['foo\n', '', 'bar\r\nba', '\r', 'z'])
98
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
99
                                 ['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
100
        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
101
                                 ['foo\n', 'bar', '\r\n', 'ba\rz\n'])
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
102
103
    def test_not_lines(self):
104
        # We should raise a TypeError, not crash
105
        self.assertRaises(TypeError, self.module.chunks_to_lines,
106
                          object())
107
        self.assertRaises(TypeError, self.module.chunks_to_lines,
108
                          [object()])
109
        self.assertRaises(TypeError, self.module.chunks_to_lines,
110
                          ['foo', object()])