/usr/share/pyshared/zope/tales/tests/test_tales.py is in python-zope.tales 3.5.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | ##############################################################################
#
# 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.
#
##############################################################################
"""TALES Tests
$Id: test_tales.py 110997 2010-04-16 19:19:24Z tseaver $
"""
import unittest
import re
from zope.tales import tales
from zope.tales.tests.simpleexpr import SimpleExpr
from doctest import DocTestSuite
from zope.testing import renormalizing
class TALESTests(unittest.TestCase):
def testIterator0(self):
# Test sample Iterator class
context = Harness(self)
it = tales.Iterator('name', (), context)
self.assert_( not it.next(), "Empty iterator")
context._complete_()
def testIterator1(self):
# Test sample Iterator class
context = Harness(self)
it = tales.Iterator('name', (1,), context)
context._assert_('setLocal', 'name', 1)
self.assert_( it.next() and not it.next(), "Single-element iterator")
context._complete_()
def testIterator2(self):
# Test sample Iterator class
context = Harness(self)
it = tales.Iterator('text', 'text', context)
for c in 'text':
context._assert_('setLocal', 'text', c)
for c in 'text':
self.assert_(it.next(), "Multi-element iterator")
self.assert_( not it.next(), "Multi-element iterator")
context._complete_()
def testRegisterType(self):
# Test expression type registration
e = tales.ExpressionEngine()
e.registerType('simple', SimpleExpr)
self.assert_( e.getTypes()['simple'] == SimpleExpr)
def testRegisterTypeUnique(self):
# Test expression type registration uniqueness
e = tales.ExpressionEngine()
e.registerType('simple', SimpleExpr)
try:
e.registerType('simple', SimpleExpr)
except tales.RegistrationError:
pass
else:
self.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, SimpleExpr)
except tales.RegistrationError:
pass
else:
self.assert_( 0, 'Invalid type name "%s" accepted.' % name)
def testCompile(self):
# Test expression compilation
e = tales.ExpressionEngine()
e.registerType('simple', SimpleExpr)
ce = e.compile('simple:x')
self.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', SimpleExpr)
return apply(e.getContext, (), kws)
def testContext0(self):
# Test use of Context
se = self.getContext().evaluate('simple:x')
self.assert_( se == ('simple', 'x'), (
'Improperly evaluated expression %s.' % `se`))
def testVariables(self):
# Test variables
ctxt = self.getContext()
ctxt.beginScope()
ctxt.setLocal('v1', 1)
ctxt.setLocal('v2', 2)
c = ctxt.vars
self.assert_( c['v1'] == 1, 'Variable "v1"')
ctxt.beginScope()
ctxt.setLocal('v1', 3)
ctxt.setGlobal('g', 1)
c = ctxt.vars
self.assert_( c['v1'] == 3, 'Inner scope')
self.assert_( c['v2'] == 2, 'Outer scope')
self.assert_( c['g'] == 1, 'Global')
ctxt.endScope()
c = ctxt.vars
self.assert_( c['v1'] == 1, "Uncovered local")
self.assert_( c['g'] == 1, "Global from inner scope")
ctxt.endScope()
class Harness(object):
def __init__(self, testcase):
self._callstack = []
self._testcase = testcase
def _assert_(self, name, *args, **kwargs):
self._callstack.append((name, args, kwargs))
def _complete_(self):
self._testcase.assert_(len(self._callstack) == 0,
"Harness methods called")
def __getattr__(self, name):
return HarnessMethod(self, name)
class HarnessMethod(object):
def __init__(self, harness, name):
self._harness = harness
self._name = name
def __call__(self, *args, **kwargs):
name = self._name
self = self._harness
cs = self._callstack
self._testcase.assert_(
len(cs),
'Unexpected harness method call "%s".' % name
)
self._testcase.assert_(
cs[0][0] == name,
'Harness method name "%s" called, "%s" expected.' %
(name, cs[0][0])
)
name, aargs, akwargs = self._callstack.pop(0)
self._testcase.assert_(aargs == args, "Harness method arguments")
self._testcase.assert_(akwargs == kwargs,
"Harness method keyword args")
def test_suite():
checker = renormalizing.RENormalizing([
(re.compile(r"object of type 'MyIter' has no len\(\)"),
r"len() of unsized object"),
])
suite = unittest.makeSuite(TALESTests)
suite.addTest(DocTestSuite("zope.tales.tales",checker=checker))
return suite
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())
|