6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
1 |
# Copyright (C) 2008, 2009, 2012, 2016 Canonical Ltd
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
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.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
27 |
)
|
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
28 |
from bzrlib.osutils import sha_string |
29 |
from bzrlib.tests import TestCase, TestCaseInTempDir |
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
30 |
|
31 |
||
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
32 |
# sample filter stacks
|
3368.2.48
by Ian Clatworthy
apply first round of poolie's review feedback |
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:]] |
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
39 |
_stack_1 = [ |
40 |
ContentFilter(_swapcase, _swapcase), |
|
41 |
]
|
|
42 |
_stack_2 = [ |
|
43 |
ContentFilter(_swapcase, _swapcase), |
|
44 |
ContentFilter(_addjunk, _deljunk), |
|
45 |
]
|
|
46 |
||
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
47 |
# sample data
|
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
48 |
_sample_external = ['Hello\n', 'World\n'] |
49 |
_internal_1 = ['hELLO\n', 'wORLD\n'] |
|
3368.2.6
by Ian Clatworthy
make filter API explicit wrt chunks and add context parameter |
50 |
_internal_2 = ['junk\n', 'hELLO\n', 'wORLD\n'] |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
51 |
|
52 |
||
3368.2.7
by Ian Clatworthy
add tests for filter contexts |
53 |
class TestContentFilterContext(TestCase): |
54 |
||
3368.2.9
by Ian Clatworthy
default context should only support relpath |
55 |
def test_empty_filter_context(self): |
3368.2.7
by Ian Clatworthy
add tests for filter contexts |
56 |
ctx = ContentFilterContext() |
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
57 |
self.assertEqual(None, ctx.relpath()) |
3368.2.9
by Ian Clatworthy
default context should only support relpath |
58 |
|
59 |
def test_filter_context_with_path(self): |
|
60 |
ctx = ContentFilterContext('foo/bar') |
|
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
61 |
self.assertEqual('foo/bar', ctx.relpath()) |
3368.2.7
by Ian Clatworthy
add tests for filter contexts |
62 |
|
63 |
||
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
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) |
|
3368.2.10
by Ian Clatworthy
don't support context for read filters |
70 |
self.assertEqual(external, filtered_input_file(f, None).read()) |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
71 |
# test a single item filter stack
|
72 |
f = StringIO.StringIO(external) |
|
73 |
expected = ''.join(_internal_1) |
|
3368.2.10
by Ian Clatworthy
don't support context for read filters |
74 |
self.assertEqual(expected, filtered_input_file(f, _stack_1).read()) |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
75 |
# test a multi item filter stack
|
76 |
f = StringIO.StringIO(external) |
|
77 |
expected = ''.join(_internal_2) |
|
3368.2.10
by Ian Clatworthy
don't support context for read filters |
78 |
self.assertEqual(expected, filtered_input_file(f, _stack_2).read()) |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
79 |
|
80 |
||
81 |
class TestFilteredOutput(TestCase): |
|
82 |
||
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
83 |
def test_filtered_output_bytes(self): |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
84 |
# test an empty stack returns the same result
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
85 |
self.assertEqual(_sample_external, list(filtered_output_bytes( |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
86 |
_sample_external, None))) |
87 |
# test a single item filter stack
|
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
88 |
self.assertEqual(_sample_external, list(filtered_output_bytes( |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
89 |
_internal_1, _stack_1))) |
90 |
# test a multi item filter stack
|
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
91 |
self.assertEqual(_sample_external, list(filtered_output_bytes( |
3368.2.3
by Ian Clatworthy
tests for ..input_file and ..output_lines |
92 |
_internal_2, _stack_2))) |
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
93 |
|
94 |
||
95 |
class TestFilteredSha(TestCaseInTempDir): |
|
96 |
||
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
97 |
def test_filtered_size_sha(self): |
98 |
# check that the size and sha matches what's expected
|
|
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
99 |
text = 'Foo Bar Baz\n' |
100 |
a = open('a', 'wb') |
|
101 |
a.write(text) |
|
102 |
a.close() |
|
3368.2.19
by Ian Clatworthy
first round of changes from abentley's review |
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', |
|
3368.2.8
by Ian Clatworthy
add tests for sha_file_by_name |
108 |
[ContentFilter(_swapcase, _swapcase)])) |
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
109 |
|
110 |
||
111 |
class TestFilterStackMaps(TestCase): |
|
112 |
||
4257.1.1
by Ian Clatworthy
register content filters using a callable, not a dict+callable |
113 |
def _register_map(self, pref, stk1, stk2): |
114 |
def stk_lookup(key): |
|
115 |
return {'v1': stk1, 'v2': stk2}.get(key) |
|
6498.5.3
by Jelmer Vernooij
Fix tests. |
116 |
filters.filter_stacks_registry.register(pref, stk_lookup) |
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
117 |
|
118 |
def test_filter_stack_maps(self): |
|
119 |
# Save the current registry
|
|
120 |
original_registry = filters._reset_registry() |
|
6498.5.3
by Jelmer Vernooij
Fix tests. |
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', [], []) |
|
3368.2.15
by Ian Clatworthy
add filter stack registry code and tests |
132 |
|
133 |
def test_get_filter_stack_for(self): |
|
134 |
# Save the current registry
|
|
135 |
original_registry = filters._reset_registry() |
|
6498.5.3
by Jelmer Vernooij
Fix tests. |
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)) |