Commits

anatoly techtonik  committed 1011911

Document the problem and add test with bogus re data to suite

  • Participants
  • Parent commits 08f42d4

Comments (0)

Files changed (2)

 **license**: Public Domain
 
 
+#### the problem and solution
+
+this example is pasted from real-word replacement rules of
+Roundup issue tracker:
+
+    >>> import re
+    >>> rules = [
+        # link to debian bug tracker
+        (re.compile('debian:\#(?P<id>\d+)'),
+         '<a href="http://bugs.debian.org/\g<id>">debian#\g<id></a>' ),
+
+        # link to local issue
+        (re.compile('\#(?P<id>\d+)'),
+         '<a href="issue\g<id>">#\g<id></a>' ),
+    ]
+    >>> text = "debian:#222"
+    >>> for search, replace in rules:
+    ...    text = search.sub(replace, text)
+    ...
+    >>> text
+    '<a href="http://bugs.debian.org/222">debian<a href="issue222">#222</a></a>'
+
+expected output is:
+
+    '<a href="http://bugs.debian.org/222">debian#222</a>'
+
+the solution:
+
+    >>> import wikify
+    >>> wrules = [wikify.RegexpRule(s,r) for s,r in rules]
+    >>> wikify.wikify("debian:#222", wrules)
+    '<a href="http://bugs.debian.org/222">debian#222</a>'
+
+
 #### usage
+
 1. define rules that match and process parts of text
 2. text = wikify(text, rules)
 
     *`bold preformatted text`*
 
 example - replace all wiki:something with HTML links
+
   - [x] wrap text into list with single item
   - [x] split text into three parts using regexp `wiki:\w+`
   - [x] copy 1st part (not-matched) into the resulting list

File tests/runtest.py

 
 rules = [wikify.RegexpRule(a,b) for a,b in substitutions]
 
+
+# --- rules from Roundup tracker
+# roundup/website/issues/extensions/local_replace.py
+roundup_rules =  [
+    (re.compile('debian:\#(?P<id>\d+)'),
+     '<a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=\g<id>">debian#\g<id></a>' ),
+
+    (re.compile('\#(?P<ws>\s*)(?P<id>\d+)'),
+     "<a href='issue\g<id>'>#\g<ws>\g<id></a>" ),
+
+    (re.compile('(?P<prews>\s+)revision(?P<ws>\s*)(?P<revision>\d+)'),
+     "\g<prews><a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=\g<revision>'>revision\g<ws>\g<revision></a>"),
+    (re.compile('(?P<prews>\s+)rev(?P<ws>\s*)(?P<revision>\d+)'),
+     "\g<prews><a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=\g<revision>'>rev\g<ws>\g<revision></a>"),
+    (re.compile('(?P<prews>\s+)(?P<revstr>r|r\s+)(?P<revision>\d+)'),
+     "\g<prews><a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=\g<revision>'>\g<revstr>\g<revision></a>"),
+]
+roundup_rules_wikify = [wikify.RegexpRule(a,b) for a,b in roundup_rules]
+
+roundup_in = """\
+ debian:#222
+ revision 222
+ wordthatendswithr 222
+ r222
+ r 222
+ #555
+"""
+roundup_out_bogusre = """\
+ <a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=222">debian<a href='issue222'>#222</a></a>
+ <a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=222'>revision 222</a>
+ wordthatendswithr 222
+ <a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=222'>r222</a>
+ <a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=222'>r 222</a>
+ <a href='issue555'>#555</a>
+"""
+roundup_out_wikify = """\
+ <a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=222">debian#222</a>
+ <a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=222'>revision 222</a>
+ wordthatendswithr 222
+ <a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=222'>r222</a>
+ <a href='http://svn.roundup-tracker.org/viewvc/roundup?view=rev&rev=222'>r 222</a>
+ <a href='issue555'>#555</a>
+"""
+
 # --/ configure wikification rules ---
 
 
-class TestBugsPythonOrgData(unittest.TestCase):
-    def test_replacement(self):
+class TestSuite(unittest.TestCase):
+    def test_bpo_data(self):
+        """ bugs.python.org replacement rules """
         self.maxDiff = None
         # the test file contains the text on odd lines and the expected
         # result on even ones, with comments starting with '##'
             # decode the str -- Unicode strings have a better diff
             self.assertEqual(p.decode(), expected_result.decode())
 
+    def test_native_roundup_rules(self):
+        text = roundup_in
+        for regexp, replace in roundup_rules:
+            text = regexp.sub(replace, text)
+        #print text
+        self.assertEqual(roundup_out_bogusre, text)
+
+    def test_wikify_roundup_rules(self):
+        wikified = wikify.wikify(roundup_in, roundup_rules_wikify)
+        #print wikified
+        self.assertEqual(roundup_out_wikify, wikified)
+
+
 # run the tests
 unittest.main()