Package qm :: Package external :: Package DocumentTemplate :: Module gparse_test
[hide private]
[frames] | no frames]

Source Code for Module qm.external.DocumentTemplate.gparse_test

  1  ############################################################################## 
  2  #  
  3  # Zope Public License (ZPL) Version 1.0 
  4  # ------------------------------------- 
  5  #  
  6  # Copyright (c) Digital Creations.  All rights reserved. 
  7  #  
  8  # This license has been certified as Open Source(tm). 
  9  #  
 10  # Redistribution and use in source and binary forms, with or without 
 11  # modification, are permitted provided that the following conditions are 
 12  # met: 
 13  #  
 14  # 1. Redistributions in source code must retain the above copyright 
 15  #    notice, this list of conditions, and the following disclaimer. 
 16  #  
 17  # 2. Redistributions in binary form must reproduce the above copyright 
 18  #    notice, this list of conditions, and the following disclaimer in 
 19  #    the documentation and/or other materials provided with the 
 20  #    distribution. 
 21  #  
 22  # 3. Digital Creations requests that attribution be given to Zope 
 23  #    in any manner possible. Zope includes a "Powered by Zope" 
 24  #    button that is installed by default. While it is not a license 
 25  #    violation to remove this button, it is requested that the 
 26  #    attribution remain. A significant investment has been put 
 27  #    into Zope, and this effort will continue if the Zope community 
 28  #    continues to grow. This is one way to assure that growth. 
 29  #  
 30  # 4. All advertising materials and documentation mentioning 
 31  #    features derived from or use of this software must display 
 32  #    the following acknowledgement: 
 33  #  
 34  #      "This product includes software developed by Digital Creations 
 35  #      for use in the Z Object Publishing Environment 
 36  #      (http://www.zope.org/)." 
 37  #  
 38  #    In the event that the product being advertised includes an 
 39  #    intact Zope distribution (with copyright and license included) 
 40  #    then this clause is waived. 
 41  #  
 42  # 5. Names associated with Zope or Digital Creations must not be used to 
 43  #    endorse or promote products derived from this software without 
 44  #    prior written permission from Digital Creations. 
 45  #  
 46  # 6. Modified redistributions of any form whatsoever must retain 
 47  #    the following acknowledgment: 
 48  #  
 49  #      "This product includes software developed by Digital Creations 
 50  #      for use in the Z Object Publishing Environment 
 51  #      (http://www.zope.org/)." 
 52  #  
 53  #    Intact (re-)distributions of any official Zope release do not 
 54  #    require an external acknowledgement. 
 55  #  
 56  # 7. Modifications are encouraged but must be packaged separately as 
 57  #    patches to official Zope releases.  Distributions that do not 
 58  #    clearly separate the patches from the original work must be clearly 
 59  #    labeled as unofficial distributions.  Modifications which do not 
 60  #    carry the name Zope may be packaged in any form, as long as they 
 61  #    conform to all of the clauses above. 
 62  #  
 63  #  
 64  # Disclaimer 
 65  #  
 66  #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY 
 67  #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 68  #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 69  #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS 
 70  #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 71  #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 72  #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
 73  #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 74  #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 75  #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 76  #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 77  #   SUCH DAMAGE. 
 78  #  
 79  #  
 80  # This software consists of contributions made by Digital Creations and 
 81  # many individuals on behalf of Digital Creations.  Specific 
 82  # attributions are listed in the accompanying credits file. 
 83  #  
 84  ############################################################################## 
 85  "$Id: gparse_test.py 694 2003-04-16 02:53:50Z sc $" 
 86  import sys, parser, symbol, token 
 87  from symbol import * 
 88  from token import * 
 89  from parser import sequence2ast, compileast, ast2list 
 90  from gparse import * 
 91   
92 -def pretty(s):
93 l=ast2list(parser.expr(s)) 94 print l 95 pret(l)
96
97 -def astl(s): return parser.ast2list(parser.expr(s))
98
99 -def pret(ast, level=0):
100 if ISTERMINAL(ast[0]): print ' '*level, ast[1] 101 else: 102 print ' '*level, sym_name[ast[0]], '(%s)' % ast[0] 103 for a in ast[1:]: 104 pret(a,level+1)
105
106 -def tpretty():
107 print 60*'=' 108 for arg in sys.argv[1:]: 109 print 110 print arg 111 pretty(arg) 112 print 60*'='
113
114 -def check(expr1=None, expr2=None):
115 ok=1 116 expr1=expr1 or sys.argv[1] 117 expr2=expr2 or sys.argv[2] 118 l1=munge(astl(expr1)) 119 l2=astl(expr2) 120 try: c1=compileast(sequence2ast(l1)) 121 except: 122 traceback.print_exc 123 c1=None 124 c2=compileast(sequence2ast(l2)) 125 if c1 !=c2: 126 ok=0 127 print 'test failed', expr1, expr2 128 print 129 print l1 130 print 131 print l2 132 print 133 134 ast=parser.sequence2ast(l1) 135 c=parser.compileast(ast) 136 137 pretty(expr1) 138 pret(l1) 139 pret(l2) 140 141 return ok
142
143 -def spam():
144 # Regression test 145 import traceback 146 ok=1 147 for expr1, expr2 in ( 148 ("a*b", "__guarded_mul__(_vars, a, b)"), 149 ("a*b*c", 150 "__guarded_mul__(_vars, __guarded_mul__(_vars, a, b), c)" 151 ), 152 ("a.b", "__guarded_getattr__(_vars, a, 'b')"), 153 ("a[b]", "__guarded_getitem__(_vars, a, b)"), 154 ("a[b,c]", "__guarded_getitem__(_vars, a, b, c)"), 155 ("a[b:c]", "__guarded_getslice__(_vars, a, b, c)"), 156 ("a[:c]", "__guarded_getslice__(_vars, a, 0, c)"), 157 ("a[b:]", "__guarded_getslice__(_vars, a, b)"), 158 ("a[:]", "__guarded_getslice__(_vars, a)"), 159 ("_vars['sequence-index'] % 2", 160 "__guarded_getitem__(_vars, _vars, 'sequence-index') % 2" 161 ), 162 ): 163 l1=munge(astl(expr1)) 164 l2=astl(expr2) 165 try: c1=compileast(sequence2ast(l1)) 166 except: 167 traceback.print_exc 168 c1=None 169 c2=compileast(sequence2ast(l2)) 170 if c1 !=c2: 171 ok=0 172 print 'test failed', expr1, expr2 173 print 174 print l1 175 print 176 print l2 177 print 178 179 ast=parser.sequence2ast(l1) 180 c=parser.compileast(ast) 181 182 if ok: print 'all tests succeeded'
183 184 if __name__=='__main__': 185 try: 186 c=sys.argv[1] 187 del sys.argv[1] 188 globals()[c]() 189 except: spam() 190