Back to index

plone3  3.1.7
testDocTests.py
Go to the documentation of this file.
00001 # setup tests with all doctests found in docs/
00002 
00003 from plone.app.linkintegrity import docs
00004 from plone.app.linkintegrity.tests import layer
00005 from plone.app.linkintegrity.parser import extractLinks
00006 from Testing.ZopeTestCase import FunctionalDocFileSuite
00007 from Products.PloneTestCase import PloneTestCase
00008 from Products.Five.testbrowser import Browser
00009 from unittest import TestSuite, TestCase, makeSuite
00010 from os.path import join, split, abspath, dirname
00011 from os import walk
00012 from re import compile
00013 from sys import argv
00014 
00015 
00016 PloneTestCase.setupPloneSite()
00017 
00018 from ZPublisher.HTTPRequest import HTTPRequest
00019 set_orig = HTTPRequest.set
00020 
00021 from zope.testing import doctest
00022 OPTIONFLAGS = (doctest.REPORT_ONLY_FIRST_FAILURE |
00023                doctest.ELLIPSIS |
00024                doctest.NORMALIZE_WHITESPACE)
00025 
00026 class LinkIntegrityFunctionalTestCase(PloneTestCase.FunctionalTestCase):
00027 
00028     layer = layer.PloneLinkintegrity
00029 
00030     def afterSetUp(self):
00031         """ create some sample content to test with """
00032         # HTTPRequest's 'set' function is set to it's original implementation
00033         # at the start of each new test, since otherwise the below monkey
00034         # patch will apply to all remaining tests (and break them);  see
00035         # comment below in 'disableEventCountHelper'
00036         HTTPRequest.set = set_orig
00037 
00038     def getBrowser(self, loggedIn=False):
00039         """ instantiate and return a testbrowser for convenience """
00040         browser = Browser()
00041         if loggedIn:
00042             user = PloneTestCase.default_user
00043             pwd = PloneTestCase.default_password
00044             browser.addHeader('Authorization', 'Basic %s:%s' % (user, pwd))
00045         return browser
00046 
00047     def setStatusCode(self, key, value):
00048         from ZPublisher import HTTPResponse
00049         HTTPResponse.status_codes[key.lower()] = value
00050 
00051     def disableEventCountHelper(self):
00052         # so here's yet another monkey patch ;), but only to avoid having
00053         # to change almost all the tests after introducing the setting of
00054         # the helper value in 'folder_delete', which prevents presenting
00055         # the user with multiple confirmation forms;  this patch prevents
00056         # setting the value and is meant to disable this optimization in
00057         # some of the tests written so far, thereby not invalidating them...
00058         def set(self, key, value, set_orig=set_orig):
00059             if key == 'link_integrity_events_to_expect':
00060                 value = 0
00061             set_orig(self, key, value)
00062         HTTPRequest.set = set
00063 
00064 
00065 class LinkIntegrityTestCase(TestCase):
00066 
00067     def testHandleParserException(self):
00068         self.assertEqual(extractLinks('<foo\'d>'), ())
00069         self.assertEqual(extractLinks('<a href="http://foo.com">foo</a><bar\'d>'), ('http://foo.com',))
00070 
00071 
00072 # we check argv to enable testing of explicitely named doctests
00073 if '-t' in argv:
00074     pattern = compile('.*\.(txt|rst)$')
00075 else:
00076     pattern = compile('^test.*\.(txt|rst)$')
00077 
00078 def test_suite():
00079     suite = TestSuite([
00080         makeSuite(LinkIntegrityTestCase),
00081     ])
00082     docs_dir = abspath(dirname(docs.__file__)) + '/'
00083     for path, dirs, files in walk(docs_dir):
00084         for name in files:
00085             relative = join(path, name)[len(docs_dir):]
00086             if not '.svn' in split(path) and pattern.search(name):
00087                 suite.addTest(FunctionalDocFileSuite(relative,
00088                     optionflags=OPTIONFLAGS,
00089                     package=docs.__name__,
00090                     test_class=LinkIntegrityFunctionalTestCase))
00091     return suite
00092