~ubuntu-branches/ubuntu/karmic/bzr/karmic-proposed

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_filters.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-04-11 14:25:12 UTC
  • mfrom: (1.1.51 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090411142512-jomeween5l8etlrm
Add missing dependency on zlib development library. (Closes:
#523595)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import StringIO
 
18
from bzrlib import errors, filters
 
19
from bzrlib.filters import (
 
20
    ContentFilter,
 
21
    ContentFilterContext,
 
22
    filtered_input_file,
 
23
    filtered_output_bytes,
 
24
    _get_filter_stack_for,
 
25
    _get_registered_names,
 
26
    internal_size_sha_file_byname,
 
27
    register_filter_stack_map,
 
28
    )
 
29
from bzrlib.osutils import sha_string
 
30
from bzrlib.tests import TestCase, TestCaseInTempDir
 
31
 
 
32
 
 
33
# sample filter stacks
 
34
def _swapcase(chunks, context=None):
 
35
    return [s.swapcase() for s in chunks]
 
36
def _addjunk(chunks):
 
37
    return ['junk\n'] + [s for s in chunks]
 
38
def _deljunk(chunks, context):
 
39
    return [s for s in chunks[1:]]
 
40
_stack_1 = [
 
41
    ContentFilter(_swapcase, _swapcase),
 
42
    ]
 
43
_stack_2 = [
 
44
    ContentFilter(_swapcase, _swapcase),
 
45
    ContentFilter(_addjunk, _deljunk),
 
46
    ]
 
47
 
 
48
# sample data
 
49
_sample_external = ['Hello\n', 'World\n']
 
50
_internal_1 = ['hELLO\n', 'wORLD\n']
 
51
_internal_2 = ['junk\n', 'hELLO\n', 'wORLD\n']
 
52
 
 
53
 
 
54
class TestContentFilterContext(TestCase):
 
55
 
 
56
    def test_empty_filter_context(self):
 
57
        ctx = ContentFilterContext()
 
58
        self.assertEqual(None, ctx.relpath())
 
59
 
 
60
    def test_filter_context_with_path(self):
 
61
        ctx = ContentFilterContext('foo/bar')
 
62
        self.assertEquals('foo/bar', ctx.relpath())
 
63
 
 
64
 
 
65
class TestFilteredInput(TestCase):
 
66
 
 
67
    def test_filtered_input_file(self):
 
68
        # test an empty stack returns the same result
 
69
        external = ''.join(_sample_external)
 
70
        f = StringIO.StringIO(external)
 
71
        self.assertEqual(external, filtered_input_file(f, None).read())
 
72
        # test a single item filter stack
 
73
        f = StringIO.StringIO(external)
 
74
        expected = ''.join(_internal_1)
 
75
        self.assertEqual(expected, filtered_input_file(f, _stack_1).read())
 
76
        # test a multi item filter stack
 
77
        f = StringIO.StringIO(external)
 
78
        expected = ''.join(_internal_2)
 
79
        self.assertEqual(expected, filtered_input_file(f, _stack_2).read())
 
80
 
 
81
 
 
82
class TestFilteredOutput(TestCase):
 
83
 
 
84
    def test_filtered_output_bytes(self):
 
85
        # test an empty stack returns the same result
 
86
        self.assertEqual(_sample_external, list(filtered_output_bytes(
 
87
            _sample_external, None)))
 
88
        # test a single item filter stack
 
89
        self.assertEqual(_sample_external, list(filtered_output_bytes(
 
90
            _internal_1, _stack_1)))
 
91
        # test a multi item filter stack
 
92
        self.assertEqual(_sample_external, list(filtered_output_bytes(
 
93
            _internal_2, _stack_2)))
 
94
 
 
95
 
 
96
class TestFilteredSha(TestCaseInTempDir):
 
97
 
 
98
    def test_filtered_size_sha(self):
 
99
        # check that the size and sha matches what's expected
 
100
        text = 'Foo Bar Baz\n'
 
101
        a = open('a', 'wb')
 
102
        a.write(text)
 
103
        a.close()
 
104
        post_filtered_content = ''.join(_swapcase([text], None))
 
105
        expected_len = len(post_filtered_content)
 
106
        expected_sha = sha_string(post_filtered_content)
 
107
        self.assertEqual((expected_len,expected_sha),
 
108
            internal_size_sha_file_byname('a',
 
109
            [ContentFilter(_swapcase, _swapcase)]))
 
110
 
 
111
 
 
112
class TestFilterStackMaps(TestCase):
 
113
 
 
114
    def _register_map(self, pref, stk1, stk2):
 
115
        def stk_lookup(key):
 
116
            return {'v1': stk1, 'v2': stk2}.get(key)
 
117
        register_filter_stack_map(pref, stk_lookup)
 
118
 
 
119
    def test_filter_stack_maps(self):
 
120
        # Save the current registry
 
121
        original_registry = filters._reset_registry()
 
122
        try:
 
123
            # Test registration
 
124
            a_stack = [ContentFilter('b', 'c')]
 
125
            z_stack = [ContentFilter('y', 'x'), ContentFilter('w', 'v')]
 
126
            self._register_map('foo', a_stack, z_stack)
 
127
            self.assertEqual(['foo'], _get_registered_names())
 
128
            self._register_map('bar', z_stack, a_stack)
 
129
            self.assertEqual(['bar', 'foo'], _get_registered_names())
 
130
            # Test re-registration raises an error
 
131
            self.assertRaises(errors.BzrError, self._register_map,
 
132
                'foo', [], [])
 
133
        finally:
 
134
            # Restore the real registry
 
135
            filters._reset_registry(original_registry)
 
136
 
 
137
    def test_get_filter_stack_for(self):
 
138
        # Save the current registry
 
139
        original_registry = filters._reset_registry()
 
140
        try:
 
141
            # Test filter stack lookup
 
142
            a_stack = [ContentFilter('b', 'c')]
 
143
            d_stack = [ContentFilter('d', 'D')]
 
144
            z_stack = [ContentFilter('y', 'x'), ContentFilter('w', 'v')]
 
145
            self._register_map('foo', a_stack, z_stack)
 
146
            self._register_map('bar', d_stack, z_stack)
 
147
            prefs = (('foo','v1'),)
 
148
            self.assertEqual(a_stack, _get_filter_stack_for(prefs))
 
149
            prefs = (('foo','v2'),)
 
150
            self.assertEqual(z_stack, _get_filter_stack_for(prefs))
 
151
            prefs = (('foo','v1'),('bar','v1'))
 
152
            self.assertEqual(a_stack + d_stack, _get_filter_stack_for(prefs))
 
153
            # Test an unknown preference
 
154
            prefs = (('baz','v1'),)
 
155
            self.assertEqual([], _get_filter_stack_for(prefs))
 
156
            # Test an unknown value
 
157
            prefs = (('foo','v3'),)
 
158
            self.assertEqual([], _get_filter_stack_for(prefs))
 
159
        finally:
 
160
            # Restore the real registry
 
161
            filters._reset_registry(original_registry)