[Zope3-checkins] CVS: Zope3/src/zope/tal/tests - test_htmltalparser.py:1.5.34.1 test_talgettext.py:1.4.34.1 test_talinterpreter.py:1.3.6.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Aug 20 10:00:37 EDT 2003


Update of /cvs-repository/Zope3/src/zope/tal/tests
In directory cvs.zope.org:/tmp/cvs-serv5066/tests

Modified Files:
      Tag: srichter-inlinepython-branch
	test_htmltalparser.py test_talgettext.py 
	test_talinterpreter.py 
Log Message:
This commit is a bit chaotic, since I accidently created my branch on an old 
checkout.

So the first thing this does is an update with HEAD.

The second part I got the following working:

  <p tal:script="server-python">print "hello"</p>

  Result <p>hello\n</p>\n

This fails the requirement that we eventually want this syntax:

  <script lang="server-python">
    print "hello"
  </script>

It will be fairly difficult to do this, since we have to introduce sort of 
a new namespace to handle this.

Other open issues (I try to solve today):

- How to store global versus local variables. There are two models I can
  see being practical:

  (a) Make all declared variables inside the code global. This would be 
      acceptable, since we want to keep it simple and the feature is aimed
      at the scripter, who usually does not think carefully about namespaces
      anyways.

  (b) Have a special markup for making variables global, maybe by reusing 
      the global keyword?

- How to handle indentation. I have not explored this, but plan to do this 
  today. I also do not know what will happen to multiline code at this 
  point.

- What happens if the code contains other tags? I don't know, but I think
  we can reuse the Javascript trick and place <!-- --> inside the script
  tag to avoid any issues. Of course we would need to handle this case too.

- Security. Currently I call the exec keyword directly without worrying 
  about any intrusive code. Since the TTW module has the same problem I 
  will look there for an answer. However, for the DummyEngine the plain 
  exec is enough I think.



=== Zope3/src/zope/tal/tests/test_htmltalparser.py 1.5 => 1.5.34.1 ===
--- Zope3/src/zope/tal/tests/test_htmltalparser.py:1.5	Fri Apr 25 14:07:22 2003
+++ Zope3/src/zope/tal/tests/test_htmltalparser.py	Wed Aug 20 09:00:01 2003
@@ -382,6 +382,17 @@
              ('endScope', ()),
              ])
 
