3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
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
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
16 |
|
17 |
import StringIO |
|
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
18 |
from bzrlib import errors, filters |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
19 |
from bzrlib.filters import ( |
20 |
ContentFilter, |
|
3368.2.7
by Ian Clatworthy
add tests for filter contexts |
21 |
ContentFilterContext, |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
22 |
filtered_input_file, |
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
23 |
filtered_output_bytes, |
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
24 |
_get_filter_stack_for, |
25 |
_get_registered_names, |
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
26 |
internal_size_sha_file_byname, |
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
27 |
register_filter_stack_map, |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
28 |
)
|
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
29 |
from bzrlib.osutils import sha_string |
30 |
from bzrlib.tests import TestCase, TestCaseInTempDir |
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
31 |
|
32 |
||
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
33 |
# sample filter stacks
|
3368.2.48
by Ian Clatworthy
apply first round of poolie's review feedback |
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:]] |
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
40 |
_stack_1 = [ |
41 |
ContentFilter(_swapcase, _swapcase), |
|
42 |
]
|
|
43 |
_stack_2 = [ |
|
44 |
ContentFilter(_swapcase, _swapcase), |
|
45 |
ContentFilter(_addjunk, _deljunk), |
|
46 |
]
|
|
47 |
||
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
48 |
# sample data
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
49 |
_sample_external = ['Hello\n', 'World\n'] |
50 |
_internal_1 = ['hELLO\n', 'wORLD\n'] |
|
3368.2.6
by Ian Clatworthy
make filter API explicit wrt chunks and add context parameter |
51 |
_internal_2 = ['junk\n', 'hELLO\n', 'wORLD\n'] |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
52 |
|
53 |
||
3368.2.7
by Ian Clatworthy
add tests for filter contexts |
54 |
class TestContentFilterContext(TestCase): |
55 |
||
3368.2.9
by Ian Clatworthy
default context should only support relpath |
56 |
def test_empty_filter_context(self): |
3368.2.7
by Ian Clatworthy
add tests for filter contexts |
57 |
ctx = ContentFilterContext() |
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
58 |
self.assertEqual(None, ctx.relpath()) |
3368.2.9
by Ian Clatworthy
default context should only support relpath |
59 |
|
60 |
def test_filter_context_with_path(self): |
|
61 |
ctx = ContentFilterContext('foo/bar') |
|
62 |
self.assertEquals('foo/bar', ctx.relpath()) |
|
3368.2.7
by Ian Clatworthy
add tests for filter contexts |
63 |
|
64 |
||
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
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) |
|
3368.2.10
by Ian Clatworthy
don't support context for read filters |
71 |
self.assertEqual(external, filtered_input_file(f, None).read()) |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
72 |
# test a single item filter stack
|
73 |
f = StringIO.StringIO(external) |
|
74 |
expected = ''.join(_internal_1) |
|
3368.2.10
by Ian Clatworthy
don't support context for read filters |
75 |
self.assertEqual(expected, filtered_input_file(f, _stack_1).read()) |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
76 |
# test a multi item filter stack
|
77 |
f = StringIO.StringIO(external) |
|
78 |
expected = ''.join(_internal_2) |
|
3368.2.10
by Ian Clatworthy
don't support context for read filters |
79 |
self.assertEqual(expected, filtered_input_file(f, _stack_2).read()) |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
80 |
|
81 |
||
82 |
class TestFilteredOutput(TestCase): |
|
83 |
||
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
84 |
def test_filtered_output_bytes(self): |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
85 |
# test an empty stack returns the same result
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
86 |
self.assertEqual(_sample_external, list(filtered_output_bytes( |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
87 |
_sample_external, None))) |
88 |
# test a single item filter stack
|
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
89 |
self.assertEqual(_sample_external, list(filtered_output_bytes( |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
90 |
_internal_1, _stack_1))) |
91 |
# test a multi item filter stack
|
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
92 |
self.assertEqual(_sample_external, list(filtered_output_bytes( |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
93 |
_internal_2, _stack_2))) |
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
94 |
|
95 |
||
96 |
class TestFilteredSha(TestCaseInTempDir): |
|
97 |
||
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
98 |
def test_filtered_size_sha(self): |
99 |
# check that the size and sha matches what's expected
|
|
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
100 |
text = 'Foo Bar Baz\n' |
101 |
a = open('a', 'wb') |
|
102 |
a.write(text) |
|
103 |
a.close() |
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
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', |
|
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
109 |
[ContentFilter(_swapcase, _swapcase)])) |
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
110 |
|
111 |
||
112 |
class TestFilterStackMaps(TestCase): |
|
113 |
||
4257.1.1
by Ian Clatworthy
register content filters using a callable, not a dict+callable |
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) |
|
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
118 |
|
119 |
def test_filter_stack_maps(self): |
|
120 |
# Save the current registry
|
|
121 |
original_registry = filters._reset_registry() |
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
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) |
|
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
136 |
|
137 |
def test_get_filter_stack_for(self): |
|
138 |
# Save the current registry
|
|
139 |
original_registry = filters._reset_registry() |
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
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)) |
|
4427.1.1
by Ian Clatworthy
(igc) fix rule handling so that eol is optional |
151 |
prefs = (('foo','v1'), ('bar','v1')) |
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
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)) |
|
4423.2.1
by Ian Clatworthy
fix bug #379370 |
159 |
# Test a value of None is skipped
|
4427.1.1
by Ian Clatworthy
(igc) fix rule handling so that eol is optional |
160 |
prefs = (('foo',None), ('bar', 'v1')) |
4423.2.1
by Ian Clatworthy
fix bug #379370 |
161 |
self.assertEqual(d_stack, _get_filter_stack_for(prefs)) |
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
162 |
finally: |
163 |
# Restore the real registry
|
|
164 |
filters._reset_registry(original_registry) |