Annotation of kupuMPIWG/plone/tests/test_html2captioned.py, revision 1.1.1.1

1.1       dwinter     1: import os, sys
                      2: if __name__ == '__main__':
                      3:     execfile(os.path.join(sys.path[0], 'framework.py'))
                      4: 
                      5: from unittest  import TestCase, TestSuite, main, makeSuite
                      6: from Products.CMFPlone.tests import PloneTestCase
                      7: from os.path import join, abspath, dirname
                      8: 
                      9: #try:
                     10: #    import Zope # Sigh, make product initialization happen
                     11: #    HAS_ZOPE = 1
                     12: #    Zope.startup()
                     13: #except ImportError:
                     14: #    HAS_ZOPE = 0
                     15: #except AttributeError: # Zope > 2.6
                     16: #    pass
                     17: 
                     18: from Products.PortalTransforms.tests.test_transforms import *
                     19: from Products.PortalTransforms.tests.utils import normalize_html
                     20: 
                     21: PREFIX = abspath(dirname(__file__))
                     22: 
                     23: def input_file_path(file):
                     24:     return join(PREFIX, 'input', file)
                     25: 
                     26: def output_file_path(file):
                     27:     return join(PREFIX, 'output', file)
                     28: 
                     29: tests =(
                     30: ('Products.kupu.plone.html2captioned', "minimal.in", "minimal.out", normalize_html, 0),
                     31: ('Products.kupu.plone.html2captioned', "simple.in", "simple.out", normalize_html, 0),
                     32: ('Products.kupu.plone.html2captioned', "baduid.in", "baduid.out", normalize_html, 0),
                     33: ('Products.kupu.plone.html2captioned', "notquoted.in", "notquoted.out", normalize_html, 0),
                     34: ('Products.kupu.plone.html2captioned', "notcaptioned.in", "notcaptioned.out", normalize_html, 0),
                     35: ('Products.kupu.plone.html2captioned', "linked.in", "linked.out", normalize_html, 0),
                     36:     )
                     37: 
                     38: class MockImage:
                     39:     def __init__(self, uid, description):
                     40:         self.uid, self.description = uid, description
                     41:     def Description(self):
                     42:         return self.description
                     43:     def absolute_url(self):
                     44:         return '[url for %s]' % self.uid
                     45: 
                     46: class MockCatalogTool:
                     47:     def lookupObject(self, uid):
                     48:         dummydata = {
                     49:             '104ede98d4c7c8eaeaa3b984f7395979': 'Test image caption'
                     50:         }
                     51:         if uid not in dummydata:
                     52:             return None
                     53:         return MockImage(uid, dummydata[uid])
                     54: 
                     55: class MockArchetypeTool:
                     56:     reference_catalog = MockCatalogTool()
                     57: 
                     58: class MockPortal:
                     59:     # Mock portal class: just enough to let me think I can lookup a
                     60:     # Description for an image from its UID.
                     61:     archetype_tool = MockArchetypeTool()
                     62: 
                     63: class TransformTest(TestCase):
                     64:     portal = MockPortal()
                     65:     
                     66:     def do_convert(self, filename=None):
                     67:         if filename is None and exists(self.output + '.nofilename'):
                     68:             output = self.output + '.nofilename'
                     69:         else:
                     70:             output = self.output
                     71:         input = open(self.input)
                     72:         orig = input.read()
                     73:         input.close()
                     74:         data = datastream(self.transform.name())
                     75:         res_data = self.transform.convert(orig, data, filename=filename, context=self.portal)
                     76:         self.assert_(idatastream.isImplementedBy(res_data))
                     77:         got = res_data.getData()
                     78:         try:
                     79:             output = open(output)
                     80:         except IOError:
                     81:             import sys
                     82:             print >>sys.stderr, 'No output file found.'
                     83:             print >>sys.stderr, 'File %s created, check it !' % self.output
                     84:             output = open(output, 'w')
                     85:             output.write(got)
                     86:             output.close()
                     87:             self.assert_(0)
                     88:         expected = output.read()
                     89:         if self.normalize is not None:
                     90:             expected = self.normalize(expected)
                     91:             got = self.normalize(got)
                     92:         output.close()
                     93: 
                     94:         self.assertEquals(got, expected,
                     95:                           '[%s]\n\n!=\n\n[%s]\n\nIN %s(%s)' % (
                     96:             got, expected, self.transform.name(), self.input))
                     97:         self.assertEquals(self.subobjects, len(res_data.getSubObjects()),
                     98:                           '%s\n\n!=\n\n%s\n\nIN %s(%s)' % (
                     99:             self.subobjects, len(res_data.getSubObjects()), self.transform.name(), self.input))
                    100: 
                    101:     def testSame(self):
                    102:         self.do_convert(filename=self.input)
                    103: 
                    104:     def testSameNoFilename(self):
                    105:         self.do_convert()
                    106: 
                    107:     def __repr__(self):
                    108:         return self.transform.name()
                    109: 
                    110: TR_NAMES = None
                    111: 
                    112: def make_tests(test_descr):
                    113:     """generate tests classes from test info
                    114: 
                    115:     return the list of generated test classes
                    116:     """
                    117:     tests = []
                    118:     for _transform, tr_input, tr_output, _normalize, _subobjects in test_descr:
                    119:         # load transform if necessary
                    120:         if type(_transform) is type(''):
                    121:             try:
                    122:                 _transform = load(_transform).register()
                    123:             except:
                    124:                 import traceback
                    125:                 traceback.print_exc()
                    126:                 continue
                    127:         #
                    128:         if TR_NAMES is not None and not _transform.name() in TR_NAMES:
                    129:             print 'skip test for', _transform.name()
                    130:             continue
                    131: 
                    132:         class TransformTestSubclass(TransformTest):
                    133:             input = input_file_path(tr_input)
                    134:             output = output_file_path(tr_output)
                    135:             transform = _transform
                    136:             normalize = lambda x, y: _normalize(y)
                    137:             subobjects = _subobjects
                    138: 
                    139:         tests.append(TransformTestSubclass)
                    140: 
                    141:     return tests
                    142: 
                    143: def test_suite():
                    144:     t = [ (_transform,
                    145:         input_file_path(tr_input),
                    146:         output_file_path(tr_output),
                    147:         _normalize,
                    148:         _subobjects)
                    149:         for _transform, tr_input, tr_output, _normalize, _subobjects in tests ]
                    150:         
                    151:     return TestSuite([makeSuite(test) for test in make_tests(t)])
                    152: 
                    153: if __name__=='__main__': 
                    154:     main(defaultTest='test_suite') 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>