[Zope-Checkins] CVS: Zope/lib/python/TAL/tests -
test_htmltalparser.py:1.33.4.1 test_talinterpreter.py:1.7.2.1
Godefroid Chapelle
gotcha at swing.be
Fri Aug 15 10:50:48 EDT 2003
Update of /cvs-repository/Zope/lib/python/TAL/tests
In directory cvs.zope.org:/tmp/cvs-serv8434/tests
Modified Files:
Tag: gotcha-talz3_backport-branch
test_htmltalparser.py test_talinterpreter.py
Log Message:
backport of TAL fixes from z3
- i18n and metal interactions
- fix handling of nested translations with tal:content/replace and i18n:name
some reformatting to ease comparisons between 2.x and 3
=== Zope/lib/python/TAL/tests/test_htmltalparser.py 1.33 => 1.33.4.1 ===
--- Zope/lib/python/TAL/tests/test_htmltalparser.py:1.33 Tue Jul 22 09:28:46 2003
+++ Zope/lib/python/TAL/tests/test_htmltalparser.py Fri Aug 15 09:50:11 2003
@@ -614,6 +614,38 @@
('rawtextColumn', ('</span>\n', 0))
])
+ def test_i18n_name_with_content(self):
+ self._run_check('<div i18n:translate="">This is text for '
+ '<span i18n:translate="" tal:content="bar" i18n:name="bar_name"/>.'
+ '</div>', [
+('setPosition', (1, 0)),
+('beginScope', {'i18n:translate': ''}),
+('startTag', ('div', [('i18n:translate', '', 'i18n')])),
+('insertTranslation',
+ ('',
+ [('rawtextOffset', ('This is text for ', 17)),
+ ('setPosition', (1, 40)),
+ ('beginScope',
+ {'tal:content': 'bar', 'i18n:name': 'bar_name', 'i18n:translate': ''}),
+ ('i18nVariable',
+ ('bar_name',
+ [('startTag',
+ ('span',
+ [('i18n:translate', '', 'i18n'),
+ ('tal:content', 'bar', 'tal'),
+ ('i18n:name', 'bar_name', 'i18n')])),
+ ('insertTranslation',
+ ('',
+ [('insertText', ('$bar$', []))])),
+ ('rawtextOffset', ('</span>', 7))],
+ None)),
+ ('endScope', ()),
+ ('rawtextOffset', ('.', 1))])),
+('endScope', ()),
+('rawtextOffset', ('</div>', 6))
+ ])
+
+
def check_i18n_name_implicit_value(self):
# input/test22.html
self._run_check('''\
@@ -725,31 +757,38 @@
def check_i18n_data_with_name(self):
# input/test29.html
self._run_check('''\
-At the tone the time will be
+<div i18n:translate="">At the tone the time will be
<span i18n:data="here/currentTime"
i18n:translate="timefmt"
- i18n:name="time">2:32 pm</span>... beep!
-''', [
- ('rawtextBeginScope',
- ('At the tone the time will be\n',
- 0,
- (2, 0),
- 0,
- {'i18n:data': 'here/currentTime',
- 'i18n:name': 'time',
- 'i18n:translate': 'timefmt'})),
- ('insertTranslation',
- ('timefmt',
- [('startTag',
- ('span',
- [('i18n:data', 'here/currentTime', 'i18n'),
- ('i18n:translate', 'timefmt', 'i18n'),
- ('i18n:name', 'time', 'i18n')])),
- ('i18nVariable', ('time', [], None))],
- '$here/currentTime$')),
- ('endScope', ()),
- ('rawtextColumn', ('... beep!\n', 0))
- ])
+ i18n:name="time">2:32 pm</span>... beep!</div>
+''',
+[('setPosition', (1, 0)),
+ ('beginScope', {'i18n:translate': ''}),
+ ('startTag', ('div', [('i18n:translate', '', 'i18n')])),
+ ('insertTranslation',
+ ('',
+ [('rawtextBeginScope',
+ ('At the tone the time will be\n',
+ 0,
+ (2, 0),
+ 0,
+ {'i18n:data': 'here/currentTime',
+ 'i18n:name': 'time',
+ 'i18n:translate': 'timefmt'})),
+ ('insertTranslation',
+ ('timefmt',
+ [('startTag',
+ ('span',
+ [('i18n:data', 'here/currentTime', 'i18n'),
+ ('i18n:translate', 'timefmt', 'i18n'),
+ ('i18n:name', 'time', 'i18n')])),
+ ('i18nVariable', ('time', [], None))],
+ '$here/currentTime$')),
+ ('endScope', ()),
+ ('rawtextOffset', ('... beep!', 9))])),
+ ('endScope', ()),
+ ('rawtextColumn', ('</div>\n', 0))]
+)
def check_i18n_explicit_msgid_with_name(self):
# input/test26.html
=== Zope/lib/python/TAL/tests/test_talinterpreter.py 1.7 => 1.7.2.1 ===
--- Zope/lib/python/TAL/tests/test_talinterpreter.py:1.7 Wed Jul 30 18:16:49 2003
+++ Zope/lib/python/TAL/tests/test_talinterpreter.py Fri Aug 15 09:50:11 2003
@@ -20,11 +20,11 @@
from StringIO import StringIO
-from TAL.TALDefs import METALError
+from TAL.TALDefs import METALError, I18NError
from TAL.HTMLTALParser import HTMLTALParser
from TAL.TALInterpreter import TALInterpreter
+from TAL.DummyEngine import DummyEngine, DummyTranslationService
from TAL.TALInterpreter import interpolate
-from TAL.DummyEngine import DummyEngine
class TestCaseBase(unittest.TestCase):
@@ -60,6 +60,130 @@
self.macro[0] = ("version", "duh")
+class I18NCornerTestCase(TestCaseBase):
+
+ def setUp(self):
+ self.engine = DummyEngine()
+ self.engine.setLocal('bar', 'BaRvAlUe')
+
+ def _check(self, program, expected):
+ result = StringIO()
+ self.interpreter = TALInterpreter(program, {}, self.engine,
+ stream=result)
+ self.interpreter()
+ self.assertEqual(expected, result.getvalue())
+
+ def test_content_with_messageid_and_i18nname_and_i18ntranslate(self):
+ # Let's tell the user this is incredibly silly!
+ self.assertRaises(
+ I18NError, self._compile,
+ '<span i18n:translate="" tal:content="bar" i18n:name="bar_name"/>')
+
+ def test_content_with_plaintext_and_i18nname_and_i18ntranslate(self):
+ # Let's tell the user this is incredibly silly!
+ self.assertRaises(
+ I18NError, self._compile,
+ '<span i18n:translate="" i18n:name="color_name">green</span>')
+
+ def test_translate_static_text_as_dynamic(self):
+ program, macros = self._compile(
+ '<div i18n:translate="">This is text for '
+ '<span i18n:translate="" tal:content="bar" i18n:name="bar_name"/>.'
+ '</div>')
+ self._check(program,
+ '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>\n')
+
+ def test_translate_static_text_as_dynamic_from_bytecode(self):
+ program = [('version', '1.4'),
+ ('mode', 'html'),
+('setPosition', (1, 0)),
+('beginScope', {'i18n:translate': ''}),
+('startTag', ('div', [('i18n:translate', '', 'i18n')])),
+('insertTranslation',
+ ('',
+ [('rawtextOffset', ('This is text for ', 17)),
+ ('setPosition', (1, 40)),
+ ('beginScope',
+ {'tal:content': 'bar', 'i18n:name': 'bar_name', 'i18n:translate': ''}),
+ ('i18nVariable',
+ ('bar_name',
+ [('startTag',
+ ('span',
+ [('i18n:translate', '', 'i18n'),
+ ('tal:content', 'bar', 'tal'),
+ ('i18n:name', 'bar_name', 'i18n')])),
+ ('insertTranslation',
+ ('',
+ [('insertText', ('$bar$', []))])),
+ ('rawtextOffset', ('</span>', 7))],
+ None)),
+ ('endScope', ()),
+ ('rawtextOffset', ('.', 1))])),
+('endScope', ()),
+('rawtextOffset', ('</div>', 6))
+]
+ self._check(program,
+ '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>\n')
+
+ def test_for_correct_msgids(self):
+
+ class CollectingTranslationService(DummyTranslationService):
+ data = []
+
+ def translate(self, domain, msgid, mapping=None,
+ context=None, target_language=None, default=None):
+ self.data.append(msgid)
+ return DummyTranslationService.translate(
+ self,
+ domain, msgid, mapping, context, target_language, default)
+
+ xlatsvc = CollectingTranslationService()
+ self.engine.translationService = xlatsvc
+ result = StringIO()
+ program, macros = self._compile(
+ '<div i18n:translate="">This is text for '
+ '<span i18n:translate="" tal:content="bar" '
+ 'i18n:name="bar_name"/>.</div>')
+ self.interpreter = TALInterpreter(program, {}, self.engine,
+ stream=result)
+ self.interpreter()
+ self.assert_('BaRvAlUe' in xlatsvc.data)
+ self.assert_('This is text for ${bar_name}.' in
+ xlatsvc.data)
+ self.assertEqual(
+ '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>\n',
+ result.getvalue())
+
+
+class I18NErrorsTestCase(TestCaseBase):
+
+ def _check(self, src, msg):
+ try:
+ self._compile(src)
+ except I18NError:
+ pass
+ else:
+ self.fail(msg)
+
+ def test_id_with_replace(self):
+ self._check('<p i18n:id="foo" tal:replace="string:splat"></p>',
+ "expected i18n:id with tal:replace to be denied")
+
+ def test_missing_values(self):
+ self._check('<p i18n:attributes=""></p>',
+ "missing i18n:attributes value not caught")
+ self._check('<p i18n:data=""></p>',
+ "missing i18n:data value not caught")
+ self._check('<p i18n:id=""></p>',
+ "missing i18n:id value not caught")
+
+ def test_id_with_attributes(self):
+ self._check('''<input name="Delete"
+ tal:attributes="name string:delete_button"
+ i18n:attributes="name message-id">''',
+ "expected attribute being both part of tal:attributes" +
+ " and having a msgid in i18n:attributes to be denied")
+
class OutputPresentationTestCase(TestCaseBase):
def check_attribute_wrapping(self):
@@ -159,6 +283,7 @@
suite.addTest(unittest.makeSuite(MacroErrorsTestCase, "check_"))
suite.addTest(unittest.makeSuite(OutputPresentationTestCase, "check_"))
suite.addTest(unittest.makeSuite(InterpolateTestCase, "check_"))
+ suite.addTest(unittest.makeSuite(I18NCornerTestCase))
return suite
More information about the Zope-Checkins
mailing list