[Zope3-checkins] CVS: Zope3/src/zope/tal/tests - test_htmltalparser.py:1.8 test_talinterpreter.py:1.8

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Aug 21 11:19:30 EDT 2003


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

Modified Files:
	test_htmltalparser.py test_talinterpreter.py 
Log Message:
Final HEAD checkin of "Inline Code Support in TAL". For detailed messages 
during the development, see "srichter-inlinepython-branch". I tested the 
code with both, Python 2.2.3 and Python 2.3 and all works fine.

Here an example of what you can do:

  <script type="text/server-python">
    print "Hello World!"
  </script>

and

 <p tal:script="text/server-python">
   print "Hello World!"
 </p>

A more elaborate example would be:

<html><body>
  <script type="text/server-python">
    global x
    x = "Hello World!"
  </script>
  <b tal:content="x" />
</body></html>

This support is currently only available in "Templated Pages" after you 
activate the hook using the "Inline Code" screen.


=== Zope3/src/zope/tal/tests/test_htmltalparser.py 1.7 => 1.8 ===
--- Zope3/src/zope/tal/tests/test_htmltalparser.py:1.7	Thu Aug 14 13:23:21 2003
+++ Zope3/src/zope/tal/tests/test_htmltalparser.py	Thu Aug 21 10:19:29 2003
@@ -12,14 +12,15 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Tests for the HTMLTALParser code generator."""
+"""Tests for the HTMLTALParser code generator.
 
+$Id$
+"""
 import pprint
 import sys
 import unittest
 
-from zope.tal import htmltalparser
-from zope.tal import taldefs
+from zope.tal import htmltalparser, taldefs
 from zope.tal.tests import utils
 
 
@@ -381,6 +382,58 @@
                rawtext('</p>')])),
              ('endScope', ()),
              ])
+
+    def test_script_1(self):
+        self._run_check('<p tal:script="text/server-python">code</p>', [
+             ('setPosition', (1, 0)),
+             ('beginScope', {'tal:script': 'text/server-python'}),
+             ('startTag', ('p',
+                           [('tal:script', 'text/server-python', 'tal')])),
+             ('evaluateCode', ('text/server-python',
+                           [('rawtextOffset', ('code', 4))])),
+             ('endScope', ()),
+             rawtext('</p>'),
+             ])
+
+    def test_script_2(self):
+        self._run_check('<tal:block script="text/server-python">'
+                        'code'
+                        '</tal:block>', [
+            ('setPosition', (1, 0)),
+            ('beginScope', {'script': 'text/server-python'}),
+            ('optTag',
+             ('tal:block',
+              None,
+              'tal',
+              0,
+              [('startTag', ('tal:block',
+                             [('script', 'text/server-python', 'tal')]))],
+              [('evaluateCode',
+                ('text/server-python',
+                 [('rawtextOffset', ('code', 4))]))])),
+            ('endScope', ())
+            ])
+
+    def test_script_3(self):
+        self._run_check('<script type="text/server-python">code</script>', [
+            ('setPosition', (1, 0)),
+            ('beginScope', {}),
+            ('optTag',
+             ('script',
+              '',
+              None,
+              0,
+              [('rawtextOffset', ('<script>', 8))],
+              [('evaluateCode',
+                ('text/server-python', [('rawtextOffset', ('code', 4))]))])),
+            ('endScope', ())
+            ])
+
+    def test_script_4(self):
+        self._run_check('<script type="text/javascript">code</script>', [
+            ('rawtextOffset',
+             ('<script type="text/javascript">code</script>', 44))
+            ])
 
     def test_attributes_1(self):
         self._run_check("<a href='foo' name='bar' tal:attributes="


=== Zope3/src/zope/tal/tests/test_talinterpreter.py 1.7 => 1.8 ===
--- Zope3/src/zope/tal/tests/test_talinterpreter.py:1.7	Sun Aug 17 01:54:50 2003
+++ Zope3/src/zope/tal/tests/test_talinterpreter.py	Thu Aug 21 10:19:29 2003
@@ -12,8 +12,10 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Tests for TALInterpreter."""
+"""Tests for TALInterpreter.
 
+$Id$
+"""
 import sys
 import unittest
 
@@ -201,6 +203,79 @@
             result.getvalue())
 
 
+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(self):
+        program, macros = self._compile(
+            '<p tal:script="text/server-python">print "hello"</p>')
+        self._check(program, '<p>hello\n</p>\n')
+
+    def test_script_and_tal_block(self):
+        program, macros = self._compile(
+            '<tal:block script="text/server-python">\n'
+            '  global x\n'
+            '  x = 1\n'
+            '</tal:block>\n'
+            '<span tal:replace="x" />')
+        self._check(program, '\n1\n')
+        self.assertEqual(self.engine.codeGlobals['x'], 1)
+
+    def test_script_and_tal_block_having_inside_print(self):
+        program, macros = self._compile(
+            '<tal:block script="text/server-python">\n'
+            '  print "hello"'
+            '</tal:block>')
+        self._check(program, 'hello\n\n')
+
+    def test_script_and_omittag(self):
+        program, macros = self._compile(
+            '<p tal:omit-tag="" tal:script="text/server-python">\n'
+            '  print "hello"'
+            '</p>')
+        self._check(program, 'hello\n\n')
+
+    def test_script_and_inside_tags(self):
+        program, macros = self._compile(
+            '<p tal:omit-tag="" tal:script="text/server-python">\n'
+            '  print "<b>hello</b>"'
+            '</p>')
+        self._check(program, '<b>hello</b>\n\n')
+
+    def test_script_and_inside_tags_with_tal(self):
+        program, macros = self._compile(
+            '<p tal:omit-tag="" tal:script="text/server-python"> <!--\n'
+            '  print """<b tal:replace="string:foo">hello</b>"""\n'
+            '--></p>')
+        self._check(program, '<b tal:replace="string:foo">hello</b>\n\n')
+
+    def test_html_script(self):
+        program, macros = self._compile(
+            '<script type="text/server-python">\n'
+            '  print "Hello world!"\n'
+            '</script>')
+        self._check(program, 'Hello world!\n')
+
+    def test_html_script_and_javascript(self):
+        program, macros = self._compile(
+            '<script type="text/javascript" src="somefile.js" />\n'
+            '<script type="text/server-python">\n'
+            '  print "Hello world!"\n'
+            '</script>')
+        self._check(program,
+                    '<script type="text/javascript" src="somefile.js" />\n'
+                    'Hello world!\n')
+
+
 class I18NErrorsTestCase(TestCaseBase):
 
     def _check(self, src, msg):
@@ -285,6 +360,7 @@
     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
 




More information about the Zope3-Checkins mailing list