Mercurial > genshi > genshi-test
comparison genshi/filters/tests/transform.py @ 916:872726bac135 experimental-py3k
add support for python 3 to genshi.filters:
* minor changes to track encoding=None API change in core genshi modules.
* renamed genshi/filters/tests/html.py to test_html.py to avoid clashes with Python 3 top-level html module when running tests subset.
* did not rename genshi/filters/html.py.
* i18n filters:
* ugettext and friends are gone in Python 3 (and only gettext and friends exist and they now handle unicode)
* Some \ line continuations inside doctests confused 2to3 and so were removed them.
* Testing picked up a problem (already present in trunk) where Translator.__call__ could end up defining gettext as an endlessly recursive function. Noted with a TODO.
author | hodgestar |
---|---|
date | Sun, 24 Oct 2010 22:21:28 +0000 |
parents | 24733a5854d9 |
children |
comparison
equal
deleted
inserted
replaced
915:9fafb35032a1 | 916:872726bac135 |
---|---|
46 return list(_generate()) | 46 return list(_generate()) |
47 | 47 |
48 | 48 |
49 def _transform(html, transformer, with_attrs=False): | 49 def _transform(html, transformer, with_attrs=False): |
50 """Apply transformation returning simplified marked stream.""" | 50 """Apply transformation returning simplified marked stream.""" |
51 if isinstance(html, basestring): | 51 if isinstance(html, basestring) and not isinstance(html, unicode): |
52 html = HTML(html) | 52 html = HTML(html, encoding='utf-8') |
53 elif isinstance(html, unicode): | |
54 html = HTML(html, encoding='utf-8') | |
53 stream = transformer(html, keep_marks=True) | 55 stream = transformer(html, keep_marks=True) |
54 return _simplify(stream, with_attrs) | 56 return _simplify(stream, with_attrs) |
55 | 57 |
56 | 58 |
57 class SelectTest(unittest.TestCase): | 59 class SelectTest(unittest.TestCase): |
58 """Test .select()""" | 60 """Test .select()""" |
59 def _select(self, select): | 61 def _select(self, select): |
60 html = HTML(FOOBAR) | 62 html = HTML(FOOBAR, encoding='utf-8') |
61 if isinstance(select, basestring): | 63 if isinstance(select, basestring): |
62 select = [select] | 64 select = [select] |
63 transformer = Transformer(select[0]) | 65 transformer = Transformer(select[0]) |
64 for sel in select[1:]: | 66 for sel in select[1:]: |
65 transformer = transformer.select(sel) | 67 transformer = transformer.select(sel) |
136 (None, END, u'root')] | 138 (None, END, u'root')] |
137 ) | 139 ) |
138 | 140 |
139 def test_select_text_context(self): | 141 def test_select_text_context(self): |
140 self.assertEqual( | 142 self.assertEqual( |
141 list(Transformer('.')(HTML('foo'), keep_marks=True)), | 143 list(Transformer('.')(HTML(u'foo'), keep_marks=True)), |
142 [('OUTSIDE', ('TEXT', u'foo', (None, 1, 0)))], | 144 [('OUTSIDE', ('TEXT', u'foo', (None, 1, 0)))], |
143 ) | 145 ) |
144 | 146 |
145 | 147 |
146 class InvertTest(unittest.TestCase): | 148 class InvertTest(unittest.TestCase): |
203 (None, END, u'root')] | 205 (None, END, u'root')] |
204 ) | 206 ) |
205 | 207 |
206 def test_invert_text_context(self): | 208 def test_invert_text_context(self): |
207 self.assertEqual( | 209 self.assertEqual( |
208 _simplify(Transformer('.').invert()(HTML('foo'), keep_marks=True)), | 210 _simplify(Transformer('.').invert()(HTML(u'foo'), keep_marks=True)), |
209 [(None, 'TEXT', u'foo')], | 211 [(None, 'TEXT', u'foo')], |
210 ) | 212 ) |
211 | 213 |
212 | 214 |
213 | 215 |
269 (EXIT, END, u'root')] | 271 (EXIT, END, u'root')] |
270 ) | 272 ) |
271 | 273 |
272 def test_empty_text_context(self): | 274 def test_empty_text_context(self): |
273 self.assertEqual( | 275 self.assertEqual( |
274 _simplify(Transformer('.')(HTML('foo'), keep_marks=True)), | 276 _simplify(Transformer('.')(HTML(u'foo'), keep_marks=True)), |
275 [(OUTSIDE, TEXT, u'foo')], | 277 [(OUTSIDE, TEXT, u'foo')], |
276 ) | 278 ) |
277 | 279 |
278 | 280 |
279 class RemoveTest(unittest.TestCase): | 281 class RemoveTest(unittest.TestCase): |
654 class Injector(object): | 656 class Injector(object): |
655 count = 0 | 657 count = 0 |
656 | 658 |
657 def __iter__(self): | 659 def __iter__(self): |
658 self.count += 1 | 660 self.count += 1 |
659 return iter(HTML('CONTENT %i' % self.count)) | 661 return iter(HTML(u'CONTENT %i' % self.count)) |
660 | 662 |
661 if isinstance(html, basestring): | 663 if isinstance(html, basestring) and not isinstance(html, unicode): |
664 html = HTML(html, encoding='utf-8') | |
665 else: | |
662 html = HTML(html) | 666 html = HTML(html) |
663 if content is None: | 667 if content is None: |
664 content = Injector() | 668 content = Injector() |
665 elif isinstance(content, basestring): | 669 elif isinstance(content, basestring): |
666 content = HTML(content) | 670 content = HTML(content) |