[Zope-Checkins] CVS: Zope/lib/python/TAL/tests - test_talinterpreter.py:1.3.100.3
Florent Guillaume
fg@nuxeo.com
Thu, 30 Jan 2003 13:57:15 -0500
Update of /cvs-repository/Zope/lib/python/TAL/tests
In directory cvs.zope.org:/tmp/cvs-serv12227/lib/python/TAL/tests
Modified Files:
Tag: Zope-2_6-branch
test_talinterpreter.py
Log Message:
Merge from HEAD:
I18n interpolation now tries to deal with the case where there is a mix
of Unicode and non-ascii string that are incompatible (because the
encoding of the latter is unknown) by substituting a representation of
the non-ascii string.
I18n interpolation doesn't fail anymore if a i18n:name is not provided,
the ${string} in the translation is just left as is.
Collector #696: tal:replace of a non-string (a number for examlpe)
associated with a i18n:name failed to be interpolated properly.
Improved tests for i18n and added tests for interpolate().
=== Zope/lib/python/TAL/tests/test_talinterpreter.py 1.3.100.2 => 1.3.100.3 ===
--- Zope/lib/python/TAL/tests/test_talinterpreter.py:1.3.100.2 Mon Dec 23 12:28:55 2002
+++ Zope/lib/python/TAL/tests/test_talinterpreter.py Thu Jan 30 13:57:12 2003
@@ -11,6 +11,7 @@
from TAL.TALDefs import METALError
from TAL.HTMLTALParser import HTMLTALParser
from TAL.TALInterpreter import TALInterpreter
+from TAL.TALInterpreter import interpolate
from TAL.DummyEngine import DummyEngine
@@ -86,6 +87,19 @@
interp()
self.assertEqual(sio.getvalue(), EXPECTED)
+ def check_i18n_replace_number(self):
+ INPUT = """
+ <p i18n:translate="foo ${bar}">
+ <span tal:replace="python:123" i18n:name="bar">para</span>
+ </p>"""
+ EXPECTED = u"""
+ <p>FOO 123</p>""" "\n"
+ program, macros = self._compile(INPUT)
+ sio = StringIO()
+ interp = TALInterpreter(program, {}, DummyEngine(), sio, wrap=60)
+ interp()
+ self.assertEqual(sio.getvalue(), EXPECTED)
+
def check_escaping(self):
INPUT = """<p tal:content="python:chr(34)+'<>&'" />"""
EXPECTED = "<p>"<>&</p>\n"
@@ -94,11 +108,56 @@
interp = TALInterpreter(program, {}, DummyEngine(), sio, wrap=60)
interp()
self.assertEqual(sio.getvalue(), EXPECTED)
-
+
+class InterpolateTestCase(TestCaseBase):
+ def check_syntax_ok(self):
+ text = "foo ${bar_0MAN} $baz_zz bee"
+ mapping = {'bar_0MAN': 'fish', 'baz_zz': 'moo'}
+ expected = "foo fish moo bee"
+ self.assertEqual(interpolate(text, mapping), expected)
+
+ def check_syntax_bad(self):
+ text = "foo $_bar_man} $ ${baz bee"
+ mapping = {'_bar_man': 'fish', 'baz': 'moo'}
+ expected = text
+ self.assertEqual(interpolate(text, mapping), expected)
+
+ def check_missing(self):
+ text = "foo ${bar} ${baz}"
+ mapping = {'bar': 'fish'}
+ expected = "foo fish ${baz}"
+ self.assertEqual(interpolate(text, mapping), expected)
+
+ def check_redundant(self):
+ text = "foo ${bar}"
+ mapping = {'bar': 'fish', 'baz': 'moo'}
+ expected = "foo fish"
+ self.assertEqual(interpolate(text, mapping), expected)
+
+ def check_numeric(self):
+ text = "foo ${bar}"
+ mapping = {'bar': 123}
+ expected = "foo 123"
+ self.assertEqual(interpolate(text, mapping), expected)
+
+ def check_unicode(self):
+ text = u"foo ${bar}"
+ mapping = {u'bar': u'baz'}
+ expected = u"foo baz"
+ self.assertEqual(interpolate(text, mapping), expected)
+
+ def check_unicode_mixed_unknown_encoding(self):
+ # This test assumes that sys.getdefaultencoding is ascii...
+ text = u"foo ${bar}"
+ mapping = {u'bar': 'd\xe9j\xe0'}
+ expected = u"foo d\\xe9j\\xe0"
+ self.assertEqual(interpolate(text, mapping), expected)
+
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MacroErrorsTestCase, "check_"))
suite.addTest(unittest.makeSuite(OutputPresentationTestCase, "check_"))
+ suite.addTest(unittest.makeSuite(InterpolateTestCase, "check_"))
return suite