Attachment 'Include_Test.py'

Download

   1 """
   2     Matching tests for Include macro
   3 
   4     This module tests the matching ability of the Include regular expression  
   5     The following URL contains what should be valid examples:
   6     
   7     http://purl.net/wiki/moinmaster/HelpOnMacros/Include
   8 
   9 """
  10 
  11 """
  12   The test set is a list of dictionarys, where each dictionary contains:
  13    The test string ('test', 'test-string'),
  14    The name string ('name', 'name-string'),
  15   and these optional values: 
  16    heading, hquote, htext, level, fquote, from, tquote, to, sort, items,
  17    skipitems, titlesonly
  18   The items correspond to the named fields in the regular expression
  19 """
  20 
  21 test_sets = [  
  22 dict([('test','FooBar'), ('name','FooBar')]), 
  23 dict([('test','FooBar, '), ('name','FooBar'), ('heading',',')]),
  24 dict([('test','FooBar, , 2'), ('name','FooBar'), ('heading', ','), 
  25       ('level','2')]), 
  26 dict([('test',"FooBar, 'All about Foo Bar', 2"), ('name','FooBar'), 
  27       ('heading',','), ('hquote',"'"), ('htext',"All about Foo Bar"),
  28       ('level','2')]),
  29 dict([('test','FooBar, , from="^----$"'), ('name','FooBar'),
  30       ('heading',','), ('fquote', '"'), ('from','^----$')]),
  31 dict([('test','FooBar, , to="^----$"'), ('name','FooBar'),
  32       ('heading',','), ('tquote','"'), ('to','^----$')]),
  33 dict([('test','^FooBar/.*, , sort=descending'), ('name','^FooBar/.*'),
  34       ('heading',','), ('sort','descending')]), 
  35 dict([('test','^FooBar/.*, , items=3'), ('name','^FooBar/.*'),
  36       ('heading',','), ('items','3')]),      
  37 dict([('test','^BlogBase/.*,, to="^----$", sort=descending, items=7'),
  38       ('name','^BlogBase/.*'), ('heading',','), ('tquote','"'),
  39       ('to','^----$'), ('sort','descending'), ('items','7')]),
  40 dict([('test','^BlogBase/.*,, to="^----$", sort=descending, items=7, skipitems=7,titlesonly'),
  41       ('name','^BlogBase/.*'), ('heading',','), ('tquote','"'),
  42       ('to','^----$'), ('sort','descending'), ('items','7'),
  43       ('skipitems','7'),('titlesonly','titlesonly')]),
  44 dict([('test','^FirstnameLastname/20..-..-..,,to="^----",sort=descending,items=3'),
  45       ('name','^FirstnameLastname/20..-..-..'), ('heading',','), 
  46       ('tquote','"'),('to','^----'),('sort','descending'),('items','3')]),
  47 dict([('test','^FirstnameLastname/20..-..-..,,to="^----",sort=descending,items=4,skipitems=3,titlesonly'),
  48       ('name','^FirstnameLastname/20..-..-..'), ('heading',','), 
  49       ('tquote','"'),('to','^----'),('sort','descending'),('items','4'),
  50       ('skipitems','3'),('titlesonly','titlesonly')]),
  51 dict([('test', 'TitleTest, "Heading for Title Test", ,to="^----$"'),  ('name', 'TitleTest'),
  52       ('heading',','), ('hquote', '"'), ('htext', 'Heading for Title Test'),
  53       ('to', '^----$')])
  54 ]
  55 
  56 import re
  57 _arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
  58 _arg_level = r',\s*(?P<level>\d+)'
  59 _arg_from = r'(,\s*from=(?P<fquote>[\'"])(?P<from>.+?)(?P=fquote))?'
  60 _arg_to = r'(,\s*to=(?P<tquote>[\'"])(?P<to>.+?)(?P=tquote))?'
  61 _arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'
  62 _arg_items = r'(,\s*items=(?P<items>\d+))?'
  63 _arg_skipitems = r'(,\s*skipitems=(?P<skipitems>\d+))?'
  64 _arg_titlesonly = r'(,\s*(?P<titlesonly>titlesonly))?'
  65 _args_re_pattern = r'^(?P<name>[^,]+)(%s(%s)?%s%s%s%s%s%s)?$' % (
  66     _arg_heading, _arg_level, _arg_from, _arg_to, _arg_sort, _arg_items,
  67     _arg_skipitems, _arg_titlesonly)
  68 
  69 def test_import_re(args_re=re.compile(_args_re_pattern)):
  70   args_re=re.compile(_args_re_pattern)
  71   test_num=0
  72   number_failed=0 
  73   for test_set in test_sets:
  74     test_failed=0
  75     test_str = test_set['test']
  76     test_num=test_num+1
  77     print "Test", test_num, ":", test_str
  78     
  79     args = args_re.match(test_str)
  80     if not args:
  81       print "  FAIL: failed to match on test string"
  82       test_failed=1
  83     else:      
  84       for k, v in test_set.iteritems():
  85         if k != 'test':
  86           re_res = args.group(k)
  87           if not re_res:
  88             print "  FAIL: failed to match on key '", k, "'"
  89             test_failed=1
  90           elif v != re_res:
  91             print "  FAIL: On key '", k, "', value was '", re_res, "', expected '", v, "'."
  92             test_failed=1
  93     number_failed += test_failed
  94   
  95   if number_failed == 0:
  96     print "All tests passed!"
  97   else:
  98     print str(number_failed), "out of", str(test_num), "tests failed."
  99 
 100 test_import_re()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-08-24 03:42:51, 4.2 KB) [[attachment:Include_Test.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.