+    def test_script_1(self):
+        self._run_check('<p tal:script="server-python">print "hello"</p>', [
+             ('setPosition', (1, 0)),
+             ('beginScope', {'tal:script': 'server-python'}),
+             ('startTag', ('p', [('tal:script', 'server-python', 'tal')])),
+             ('evaluateCode', ('server-python',
+                           [('rawtextOffset', ('print "hello"', 13))])),
+             ('endScope', ()),
+             rawtext('</p>'),
+             ])
+
     def test_attributes_1(self):
         self._run_check("<a href='foo' name='bar' tal:attributes="
                         "'href string:http://www.zope.org; x string:y'>"
@@ -602,6 +613,37 @@
   ('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 test_i18n_name_implicit_value(self):
         # input/test22.html
         self._run_check('''\
@@ -713,32 +755,40 @@
     def test_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 test_i18n_explicit_msgid_with_name(self):
         # input/test26.html
         self._run_check('''\


=== Zope3/src/zope/tal/tests/test_talgettext.py 1.4 => 1.4.34.1 ===
--- Zope3/src/zope/tal/tests/test_talgettext.py:1.4	Thu May  1 15:35:49 2003
+++ Zope3/src/zope/tal/tests/test_talgettext.py	Wed Aug 20 09:00:01 2003
@@ -31,10 +31,10 @@
         engine = POEngine()
         engine.file = 'foo.pt'
         for key in test_keys:
-            engine.translate(key)
+            engine.translate(key, 'domain')
 
         for key in test_keys:
-            self.failIf(key not in engine.catalog,
+            self.failIf(key not in engine.catalog['domain'],
                         "POEngine catalog does not properly store message ids"
                         )
 


=== Zope3/src/zope/tal/tests/test_talinterpreter.py 1.3 => 1.3.6.1 ===
--- Zope3/src/zope/tal/tests/test_talinterpreter.py:1.3	Tue Jul 22 08:37:31 2003
+++ Zope3/src/zope/tal/tests/test_talinterpreter.py	Wed Aug 20 09:00:01 2003
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+# -*- coding: ISO-8859-1 -*-
 ##############################################################################
 #
 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
@@ -22,9 +22,9 @@
 from zope.tal.taldefs import METALError, I18NError
 from zope.tal.htmltalparser import HTMLTALParser
 from zope.tal.talinterpreter import TALInterpreter
-from zope.tal.dummyengine import DummyEngine
+from zope.tal.dummyengine import DummyEngine, DummyTranslationService
 from zope.tal.tests import utils
-
+from zope.i18n.messageid import MessageID
 
 class TestCaseBase(unittest.TestCase):
 
@@ -59,6 +59,166 @@
         self.macro[0] = ("version", "duh")
 
 
+class ScriptTestCase(TestCaseBase):
+
+    def setUp(self):
+        self.engine = DummyEngine()
+
+    def _check(self, program, expected):
+        result = StringIO()
+        self.interpreter = TALInterpreter(program, {}, self.engine,
+                                          stream=result)
+        self.interpreter()
+        self.assertEqual(expected, result.getvalue())
+
+    def test_simple_script(self):
+        program, macros = self._compile(
+            '<p tal:script="server-python">print "hello"</p>')
+        self._check(program, '<p>hello\n</p>\n')
+
+
+class I18NCornerTestCase(TestCaseBase):
+
+    def setUp(self):
+        self.engine = DummyEngine()
+        self.engine.setLocal('foo', MessageID('FoOvAlUe', 'default'))
+        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_simple_messageid_translate(self):
+        # This test is mainly here to make sure our DummyEngine works
+        # correctly.
+        program, macros = self._compile('<span tal:content="foo"/>')
+        self._check(program, '<span>FOOVALUE</span>\n')
+
+        program, macros = self._compile('<span tal:replace="foo"/>')
+        self._check(program, 'FOOVALUE\n')
+
+    def test_replace_with_messageid_and_i18nname(self):
+        program, macros = self._compile(
+            '<div i18n:translate="" >'
+            '<span tal:replace="foo" i18n:name="foo_name"/>'
+            '</div>')
+        self._check(program, '<div>FOOVALUE</div>\n')
+
+    def test_pythonexpr_replace_with_messageid_and_i18nname(self):
+        program, macros = self._compile(
+            '<div i18n:translate="" >'
+            '<span tal:replace="python: foo" i18n:name="foo_name"/>'
+            '</div>')
+        self._check(program, '<div>FOOVALUE</div>\n')
+
+    def test_structure_replace_with_messageid_and_i18nname(self):
+        program, macros = self._compile(
+            '<div i18n:translate="" >'
+            '<span tal:replace="structure foo" i18n:name="foo_name"/>'
+            '</div>')
+        self._check(program, '<div>FOOVALUE</div>\n')
+
+    def test_complex_replace_with_messageid_and_i18nname(self):
+        program, macros = self._compile(
+            '<div i18n:translate="" >'
+            '<em i18n:name="foo_name">'
+            '<span tal:replace="foo"/>'
+            '</em>'
+            '</div>')
+        self._check(program, '<div>FOOVALUE</div>\n')
+
+    def test_content_with_messageid_and_i18nname(self):
+        program, macros = self._compile(
+            '<div i18n:translate="" >'
+            '<span tal:content="foo" i18n:name="foo_name"/>'
+            '</div>')
+        self._check(program, '<div><span>FOOVALUE</span></div>\n')
+
+    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="foo" i18n:name="foo_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, msgid, domain=None, mapping=None,
+                          context=None, target_language=None, default=None):
+                self.data.append(msgid)
+                return DummyTranslationService.translate(
+                    self,
+                    msgid, domain, 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):
@@ -105,6 +265,25 @@
         </html>''' "\n"
         self.compare(INPUT, EXPECTED)
 
+    def test_unicode_content(self):
+        INPUT = """<p tal:content="python:u'déjà-vu'">para</p>"""
+        EXPECTED = u"""<p>déjà-vu</p>""" "\n"
+        self.compare(INPUT, EXPECTED)
+
+    def test_unicode_structure(self):
+        INPUT = """<p tal:replace="structure python:u'déjà-vu'">para</p>"""
+        EXPECTED = u"""déjà-vu""" "\n"
+        self.compare(INPUT, EXPECTED)
+
+    def test_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 test_entities(self):
         INPUT = ('<img tal:define="foo nothing" '
                  'alt="&a; &#1; &#x0a; &a &#45 &; &#0a; <>" />')
@@ -124,6 +303,8 @@
     suite = unittest.makeSuite(I18NErrorsTestCase)
     suite.addTest(unittest.makeSuite(MacroErrorsTestCase))
     suite.addTest(unittest.makeSuite(OutputPresentationTestCase))
+    suite.addTest(unittest.makeSuite(ScriptTestCase))
+    suite.addTest(unittest.makeSuite(I18NCornerTestCase))
     return suite
 
 if __name__ == "__main__":




More information about the Zope3-Checkins mailing list