[Zope-Checkins] CVS: Zope/lib/python/TAL/tests - test_talinterpreter.py:1.6

Florent Guillaume fg@nuxeo.com
Thu, 30 Jan 2003 13:18:48 -0500


Update of /cvs-repository/Zope/lib/python/TAL/tests
In directory cvs.zope.org:/tmp/cvs-serv6732/lib/python/TAL/tests

Modified Files:
	test_talinterpreter.py 
Log Message:
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.5 => 1.6 ===
--- Zope/lib/python/TAL/tests/test_talinterpreter.py:1.5	Mon Dec 16 17:52:11 2002
+++ Zope/lib/python/TAL/tests/test_talinterpreter.py	Thu Jan 30 13:18:46 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
 
 
@@ -74,6 +75,15 @@
         EXPECTED = u"""déjà-vu""" "\n"
         self.compare(INPUT, 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"
+        self.compare(INPUT, EXPECTED)
+
     def check_entities(self):
         INPUT = ('<img tal:define="foo nothing" '
                  'alt="&a; &#1; &#x0a; &a &#45 &; &#0a; <>" />')
@@ -88,10 +98,55 @@
         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