Back to index

plone3  3.1.7
test_html2captioned.py
Go to the documentation of this file.
00001 import os, sys
00002 if __name__ == '__main__':
00003     execfile(os.path.join(sys.path[0], 'framework.py'))
00004 
00005 from kuputestcase import *
00006 
00007 from unittest  import TestCase, TestSuite, main, makeSuite
00008 from os.path import join, abspath, dirname
00009 
00010 from Products.PortalTransforms.tests.test_transforms import *
00011 try:
00012     from Products.PortalTransforms.z3.interfaces import IDataStream
00013 except ImportError:
00014     IDataStream = None
00015 from Products.PortalTransforms.tests.utils import normalize_html
00016 from Products.kupu import kupu_globals
00017 
00018 PREFIX = abspath(dirname(__file__))
00019 
00020 def input_file_path(file):
00021     return join(PREFIX, 'input', file)
00022 
00023 def output_file_path(file):
00024     return join(PREFIX, 'output', file)
00025 
00026 tests =(
00027 ('Products.kupu.plone.html2captioned', "minimal.in", "minimal.out", normalize_html, 0),
00028 ('Products.kupu.plone.html2captioned', "simple.in", "simple.out", normalize_html, 0),
00029 ('Products.kupu.plone.html2captioned', "baduid.in", "baduid.out", normalize_html, 0),
00030 ('Products.kupu.plone.html2captioned', "notquoted.in", "notquoted.out", normalize_html, 0),
00031 ('Products.kupu.plone.html2captioned', "notcaptioned.in", "notcaptioned.out", normalize_html, 0),
00032 ('Products.kupu.plone.html2captioned', "linked.in", "linked.out", normalize_html, 0),
00033 ('Products.kupu.plone.html2captioned', "umlauts.in", "umlauts.out", normalize_html, 0),
00034 ('Products.kupu.plone.html2captioned', "umlauts2.in", "umlauts2.out", normalize_html, 0),
00035     )
00036 
00037 class TransformTest(KupuTestCase):
00038     def do_convert(self, filename=None):
00039         if filename is None and exists(self.output + '.nofilename'):
00040             output = self.output + '.nofilename'
00041         else:
00042             output = self.output
00043         input = open(self.input)
00044         orig = input.read()
00045         input.close()
00046         data = datastream(self.transform.name())
00047         res_data = self.transform.convert(orig, data, filename=filename, context=self.portal)
00048         if IDataStream is not None:
00049             self.assert_(IDataStream.providedBy(res_data))
00050         got = res_data.getData()
00051         try:
00052             output = open(output)
00053         except IOError:
00054             import sys
00055             print >>sys.stderr, 'No output file found.'
00056             print >>sys.stderr, 'File %s created, check it !' % self.output
00057             output = open(output, 'w')
00058             output.write(got)
00059             output.close()
00060             self.assert_(0)
00061         expected = output.read()
00062         raw_got, raw_expected = got, expected
00063         if self.normalize is not None:
00064             expected = self.normalize(expected)
00065             got = self.normalize(got)
00066         output.close()
00067 
00068         self.assertEquals(got, expected,
00069                           'Got:\n[%s]\n\n!=\n\nExpected:\n[%s]\n\nIN %s(%s)' % (
00070             got, expected, self.transform.name(), self.input))
00071         self.assertEquals(self.subobjects, len(res_data.getSubObjects()),
00072                           'Subobjects:\n%s\n\n!=\n\nLength: %s\n\nIN %s(%s)' % (
00073             self.subobjects, len(res_data.getSubObjects()), self.transform.name(), self.input))
00074 
00075     def testSame(self):
00076         self.setup_content()
00077         self.do_convert(filename=self.input)
00078 
00079     def testSameNoFilename(self):
00080         self.setup_content()
00081         self.do_convert()
00082 
00083     def __repr__(self):
00084         return self.transform.name()
00085 
00086 TR_NAMES = None
00087 
00088 def make_tests(test_descr):
00089     """generate tests classes from test info
00090 
00091     return the list of generated test classes
00092     """
00093     tests = []
00094     for _transform, tr_input, tr_output, _normalize, _subobjects in test_descr:
00095         # load transform if necessary
00096         if type(_transform) is type(''):
00097             try:
00098                 _transform = load(_transform).register()
00099             except:
00100                 import traceback
00101                 traceback.print_exc()
00102                 continue
00103         #
00104         if TR_NAMES is not None and not _transform.name() in TR_NAMES:
00105             print 'skip test for', _transform.name()
00106             continue
00107 
00108         class TransformTestSubclass(TransformTest):
00109             input = input_file_path(tr_input)
00110             output = output_file_path(tr_output)
00111             transform = _transform
00112             normalize = lambda x, y: _normalize(y)
00113             subobjects = _subobjects
00114 
00115         tests.append(TransformTestSubclass)
00116 
00117     return tests
00118 
00119 def test_suite():
00120     t = [ (_transform,
00121         input_file_path(tr_input),
00122         output_file_path(tr_output),
00123         _normalize,
00124         _subobjects)
00125         for _transform, tr_input, tr_output, _normalize, _subobjects in tests ]
00126         
00127     return TestSuite([makeSuite(test) for test in make_tests(t)])
00128 
00129 if __name__=='__main__': 
00130     main(defaultTest='test_suite')