diff options
-rw-r--r-- | tests/batch.py | 118 | ||||
-rw-r--r-- | tests/test_htmltests.py | 132 | ||||
-rw-r--r-- | tests/testpackage/content.py | 28 | ||||
-rw-r--r-- | tests/util.py | 111 |
4 files changed, 389 insertions, 0 deletions
diff --git a/tests/batch.py b/tests/batch.py new file mode 100644 index 0000000..dea1daf --- /dev/null +++ b/tests/batch.py @@ -0,0 +1,118 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Batching support tests + +$Id$ +""" + +class batch(object): + """Create a sequence batch""" + + def __init__(self, sequence, size, start=0, end=0, + orphan=3, overlap=0): + + start=start+1 + + start,end,sz=opt(start,end,size,orphan,sequence) + + self._last=end-1 + self._first=start-1 + + self._sequence=sequence + self._size=size + self._start=start + self._end=end + self._orphan=orphan + self._overlap=overlap + + def previous_sequence(self): return self._first + + def previous_sequence_end_number(self): + start,end,spam=opt(0, self._start-1+self._overlap, + self._size, self._orphan, self._sequence) + return end + + def previous_sequence_start_number(self): + start,end,spam=opt(0, self._start-1+self._overlap, + self._size, self._orphan, self._sequence) + return start + + def previous_sequence_end_item(self): + start,end,spam=opt(0, self._start-1+self._overlap, + self._size, self._orphan, self._sequence) + return self._sequence[end-1] + + def previous_sequence_start_item(self): + start,end,spam=opt(0, self._start-1+self._overlap, + self._size, self._orphan, self._sequence) + return self._sequence[start-1] + + def next_sequence_end_number(self): + start,end,spam=opt(self._end+1-self._overlap, 0, + self._size, self._orphan, self._sequence) + return end + + def next_sequence_start_number(self): + start,end,spam=opt(self._end+1-self._overlap, 0, + self._size, self._orphan, self._sequence) + return start + + def next_sequence_end_item(self): + start,end,spam=opt(self._end+1-self._overlap, 0, + self._size, self._orphan, self._sequence) + return self._sequence[end-1] + + def next_sequence_start_item(self): + start,end,spam=opt(self._end+1-self._overlap, 0, + self._size, self._orphan, self._sequence) + return self._sequence[start-1] + + + def next_sequence(self): + try: self._sequence[self._end] + except IndexError: return 0 + else: return 1 + + def __getitem__(self, index): + if index > self._last: raise IndexError, index + return self._sequence[index+self._first] + +def opt(start,end,size,orphan,sequence): + if size < 1: + if start > 0 and end > 0 and end >= start: + size=end+1-start + else: size=7 + + if start > 0: + + try: sequence[start-1] + except: start=len(sequence) + + if end > 0: + if end < start: end=start + else: + end=start+size-1 + try: sequence[end+orphan-1] + except: end=len(sequence) + elif end > 0: + try: sequence[end-1] + except: end=len(sequence) + start=end+1-size + if start - 1 < orphan: start=1 + else: + start=1 + end=start+size-1 + try: sequence[end+orphan-1] + except: end=len(sequence) + return start,end,size diff --git a/tests/test_htmltests.py b/tests/test_htmltests.py new file mode 100644 index 0000000..ed0ea1d --- /dev/null +++ b/tests/test_htmltests.py @@ -0,0 +1,132 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Page Template HTML Tests + +$Id$ +""" +import unittest + +from zope.pagetemplate.tests import util +from zope.pagetemplate.pagetemplate import PageTemplate + + +class Folder(object): + context = property(lambda self: self) + +class HTMLTests(unittest.TestCase): + + def setUp(self): + self.folder = f = Folder() + f.laf = PageTemplate() + f.t = PageTemplate() + + def getProducts(self): + return [ + {'description': 'This is the tee for those who LOVE Zope. ' + 'Show your heart on your tee.', + 'price': 12.99, 'image': 'smlatee.jpg' + }, + {'description': 'This is the tee for Jim Fulton. ' + 'He\'s the Zope Pope!', + 'price': 11.99, 'image': 'smpztee.jpg' + }, + ] + + def test_1(self): + laf = self.folder.laf + laf.write(util.read_input('teeshoplaf.html')) + expect = util.read_output('teeshoplaf.html') + util.check_html(expect, laf()) + + def test_2(self): + self.folder.laf.write(util.read_input('teeshoplaf.html')) + + t = self.folder.t + t.write(util.read_input('teeshop2.html')) + expect = util.read_output('teeshop2.html') + out = t(laf = self.folder.laf, getProducts = self.getProducts) + util.check_html(expect, out) + + + def test_3(self): + self.folder.laf.write(util.read_input('teeshoplaf.html')) + + t = self.folder.t + t.write(util.read_input('teeshop1.html')) + expect = util.read_output('teeshop1.html') + out = t(laf = self.folder.laf, getProducts = self.getProducts) + util.check_html(expect, out) + + def test_SimpleLoop(self): + t = self.folder.t + t.write(util.read_input('loop1.html')) + expect = util.read_output('loop1.html') + out = t() + util.check_html(expect, out) + + def test_GlobalsShadowLocals(self): + t = self.folder.t + t.write(util.read_input('globalsshadowlocals.html')) + expect = util.read_output('globalsshadowlocals.html') + out = t() + util.check_html(expect, out) + + def test_StringExpressions(self): + t = self.folder.t + t.write(util.read_input('stringexpression.html')) + expect = util.read_output('stringexpression.html') + out = t() + util.check_html(expect, out) + + def test_ReplaceWithNothing(self): + t = self.folder.t + t.write(util.read_input('checknothing.html')) + expect = util.read_output('checknothing.html') + out = t() + util.check_html(expect, out) + + def test_WithXMLHeader(self): + t = self.folder.t + t.write(util.read_input('checkwithxmlheader.html')) + expect = util.read_output('checkwithxmlheader.html') + out = t() + util.check_html(expect, out) + + def test_NotExpression(self): + t = self.folder.t + t.write(util.read_input('checknotexpression.html')) + expect = util.read_output('checknotexpression.html') + out = t() + util.check_html(expect, out) + + def test_PathNothing(self): + t = self.folder.t + t.write(util.read_input('checkpathnothing.html')) + expect = util.read_output('checkpathnothing.html') + out = t() + util.check_html(expect, out) + + def test_PathAlt(self): + t = self.folder.t + t.write(util.read_input('checkpathalt.html')) + expect = util.read_output('checkpathalt.html') + out = t() + util.check_html(expect, out) + + +def test_suite(): + return unittest.makeSuite(HTMLTests) + +if __name__=='__main__': + unittest.TextTestRunner().run(test_suite()) diff --git a/tests/testpackage/content.py b/tests/testpackage/content.py new file mode 100644 index 0000000..7d51454 --- /dev/null +++ b/tests/testpackage/content.py @@ -0,0 +1,28 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Content + +$Id$ +""" + +class Content(object): + def getSomething(self): + return 42 + + +class PTComponent(object): + def __init__(self, content, request): + self.content = content + + index = PageTemplateFile("view.pt", engine_name="unrestricted") diff --git a/tests/util.py b/tests/util.py new file mode 100644 index 0000000..31fa0f0 --- /dev/null +++ b/tests/util.py @@ -0,0 +1,111 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Utilities + +$Id$ +""" +import os +import re +import sys + + +class Bruce(object): + __allow_access_to_unprotected_subobjects__=1 + def __str__(self): return 'bruce' + def __int__(self): return 42 + def __float__(self): return 42.0 + def keys(self): return ['bruce']*7 + def values(self): return [self]*7 + def items(self): return [('bruce',self)]*7 + def __len__(self): return 7 + def __getitem__(self,index): + if ininstance(index, int) and (index < 0 or index > 6): + raise IndexError, index + return self + isDocTemp = 0 + def __getattr__(self,name): + if name.startswith('_'): + raise AttributeError, name + return self + +bruce = Bruce() + +class arg(object): + __allow_access_to_unprotected_subobjects__ = 1 + def __init__(self,nn,aa): self.num, self.arg = nn, aa + def __str__(self): return str(self.arg) + +class argv(object): + __allow_access_to_unprotected_subobjects__ = 1 + + def __init__(self, argv=sys.argv[1:]): + args = self.args = [] + for aa in argv: + args.append(arg(len(args)+1,aa)) + + def items(self): + return map(lambda a: ('spam%d' % a.num, a), self.args) + + def values(self): return self.args + + def getRoot(self): + return self + + context = property(lambda self: self) + +def nicerange(lo, hi): + if hi <= lo+1: + return str(lo+1) + else: + return "%d,%d" % (lo+1, hi) + +def dump(tag, x, lo, hi): + for i in xrange(lo, hi): + print '%s %s' % (tag, x[i]), + +def check_html(s1, s2): + s1 = normalize_html(s1) + s2 = normalize_html(s2) + assert s1==s2, (s1, s2, "HTML Output Changed") + +def check_xml(s1, s2): + s1 = normalize_xml(s1) + s2 = normalize_xml(s2) + assert s1==s2, ("XML Output Changed:\n%s\n\n%s" % (s1, s2)) + +def normalize_html(s): + s = re.sub(r"[ \t]+", " ", s) + s = re.sub(r"/>", ">", s) + return s + +def normalize_xml(s): + s = re.sub(r"\s+", " ", s) + s = re.sub(r"(?s)\s+<", "<", s) + s = re.sub(r"(?s)>\s+", ">", s) + return s + + +import zope.pagetemplate.tests + +dir = os.path.dirname(zope.pagetemplate.tests.__file__) +input_dir = os.path.join(dir, 'input') +output_dir = os.path.join(dir, 'output') + +def read_input(filename): + filename = os.path.join(input_dir, filename) + return open(filename, 'r').read() + +def read_output(filename): + filename = os.path.join(output_dir, filename) + return open(filename, 'r').read() |