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

Source Code for Module qm.external.DocumentTemplate.DTtest

  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  """Document Template Tests 
 86  """ 
 87   
 88  __rcs_id__='$Id: DTtest.py 694 2003-04-16 02:53:50Z sc $' 
 89  __version__='$Revision: 694 $'[11:-2] 
 90   
 91  from DocumentTemplate import * 
 92  import sys 
 93   
94 -class Bruce:
95 - def __str__(self): return 'bruce'
96 - def __int__(self): return 42
97 - def __float__(self): return 42.0
98 - def keys(self): return ['bruce']*7
99 - def values(self): return [self]*7
100 - def items(self): return [('bruce',self)]*7
101 - def __len__(self): return 7
102 - def __getitem__(self,index):
103 if (type(index) is type(1) and 104 (index < 0 or index > 6)): raise IndexError, index 105 return self
106 isDocTemp=0
107 - def __getattr__(self,name):
108 if name[:1]=='_': raise AttributeError, name 109 return self
110 111 bruce=Bruce() 112
113 -class arg:
114 - def __init__(self,nn,aa): self.num, self.arg = nn, aa
115
116 -class argv:
117 - def __init__(self):
118 import sys 119 args=self.args=[] 120 for aa in sys.argv[1:]: args.append(arg(len(args)+1,aa))
121
122 - def items(self):
123 return map(lambda a: ('spam%d' % a.num, a), self.args)
124
125 - def values(self): return self.args
126
127 -def test1():
128 129 aa=argv() 130 131 ss=String( 132 """\ 133 %(comment)[ blah %(comment)] 134 <html><head><title>Test of documentation templates</title></head> 135 <body> 136 %(if args)[ 137 <dl><dt>The arguments to this test program were:<p> 138 <dd> 139 <ul> 140 %(in args)[ 141 <li>Argument number %(num)d was %(arg)s 142 %(in args)] 143 </ul></dl><p> 144 %(if args)] 145 %(else args)[ 146 No arguments were given.<p> 147 %(else args)] 148 And thats da trooth. 149 </body></html> 150 """) 151 152 print ss(aa) 153 154 print 'num inaccessible:' 155 # ss.names({'args':'args'}) 156 print ss(aa) 157 158 print 'quoted source:' 159 print str(ss) 160 161 try: 162 ss(hello=1,world=2) 163 print 'test if test failed' 164 except: pass 165 166 # Test nested templates 167 a=arg(42,'bruce') 168 a.header=HTML("<!--#var arg--> data <!--#var num fmt=%d-->:\n") 169 print String("%(header)s number: %(num)d")(a)
170
171 -def test2():
172 # test1() 173 174 aa=argv() 175 176 print HTML( 177 '''\ 178 <html><head><title>Test of documentation templates</title></head> 179 <body> 180 <!--#if values--> 181 The arguments were: 182 <!--#in 183 values--> 184 <!--#var 185 sequence-roman-->. 186 Argument <!--#var 187 num fmt=d--> was <!--#var arg--> 188 <!--#/in values--> 189 <!--#else values--> 190 No arguments were given.<p> 191 <!--#/if values--> 192 And I\'m 100% sure! 193 </body></html> 194 ''')(aa)
195
196 -def test3():
197 test2() 198 199 aa=argv() 200 201 h=HTML( 202 '''\ 203 <html><head><title>Test of documentation templates</title></head> 204 <body> 205 <!--#if args--> 206 The arguments were: 207 <!--#in args size=size end=end--> 208 <!--#if previous-sequence--> 209 (<!--#var previous-sequence-start-arg-->- 210 <!--#var previous-sequence-end-arg-->) 211 <!--#/if previous-sequence--> 212 <!--#if sequence-start--> 213 <dl> 214 <!--#/if sequence-start--> 215 <dt><!--#var sequence-arg-->.</dt> 216 <dd>Argument <!--#var num fmt=d--> was <!--#var arg--></dd> 217 <!--#if next-sequence--> 218 (<!--#var next-sequence-start-arg-->- 219 <!--#var next-sequence-end-arg-->) 220 <!--#/if next-sequence--> 221 <!--#/in args--> 222 </dl> 223 <!--#else args--> 224 No arguments were given.<p> 225 <!--#/if args--> 226 And I\'m 100% sure! 227 </body></html> 228 ''') 229 230 size,orphan=5,0 231 for end in range(20): 232 end=end+1 233 print '='*60, '\n' 234 print h(aa,size=size, orphan=orphan, end=end)
235
236 -def test3_okay(key, val):
237 print 'Testing', key 238 return 1
239
240 -def test4():
241
242 - def item(key,**kw): return (key,kw)
243 - def item2(key,**kw): return kw
244
245 - class item_class:
246 - def __init__(self,key,**kw):
247 for k in kw.keys(): self.__dict__[k]=kw[k]
248 249 items=( 250 item( 1,dealer='Bay Chevy', make='Chevrolet', model='Caprice', year=96), 251 item( 2,dealer='Bay Chevy', make='Chevrolet', model='Nova', year=96), 252 item( 4,dealer='Bay Chevy', make='Chevrolet', model='Nova', year=96), 253 item( 5,dealer='Bay Chevy', make='Chevrolet', model='Nova', year=96), 254 item( 3,dealer='Bay Chevy', make='Chevrolet', model='Corvett', year=96), 255 item( 6,dealer='Bay Chevy', make='Chevrolet', model='Lumina', year=96), 256 item( 7,dealer='Bay Chevy', make='Chevrolet', model='Lumina', year=96), 257 item( 8,dealer='Bay Chevy', make='Chevrolet', model='Lumina', year=95), 258 item( 9,dealer='Bay Chevy', make='Chevrolet', model='Corsica', year=96), 259 item(10,dealer='Bay Chevy', make='Chevrolet', model='Corsica', year=96), 260 item(11,dealer='Bay Chevy', make='Toyota', model='Camry', year=95), 261 item(12,dealer='Colman Olds', make='Olds', model='Ciera', year=96), 262 item(12,dealer='Colman Olds', make='Olds', model='Ciera', year=96), 263 item(12,dealer='Colman Olds', make='Olds', model='Ciera', year=96), 264 item(12,dealer='Colman Olds', make='Olds', model='Cutlass', year=96), 265 item(12,dealer='Colman Olds', make='Olds', model='Cutlas', year=95), 266 item(12,dealer='Colman Olds', make='Dodge', model='Shadow', year=93), 267 item(12,dealer='Colman Olds', make='Jeep', model='Cheroke', year=94), 268 item(12,dealer='Colman Olds', make='Toyota', model='Previa', year=92), 269 item(12,dealer='Colman Olds', make='Toyota', model='Celica', year=93), 270 item(12,dealer='Colman Olds', make='Toyota', model='Camry', year=93), 271 item(12,dealer='Colman Olds', make='Honda', model='Accord', year=94), 272 item(12,dealer='Colman Olds', make='Honda', model='Accord', year=92), 273 item(12,dealer='Colman Olds', make='Honda', model='Civic', year=94), 274 item(12,dealer='Colman Olds', make='Honda', model='Civix', year=93), 275 item( 1,dealer='Spam Chev', make='Chevrolet', model='Caprice', year=96), 276 item( 2,dealer='Spam Chev', make='Chevrolet', model='Nova', year=96), 277 item( 4,dealer='Spam Chev', make='Chevrolet', model='Nova', year=96), 278 item( 5,dealer='Spam Chev', make='Chevrolet', model='Nova', year=96), 279 item( 3,dealer='Spam Chev', make='Chevrolet', model='Corvett', year=96), 280 item( 6,dealer='Spam Chev', make='Chevrolet', model='Lumina', year=96), 281 item( 7,dealer='Spam Chev', make='Chevrolet', model='Lumina', year=96), 282 item( 8,dealer='Spam Chev', make='Chevrolet', model='Lumina', year=95), 283 item( 9,dealer='Spam Chev', make='Chevrolet', model='Corsica', year=96), 284 item(10,dealer='Spam Chev', make='Chevrolet', model='Corsica', year=96), 285 item(11,dealer='Spam Chevy', make='Toyota', model='Camry', year=95), 286 item(12,dealer='Spam Olds', make='Olds', model='Ciera', year=96), 287 item(12,dealer='Spam Olds', make='Olds', model='Ciera', year=96), 288 item(12,dealer='Spam Olds', make='Olds', model='Ciera', year=96), 289 item(12,dealer='Spam Olds', make='Olds', model='Cutlass', year=96), 290 item(12,dealer='Spam Olds', make='Olds', model='Cutlas', year=95), 291 item(12,dealer='Spam Olds', make='Dodge', model='Shadow', year=93), 292 item(12,dealer='Spam Olds', make='Jeep', model='Cheroke', year=94), 293 item(12,dealer='Spam Olds', make='Toyota', model='Previa', year=92), 294 item(12,dealer='Spam Olds', make='Toyota', model='Celica', year=93), 295 item(12,dealer='Spam Olds', make='Toyota', model='Camry', year=93), 296 item(12,dealer='Spam Olds', make='Honda', model='Accord', year=94), 297 item(12,dealer='Spam Olds', make='Honda', model='Accord', year=92), 298 item(12,dealer='Spam Olds', make='Honda', model='Civic', year=94), 299 item(12,dealer='Spam Olds', make='Honda', model='Civix', year=93), 300 ) 301 302 html=HTML( 303 '''\ 304 <html><head><title>Inventory by Dealer</title></head><body> 305 <dl> 306 <!--#in inventory mapping size=5 start=first_ad--> 307 <!--#if previous-sequence--> 308 <!--#in 309 previous-batches mapping--> 310 (<!--#var batch-start-var-dealer--> 311 <!--#var batch-start-var-year--> 312 <!--#var batch-start-var-make--> 313 <!--#var batch-start-var-model--> 314 - 315 <!--#var batch-end-var-dealer--> 316 <!--#var batch-end-var-year--> 317 <!--#var batch-end-var-make--> 318 <!--#var batch-end-var-model--> 319 ) 320 <!--#/in previous-batches--> 321 <!--#/if previous-sequence--> 322 <!--#if first-dealer--> 323 <dt><!--#var dealer--></dt><dd> 324 <!--#/if first-dealer--> 325 <!--#var year--> <!--#var make--> <!--#var model--> <p> 326 <!--#if last-dealer--> 327 </dd> 328 <!--#/if last-dealer--> 329 <!--#if next-sequence--> 330 <!--#in next-batches mapping--> 331 (<!--#var batch-start-var-dealer--> 332 <!--#var batch-start-var-year--> 333 <!--#var batch-start-var-make--> 334 <!--#var batch-start-var-model--> 335 - 336 <!--#var batch-end-var-dealer--> 337 <!--#var batch-end-var-year--> 338 <!--#var batch-end-var-make--> 339 <!--#var batch-end-var-model--> 340 ) 341 <!--#/in next-batches--> 342 <!--#/if next-sequence--> 343 <!--#/in inventory--> 344 </dl> 345 </body></html> 346 ''') 347 348 print html(inventory=items, first_ad=15)
349
350 -def test5():
351 html=HTML( 352 '''\ 353 <html><head><title>Affiliate Manager Affiliate Menu</title></head><body> 354 355 <CENTER> 356 <FONT SIZE="+2">Affiliate Manager Menu</FONT> 357 <p> 358 359 <!--#if affiliates--> 360 Select an affiliate to visit:<br> 361 <UL> 362 <!--#in affiliates--> 363 <LI><A HREF="<!--#var URL1-->/<!--#var ID-->/"> 364 <!--#var name--></A></LI> 365 <!--#/in affiliates--> 366 </UL> 367 368 <!--#/if affiliates--> 369 370 <p> 371 <A HREF="<!--#var URL1-->/add_affiliate_form">Add an affiliate</A> 372 373 <!--#if affiliates--> 374 * <A HREF="<!--#var URL1-->/delete_affiliates_form"> 375 Delete affiliates</A> 376 <!--#/if affiliates--> 377 378 </p> 379 </CENTER> 380 </body> 381 382 </html>''') 383 384 print html(affiliates=[], URL1='www')
385
386 -def test6():
387 - def d(**kw): return kw
388 data=(d(name='jim', age=38), 389 # d(name='kak', age=40), 390 d(name='will', age=7), 391 d(name='drew', age=4), 392 d(name='ches', age=1), 393 ) 394 html=HTML( 395 """Ages:\n 396 <!--#in data mapping--> 397 <!--#if sequence-end--> 398 --------------- 399 for variable name: 400 min: <!--#var min-name--> 401 max: <!--#var max-name--> 402 count: <!--#var count-name--> 403 total: <!--#var total-name--> 404 median: <!--#var median-name--> 405 --------------- 406 for variable age: 407 min: <!--#var min-age--> 408 max: <!--#var max-age--> 409 count: <!--#var count-age--> 410 total: <!--#var total-age--> 411 median: <!--#var median-age--> 412 mean: <!--#var mean-age--> 413 s.d. <!--#var standard-deviation-age--> 414 --------------- 415 <!--#/if sequence-end--> 416 <!--#/in data--> 417 """) 418 print html(data=data)
419
420 -def test7():
421 import DateTime 422 html=HTML(""" 423 <!--#var name capitalize spacify--> is 424 <!--#var date fmt=year-->/<!--#var date fmt=month-->/<!--#var date fmt=day--> 425 """) 426 html.names({'name':'name', 'date':'date'}) 427 print html(date=DateTime.DateTime(), 428 name='todays_date')
429
430 -def test8():
431 import DateTime 432 html=String(""" 433 %(name capitalize spacify)s is 434 %(date fmt=year)s/%(date fmt=month)s/%(date fmt=day)s 435 """) 436 print html(date=DateTime.DateTime(), 437 name='todays_date')
438
439 -def test9():
440 html=HTML( 441 """ 442 <!--#in spam--> 443 <!--#in sequence-item--> 444 <!--#var sequence-item--> 445 <!--#/in sequence-item--> 446 <!--#/in spam--> 447 """) 448 print html(spam=[[1,2,3],[4,5,6]])
449
450 -def test9a():
451 html=HTML( 452 """ 453 <!--#in spam--> 454 <!--#in sequence-item--> 455 <!--#var sequence-item--> 456 <!--#/in sequence-item--> 457 <!--#/in spam--> 458 """) 459 print html(spam=[[1,2,3],[4,5,6]])
460
461 -def test10():
462 #import Missing 463 html=HTML( 464 """ 465 <!--#var spam fmt="$%.2f bob's your uncle" null="spam%eggs!|"--> 466 """) #' 467 print html(spam=42) 468 print html(spam=None)
469 #print html(spam=Missing.Value) 470 471
472 -def test11():
473 #import Missing 474 html=HTML( 475 """ 476 <!--#var spam --> 477 html: <!--#var spam fmt=html-quote--> 478 url: <!--#var spam fmt=url-quote--> 479 multi: <!--#var spam fmt=multi-line--> 480 dollars: <!--#var spam fmt=whole-dollars--> 481 cents: <!--#var spam fmt=dollars-and-cents--> 482 dollars,: <!--#var spam fmt=dollars-with-commas--> 483 cents,: <!--#var spam fmt=dollars-and-cents-with-commas--> 484 485 """) 486 487 print html(spam=4200000) 488 print html(spam=None) 489 print html(spam='<a href="spam">\nfoo bar')
490 # print html(spam=Missing.Value) 491 492
493 -class test12ob:
494 - def __init__(self,**kw):
495 for k,v in kw.items(): self.__dict__[k]=v
496 - def puke(self):
497 raise 'Puke', 'raaalf'
498
499 -def test12():
500 - class foo:
501 - def __len__(self): return 9
502 - def __getitem__(self,i):
503 if i >= 9: raise IndexError, i 504 return test12ob(index=i, value='item %s' % i)
505 506 html=HTML( 507 """ 508 <!--#if spam--> 509 <!--#in spam--> 510 <!--#var value--> 511 <!--#var puke--> 512 <!--#/in spam--> 513 <!--#/if spam--> 514 """) 515 try: print html(spam=foo()) 516 except: return 517 raise 'DocumentTemplate bug', ( 518 'Puke error not properly propigated in test 12')
519
520 -def test13():
521 "Test automatic rendering of callable obnjects"
522 - class C:
523 x=1
524 - def y(self): return self.x*2
525 h=HTML("The h method, <!--#var x--> <!--#var y-->") 526 h2=HTML("The h2 method")
527 528 print HTML("<!--#var x-->, <!--#var y-->, <!--#var h-->")(C()) 529 print HTML( 530 """ 531 <!--#var expr="_.render(i.x)"-->, 532 <!--#var expr="_.render(i.y)"-->, 533 <!--#var expr="_.render(i.h2)"-->""")(i=C())
534
535 -def test14():
536 # test with tag
537 - class person:
538 name='Jim' 539 height_inches=73
540 541 print HTML("""<!--#with person--> 542 Hi, my name is <!--#var name--> 543 My height is <!--#var "height_inches*2.54"--> centimeters. 544 <!--#/with-->""")(person=person)
545
546 -def test15():
547 # test raise tag 548 try: 549 print HTML("""<!--#raise IndexError--> 550 The raise tag test suceeded! 551 <!--#/raise-->""")() 552 except IndexError, v: 553 print v
554
555 -def main():
556 import traceback 557 print 'Test 1', '='*60 558 try: test1() 559 except: traceback.print_exc() 560 print 'Test 2', '='*60 561 try: test2() 562 except: traceback.print_exc() 563 print 'Test 3', '='*60 564 try: test3() 565 except: traceback.print_exc() 566 print 'Test 4', '='*60 567 try: test4() 568 except: traceback.print_exc() 569 print 'Test 5', '='*60 570 try: test5() 571 except: traceback.print_exc() 572 print 'Test 6', '='*60 573 try: test6() 574 except: traceback.print_exc() 575 print 'Test 9', '='*60 576 try: test9() 577 except: traceback.print_exc() 578 print 'Test 9a', '='*60 579 try: test9a() 580 except: traceback.print_exc() 581 print 'Test 10', '='*60 582 try: test10() 583 except: traceback.print_exc() 584 print 'Test 11', '='*60 585 try: test11() 586 except: traceback.print_exc() 587 print 'Test 14', '='*60 588 try: test14() 589 except: traceback.print_exc() 590 print 'Test 15', '='*60 591 try: test15() 592 except: traceback.print_exc()
593 594 595 if __name__ == "__main__": 596 try: command=sys.argv[1] 597 except: command='main' 598 globals()[command]() 599