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 1",1,to="^----$"'),  ('name', 'TitleTest'),
  52       ('heading',','), ('hquote', '"'), ('htext', 'Heading for Title Test 1'),
  53       ('level','1'),('to', '^----$')]),
  54 dict([('test', 'TitleTest, "Heading for Title Test 2", ,to="^----$"'),  ('name', 'TitleTest'),
  55       ('heading',','), ('hquote', '"'), ('htext', 'Heading for Title Test 2'),
  56       ('to', '^----$')])
  57 ]
  58 
  59 import re
  60 
  61 _arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
  62 OLD_arg_level = r',\s*(?P<level>\d+)'
  63 _arg_level = r',\s*(?P<level>\d+)?'
  64 _arg_from = r'(,\s*from=(?P<fquote>[\'"])(?P<from>.+?)(?P=fquote))?'
  65 _arg_to = r'(,\s*to=(?P<tquote>[\'"])(?P<to>.+?)(?P=tquote))?'
  66 _arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'
  67 _arg_items = r'(,\s*items=(?P<items>\d+))?'
  68 _arg_skipitems = r'(,\s*skipitems=(?P<skipitems>\d+))?'
  69 _arg_titlesonly = r'(,\s*(?P<titlesonly>titlesonly))?'
  70 _args_re_pattern_OLD = r'^(?P<name>[^,]+)(%s(%s)?%s%s%s%s%s%s)?$' % (
  71     _arg_heading, OLD_arg_level, _arg_from, _arg_to, _arg_sort, _arg_items,
  72     _arg_skipitems, _arg_titlesonly)
  73 _args_re_pattern = r'^(?P<name>[^,]+)(%s(%s)?%s%s%s%s%s%s)?$' % (
  74     _arg_heading, _arg_level, _arg_from, _arg_to, _arg_sort, _arg_items,
  75     _arg_skipitems, _arg_titlesonly)
  76 
  77 def test_import_re(pattern=_args_re_pattern):
  78   args_re=re.compile(pattern)
  79   test_num=0
  80   number_failed=0 
  81   for test_set in test_sets:
  82     test_failed=0
  83     test_str = test_set['test']
  84     test_num=test_num+1
  85     print "Test", test_num, ":", test_str
  86     
  87     args = args_re.match(test_str)
  88     if not args:
  89       print "  FAIL: failed to match on test string"
  90       test_failed=1
  91     else:      
  92       for k, v in test_set.iteritems():
  93         if k != 'test' and k != 'hquote':
  94           re_res = args.group(k)
  95           if not re_res:
  96             print "  FAIL: failed to match on key '", k, "'"
  97             test_failed=1
  98           elif v != re_res:
  99             print "  FAIL: On key '", k, "', value was '", re_res, "', expected '", v, "'."
 100             test_failed=1
 101     number_failed += test_failed
 102   
 103   if number_failed == 0:
 104     print "All tests passed!"
 105   else:
 106     print str(number_failed), "out of", str(test_num), "tests failed."
 107 
 108 print "*** ORIGINAL CODE ***"
 109 test_import_re(_args_re_pattern_OLD)
 110 
 111 print
 112 print "*** CHANGED CODE ***"
 113 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-28 04:43:59, 4.7 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.