[Zope3-checkins] CVS: Zope3/src/zope/tales/tests - test_tales.py:1.3
Jim Fulton
jim@zope.com
Tue, 20 May 2003 16:30:18 -0400
Update of /cvs-repository/Zope3/src/zope/tales/tests
In directory cvs.zope.org:/tmp/cvs-serv31353/src/zope/tales/tests
Modified Files:
test_tales.py
Log Message:
Moved the SimpleExpr class, which is only used for testing to the test
package.
Added a doctest test suite.
=== Zope3/src/zope/tales/tests/test_tales.py 1.2 => 1.3 ===
--- Zope3/src/zope/tales/tests/test_tales.py:1.2 Thu May 1 15:35:50 2003
+++ Zope3/src/zope/tales/tests/test_tales.py Tue May 20 16:30:17 2003
@@ -14,71 +14,73 @@
import unittest
from zope.tales import tales
+from zope.tales.tests.simpleexpr import SimpleExpr
+from zope.testing.doctestunit import DocTestSuite
class TALESTests(unittest.TestCase):
def testIterator0(self):
# Test sample Iterator class
- context = Harness()
+ context = Harness(self)
it = tales.Iterator('name', (), context)
- assert not it.next(), "Empty iterator"
+ self.assert_( not it.next(), "Empty iterator")
context._complete_()
def testIterator1(self):
# Test sample Iterator class
- context = Harness()
+ context = Harness(self)
it = tales.Iterator('name', (1,), context)
context._assert_('setLocal', 'name', 1)
- assert it.next() and not it.next(), "Single-element iterator"
+ self.assert_( it.next() and not it.next(), "Single-element iterator")
context._complete_()
def testIterator2(self):
# Test sample Iterator class
- context = Harness()
+ context = Harness(self)
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"
+ self.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
+ e.registerType('simple', SimpleExpr)
+ self.assert_( e.getTypes()['simple'] == SimpleExpr)
def testRegisterTypeUnique(self):
# Test expression type registration uniqueness
e = tales.ExpressionEngine()
- e.registerType('simple', tales.SimpleExpr)
+ e.registerType('simple', SimpleExpr)
try:
- e.registerType('simple', tales.SimpleExpr)
+ e.registerType('simple', SimpleExpr)
except tales.RegistrationError:
pass
else:
- assert 0, "Duplicate registration accepted."
+ 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, tales.SimpleExpr)
+ e.registerType(name, SimpleExpr)
except tales.RegistrationError:
pass
else:
- assert 0, 'Invalid type name "%s" accepted.' % name
+ self.assert_( 0, 'Invalid type name "%s" accepted.' % name)
def testCompile(self):
# Test expression compilation
e = tales.ExpressionEngine()
- e.registerType('simple', tales.SimpleExpr)
+ e.registerType('simple', SimpleExpr)
ce = e.compile('simple:x')
- assert ce(None) == ('simple', 'x'), (
- 'Improperly compiled expression %s.' % `ce`)
+ self.assert_( ce(None) == ('simple', 'x'), (
+ 'Improperly compiled expression %s.' % `ce`))
def testGetContext(self):
# Test Context creation
@@ -88,67 +90,88 @@
def getContext(self, **kws):
e = tales.ExpressionEngine()
- e.registerType('simple', tales.SimpleExpr)
+ e.registerType('simple', 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`)
+ self.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"'
+ c = ctxt.vars
+ self.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'
+ 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()
- assert c['v1'] == 1, "Uncovered local"
- assert c['g'] == 1, "Global from inner scope"
+ c = ctxt.vars
+ self.assert_( c['v1'] == 1, "Uncovered local")
+ self.assert_( c['g'] == 1, "Global from inner scope")
ctxt.endScope()
class Harness:
- def __init__(self):
- self.__callstack = []
+ def __init__(self, testcase):
+ self._callstack = []
+ self._testcase = testcase
def _assert_(self, name, *args, **kwargs):
- self.__callstack.append((name, args, kwargs))
+ self._callstack.append((name, args, kwargs))
def _complete_(self):
- assert len(self.__callstack) == 0, "Harness methods called"
+ assert len(self._callstack) == 0, "Harness methods called"
def __getattr__(self, name):
- cs = self.__callstack
- assert len(cs), 'Unexpected harness method call "%s".' % name
- assert cs[0][0] == name, (
- 'Harness method name "%s" called, "%s" expected.' %
- (name, cs[0][0]) )
- return self._method_
+ return HarnessMethod(self, name)
+
+class HarnessMethod:
- def _method_(self, *args, **kwargs):
- name, aargs, akwargs = self.__callstack.pop(0)
- assert aargs == args, "Harness method arguments"
- assert akwargs == kwargs, "Harness method keyword args"
+ 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():
- return unittest.makeSuite(TALESTests)
+ suite = unittest.makeSuite(TALESTests)
+ suite.addTest(DocTestSuite("zope.tales.tales"))
+ return suite
if __name__ == '__main__':