Source

wikify / tests / test_python_tracker.py

import sys
import unittest
import os.path
import re

testdir = os.path.dirname(os.path.abspath(__file__))
# add the dir where wikify.py is (i.e. one level up)
sys.path.append(os.path.dirname(testdir))

import wikify

# --- configure rules
substitutions = [
    # URLs (adapted from Roundup)
    # replacement does not escape HTML chars
    (r'''(?x)\b
         (
          (ht|f)tp(s?)://                   # protocol
          ([\w]+(:\w+)?@)?                  # username/password
          ([\w\-]+)                         # hostname
          ((\.[\w-]+)+)?                    # .domain.etc
         |                                  # ... or ...
          ([\w]+(:\w+)?@)?                  # username/password
          www\.                             # "www."
          ([\w\-]+\.)+                      # hostname
          [\w]{2,5}                         # TLD
         )
         (:[\d]{1,5})?                      # port
         (                                  # path etc.
          /(
            [\w\-$+*()@&=?/~\\#%.,;:!]*
            [\w\-$+*()@&=?/~\\#%]             # end of URL
           )
         )?
     ''', r'<a href="\0">\0</a>'),

    # email
    (r'\b[-+=%/\w\.]+@[\w\.\-]+', r'<a href="mailto:\0">\0</a>'),

    # deadbeeffeed  (hashes with exactly twelve or forty chars)
    (r'\b[a-fA-F0-9]{40}\b',
     r'<a href="http://hg.python.org/lookup/\0">\0</a>'),
    (r'\b(?P<revision>[a-fA-F0-9]{12})\b',
     r'<a href="http://hg.python.org/lookup/\0">\0</a>'),

    # r12345, r 12345, rev12345, rev. 12345, revision12345, revision 12345
    (r'\b(?P<revstr>r\.?(ev\.?(ision)?)?\s*)(?P<revision>\d{4,})',
     r'<a href="http://hg.python.org/lookup/r\4">\1\4</a>'),

    # devguide
    (r'\bdevguide(/\w+(\.html)?(#[\w-]+)?)?',
     r'<a href="http://docs.python.org/\0">\0</a>'),
]


rules = [wikify.create_regexp_rule(a,b) for a,b in substitutions]

class TestBugsPythonOrgData(unittest.TestCase):
    def test_replacement(self):
        self.maxDiff = None
        # the test file contains the text on odd lines and the expected
        # result on even ones, with comments starting with '##'
        f = open(os.path.join(testdir, 'data_python_tracker.txt'))
        for text, expected_result in zip(f, f):
            if text.startswith('##') and expected_result.startswith('##'):
                continue  # skip the comments
            p = wikify.wikify(text, rules)
            # decode the str -- Unicode strings have a better diff
            self.assertEqual(p.decode(), expected_result.decode())

# run the tests
unittest.main()