[Zope-Checkins] CVS: Releases/Zope/lib/python/Products/PageTemplates/tests - testExpressions.py:1.11.46.1 testTALES.py:1.8.14.1

Evan Simpson evan at 4-am.com
Fri Aug 29 15:16:08 EDT 2003


Update of /cvs-repository/Releases/Zope/lib/python/Products/PageTemplates/tests
In directory cvs.zope.org:/tmp/cvs-serv25364/lib/python/Products/PageTemplates/tests

Modified Files:
      Tag: evan-pathprefix-branch
	testExpressions.py testTALES.py 
Log Message:
Added the ability to define prefixes using tal:define="prefix name expr".
Expression types for the prefix namespace are *not* the same as normal
TALES expression types.  There are only two: 'builtin' and 'ext'.
'builtin' allows the built-in prefix types to be renamed, and 'ext'
(for 'external' and/or 'extension') allows access to additional
prefix types defined by trusted code.  An example of this is
"ext:PageTemplates.NumericConversions", defined in "ExtSample.py".

The convention I'm trying to establish is that 'ext' prefix types
use names of the form "<Product>.<Descriptive Name>".

In order to accomodate this new facility, I had to defer the compilation
step for prefixed path segments until they are evaluated.  The compiled
segment is cached, and reused if the mapping from local prefix name to
prefix type doesn't change (which it usually won't).



=== Releases/Zope/lib/python/Products/PageTemplates/tests/testExpressions.py 1.11 => 1.11.46.1 ===
--- Releases/Zope/lib/python/Products/PageTemplates/tests/testExpressions.py:1.11	Thu Sep 26 17:33:17 2002
+++ Releases/Zope/lib/python/Products/PageTemplates/tests/testExpressions.py	Fri Aug 29 14:15:37 2003
@@ -8,8 +8,12 @@
         self.e = e = Expressions.getEngine()
         self.ec = e.getContext(
             one = 1,
-            d = {'one': 1, 'b': 'b', '': 'blank', '_': 'under'},
+            d = {'one': 1, 1: 'one',
+                 'b': 'b',
+                 '': 'blank', '_': 'under',
+                 'keys': ('skeleton', 'master')},
             blank = '',
+            s = 's',
             )
 
     def tearDown(self):
@@ -51,6 +55,21 @@
         assert ec.evaluate('x | string:x') == 'x'
         assert ec.evaluate('x | string:$one') == '1'
         assert ec.evaluate('x | not:exists:x')
+
+    def testPrefixes(self):
+        '''Test prefixed path segments'''
+        ec = self.ec
+        assert ec.evaluate('d/key:one') == 1
+        assert ec.evaluate('d/item:1') == 'one'
+        assert ec.evaluate('d/key:keys/item:0') == 'skeleton'
+        assert ec.evaluate('d/var:blank') == 'blank'
+        assert ec.evaluate('d/attr:get/call:blank') == 'blank'
+        assert ec.evaluate('d/attr:get/call:s') == None
+        assert ec.evaluate('one/fmt:%0.3f') == '1.000'
+        assert ec.evaluate('d/fmt:%(one)s + %(one)s = 2') == '1 + 1 = 2'
+
+        ec.setPrefix('i', 'builtin:item')
+        assert ec.evaluate('d/key:keys/i:0') == 'skeleton'
 
 def test_suite():
     return unittest.makeSuite(ExpressionTests)


=== Releases/Zope/lib/python/Products/PageTemplates/tests/testTALES.py 1.8 => 1.8.14.1 ===
--- Releases/Zope/lib/python/Products/PageTemplates/tests/testTALES.py:1.8	Thu Feb 27 11:18:41 2003
+++ Releases/Zope/lib/python/Products/PageTemplates/tests/testTALES.py	Fri Aug 29 14:15:37 2003
@@ -1,6 +1,6 @@
 import os, sys, unittest
 
-from Products.PageTemplates import TALES
+from Products.PageTemplates import TALES, PathPrefixes
 from Products.PageTemplates.tests import harness1
 import string
 
@@ -70,6 +70,34 @@
             else:
                 assert 0, 'Invalid type name "%s" accepted.' % name
 
+    def testRegisterPrefixType(self):
+        '''Test prefix type registration'''
+        e = TALES.Engine()
+        e.registerPrefixType('builtin', PathPrefixes.PrefixExpr)
+        assert e.getPrefixTypes()['builtin'] == PathPrefixes.PrefixExpr
+
+    def testRegisterPrefixTypeUnique(self):
+        '''Test prefix type registration uniqueness'''
+        e = TALES.Engine()
+        e.registerPrefixType('builtin', PathPrefixes.PrefixExpr)
+        try:
+            e.registerPrefixType('builtin', PathPrefixes.PrefixExpr)
+        except TALES.RegistrationError:
+            pass
+        else:
+            assert 0, "Duplicate registration accepted."
+
+    def testRegisterPrefixTypeNameConstraints(self):
+        '''Test constraints on prefix type names'''
+        e = TALES.Engine()
+        for name in '1A', 'A!', 'AB ':
+            try:
+                e.registerPrefixType(name, PathPrefixes.PrefixExpr)
+            except TALES.RegistrationError:
+                pass
+            else:
+                assert 0, 'Invalid type name "%s" accepted.' % name
+
     def testCompile(self):
         '''Test expression compilation'''
         e = TALES.Engine()
@@ -78,6 +106,15 @@
         assert ce(None) == ('simple', 'x'), (
             'Improperly compiled expression %s.' % `ce`)
 
+    def testCompilePrefix(self):
+        '''Test prefix compilation'''
+        e = TALES.Engine()
+        e.registerPrefixType('builtin', PathPrefixes.PrefixExpr)
+        pname, ce = e.compilePrefix('builtin:var')
+        assert pname == 'var', 'prefix name %s != var' % ptype
+        assert ce(None) == PathPrefixes.builtins['var'], (
+            'Improperly compiled prefix %s gave %s.' % (`ce`, `ce(None)`))
+
     def testGetContext(self):
         '''Test Context creation'''
         TALES.Engine().getContext()
@@ -88,6 +125,7 @@
         e = TALES.Engine()
         e.registerType('simple', TALES.SimpleExpr)
         e.registerType('unicode', DummyUnicodeExpr)
+        e.registerPrefixType('builtin', PathPrefixes.PrefixExpr)
         return e.getContext(**kws)
 
     def testContext0(self):
@@ -125,6 +163,15 @@
         assert c['g'] == 1, "Global from inner scope"
 
         ctxt.endScope()
+
+    def testPrefixes(self):
+        '''Test Prefixes'''
+        ctxt = self.getContext()
+        ctxt.beginScope()
+        ctxt.setPrefix('i', 'builtin:item')
+
+        assert ctxt.prefixes['i'] == ('item', PathPrefixes.builtins['item']), (
+            'Prefix registration failed')
 
 def test_suite():
     return unittest.makeSuite(TALESTests)




More information about the Zope-Checkins mailing list