~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_filters.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

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