[Zope3-checkins] CVS: Zope3/src/zope/pagetemplate/tests - __init__.py:1.1.2.1 batch.py:1.1.2.1 test_basictemplate.py:1.1.2.1 test_expressions.py:1.1.2.1 test_htmltests.py:1.1.2.1 test_tales.py:1.1.2.1 util.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:33:01 -0500


Update of /cvs-repository/Zope3/src/zope/pagetemplate/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/pagetemplate/tests

Added Files:
      Tag: NameGeddon-branch
	__init__.py batch.py test_basictemplate.py test_expressions.py 
	test_htmltests.py test_tales.py util.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/pagetemplate/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/pagetemplate/tests/batch.py ===
##############################################################################
#
# 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.0 (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.
# 
##############################################################################
__allow_access_to_unprotected_subobjects__={'batch': 1}
__roles__=None


class batch:
    """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


=== Added File Zope3/src/zope/pagetemplate/tests/test_basictemplate.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
import os, sys, unittest

from Zope.PageTemplate.tests import util
from zope.pagetemplate.pagetemplate import PageTemplate

class BasicTemplateTests(unittest.TestCase):

   def setUp(self):
      self.t = PageTemplate()

   def check_if_in_var(self):
      # DTML test 1: if, in, and var:
      pass # for unittest
      """
      %(comment)[ blah %(comment)]
      <html><head><title>Test of documentation templates</title></head>
      <body>
      %(if args)[
      <dl><dt>The arguments to this test program were:<p>
      <dd>
      <ul>
      %(in args)[
        <li>Argument number %(num)d was %(arg)s
      %(in args)]
      </ul></dl><p>
      %(if args)]
      %(else args)[
      No arguments were given.<p>
      %(else args)]
      And thats da trooth.
      </body></html>
      """
      tal = util.read_input('DTML1.html')
      self.t.write(tal)

      aa = util.argv(('one', 'two', 'three', 'cha', 'cha', 'cha'))
      o = self.t(content=aa)
      expect = util.read_output('DTML1a.html')

      util.check_xml(expect, o)

      aa = util.argv(())
      o = self.t(content=aa)
      expect = util.read_output('DTML1b.html')
      util.check_xml(expect, o)

   def check_batches_and_formatting(self):
      # DTML test 3: batches and formatting:
      pass # for unittest
      """
        <html><head><title>Test of documentation templates</title></head>
        <body>
        <!--#if args-->
          The arguments were:
          <!--#in args size=size end=end-->
              <!--#if previous-sequence-->
                 (<!--#var previous-sequence-start-arg-->-
                  <!--#var previous-sequence-end-arg-->)
              <!--#/if previous-sequence-->
              <!--#if sequence-start-->
                 <dl>
              <!--#/if sequence-start-->
              <dt><!--#var sequence-arg-->.</dt>
              <dd>Argument <!--#var num fmt=d--> was <!--#var arg--></dd>
              <!--#if next-sequence-->
                 (<!--#var next-sequence-start-arg-->-
                  <!--#var next-sequence-end-arg-->)
              <!--#/if next-sequence-->
          <!--#/in args-->
          </dl>
        <!--#else args-->
          No arguments were given.<p>
        <!--#/if args-->
        And I\'m 100% sure!
        </body></html>
      """
      tal = util.read_input('DTML3.html')
      self.t.write(tal)

      aa = util.argv(('one', 'two', 'three', 'four', 'five',
                      'six', 'seven', 'eight', 'nine', 'ten',
                      'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
                      'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty',
                      ))
      from Zope.PageTemplate.tests import batch
      o = self.t(content=aa, batch=batch.batch(aa.args, 5))

      expect = util.read_output('DTML3.html')
      util.check_xml(expect, o)


def test_suite():
   return unittest.makeSuite(BasicTemplateTests, 'check')

if __name__ == '__main__':
   unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/pagetemplate/tests/test_expressions.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
import os, sys, unittest

from zope.pagetemplate.engine import Engine

class Data:

    def __init__(self, **kw):
        self.__dict__.update(kw)

    def __repr__(self): return self.name


def dict(**kw):
    return kw


class ExpressionTests(unittest.TestCase):

    def testCompile(self):
        # Test expression compilation
        context = Data(
            vars = dict(
              x = Data(
                 name = 'xander',
                 y = Data(
                    name = 'yikes',
                    z = Data(name = 'zope')
                    )
                 ),
              y = Data(z = 3),
              b = 'boot',
              B = 2,
              )
            )


        engine = Engine

        expr = engine.compile('x')
        self.assertEqual(expr(context), context.vars['x'])

        expr = engine.compile('x/y')
        self.assertEqual(expr(context), context.vars['x'].y)

        expr = engine.compile('x/y/z')
        self.assertEqual(expr(context), context.vars['x'].y.z)

        expr = engine.compile('path:a|b|c/d/e')
        self.assertEqual(expr(context), 'boot')

        expr = engine.compile('string:Fred')
        self.assertEqual(expr(context), 'Fred')

        expr = engine.compile('string:A$B')
        self.assertEqual(expr(context), 'A2')

        expr = engine.compile('string:a ${x/y} b ${y/z} c')
        self.assertEqual(expr(context), 'a yikes b 3 c')

        expr = engine.compile('python: 2 + 2')
        self.assertEqual(expr(context), 4)

        expr = engine.compile('python: 2 \n+\n 2\n')
        self.assertEqual(expr(context), 4)


def test_suite():
    return unittest.makeSuite(ExpressionTests)


if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/pagetemplate/tests/test_htmltests.py ===
##############################################################################
#
# 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.0 (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.
# 
##############################################################################
import os, sys, unittest

from Zope.PageTemplate.tests import util
from zope.pagetemplate.pagetemplate import PageTemplate


class Folder:
   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 check1(self):
      laf = self.folder.laf
      laf.write(util.read_input('TeeShopLAF.html'))
      expect = util.read_output('TeeShopLAF.html')
      util.check_html(expect, laf())

   def check2(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 check3(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 checkSimpleLoop(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 checkGlobalsShadowLocals(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 checkStringExpressions(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 checkReplaceWithNothing(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 checkWithXMLHeader(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 checkNotExpression(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 checkPathNothing(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 checkPathAlt(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, 'check')

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/pagetemplate/tests/test_tales.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
import os, sys, unittest

from Zope.PageTemplate import TALES
from Zope.PageTemplate.tests import harness1


class TALESTests(unittest.TestCase):

    def testIterator0(self):
        # Test sample Iterator class
        context = harness1()
        it = TALES.Iterator('name', (), context)
        assert not it.next(), "Empty iterator"
        context._complete_()

    def testIterator1(self):
        # Test sample Iterator class
        context = harness1()
        it = TALES.Iterator('name', (1,), context)
        context._assert_('setLocal', 'name', 1)
        assert it.next() and not it.next(), "Single-element iterator"
        context._complete_()

    def testIterator2(self):
        # Test sample Iterator class
        context = harness1()
        it = TALES.Iterator('text', 'text', context)
        for c in 'text':
            context._assert_('setLocal', 'text', c)
        for c in 'text':
            assert it.next(), "Multi-element iterator"
        assert not it.next(), "Multi-element iterator"
        context._complete_()

    def testRegisterType(self):
        # Test expression type registration
        e = TALES.ExpressionEngine()
        e.registerType('simple', TALES.SimpleExpr)
        assert e.getTypes()['simple'] == TALES.SimpleExpr

    def testRegisterTypeUnique(self):
        # Test expression type registration uniqueness
        e = TALES.ExpressionEngine()
        e.registerType('simple', TALES.SimpleExpr)
        try:
            e.registerType('simple', TALES.SimpleExpr)
        except TALES.RegistrationError:
            pass
        else:
            assert 0, "Duplicate registration accepted."

    def testRegisterTypeNameConstraints(self):
        # Test constraints on expression type names
        e = TALES.ExpressionEngine()
        for name in '1A', 'A!', 'AB ':
            try:
                e.registerType(name, TALES.SimpleExpr)
            except TALES.RegistrationError:
                pass
            else:
                assert 0, 'Invalid type name "%s" accepted.' % name

    def testCompile(self):
        # Test expression compilation
        e = TALES.ExpressionEngine()
        e.registerType('simple', TALES.SimpleExpr)
        ce = e.compile('simple:x')
        assert ce(None) == ('simple', 'x'), (
            'Improperly compiled expression %s.' % `ce`)

    def testGetContext(self):
        # Test Context creation
        TALES.ExpressionEngine().getContext()
        TALES.ExpressionEngine().getContext(v=1)
        TALES.ExpressionEngine().getContext(x=1, y=2)

    def getContext(self, **kws):
        e = TALES.ExpressionEngine()
        e.registerType('simple', TALES.SimpleExpr)
        return apply(e.getContext, (), kws)

    def testContext0(self):
        # Test use of Context
        se = self.getContext().evaluate('simple:x')
        assert se == ('simple', 'x'), (
            'Improperly evaluated expression %s.' % `se`)

    def testVariables(self):
        # Test variables
        ctxt = self.getContext()
        c = ctxt.vars
        ctxt.beginScope()
        ctxt.setLocal('v1', 1)
        ctxt.setLocal('v2', 2)

        assert c['v1'] == 1, 'Variable "v1"'

        ctxt.beginScope()
        ctxt.setLocal('v1', 3)
        ctxt.setGlobal('g', 1)

        assert c['v1'] == 3, 'Inner scope'
        assert c['v2'] == 2, 'Outer scope'
        assert c['g'] == 1, 'Global'

        ctxt.endScope()

        assert c['v1'] == 1, "Uncovered local"
        assert c['g'] == 1, "Global from inner scope"

        ctxt.endScope()


def test_suite():
    return unittest.makeSuite(TALESTests)


if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/pagetemplate/tests/util.py ===
##############################################################################
#
# 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.0 (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.
# 
##############################################################################
import os, sys, re
from types import IntType

class Bruce:
    __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, IntType) 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:
    __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:
    __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 getPhysicalRoot(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()