[Zope3-checkins] CVS: Zope3/src/zope/documenttemplate/tests - __init__.py:1.1.2.1 dtmltestbase.py:1.1.2.1 testdt_if.py:1.1.2.1 testdt_in.py:1.1.2.1 testdt_try.py:1.1.2.1 testdt_var.py:1.1.2.1 testdt_with.py:1.1.2.1
Jim Fulton
jim@zope.com
Mon, 23 Dec 2002 14:32:49 -0500
Update of /cvs-repository/Zope3/src/zope/documenttemplate/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/documenttemplate/tests
Added Files:
Tag: NameGeddon-branch
__init__.py dtmltestbase.py testdt_if.py testdt_in.py
testdt_try.py testdt_var.py testdt_with.py
Log Message:
Initial renaming before debugging
=== Added File Zope3/src/zope/documenttemplate/tests/__init__.py ===
#
# This file is necessary to make this directory a package.
=== Added File Zope3/src/zope/documenttemplate/tests/dtmltestbase.py ===
##############################################################################
#
# Copyright (c) 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
#
##############################################################################
"""Document Template Tests
$Id: dtmltestbase.py,v 1.1.2.1 2002/12/23 19:32:47 jim Exp $
"""
import os
import unittest
from zope.documenttemplate.dt_html import HTML
if __name__=='__main__':
here = os.curdir
else:
from zope.documenttemplate import tests
here = tests.__path__[0]
def read_file(name):
f = open(os.path.join(here, name), 'r')
res = f.read()
f.close()
return res
class ObjectStub:
def __init__(self, **kw):
for k, v in kw.items():
self.__dict__[k]=v
def __repr__(self):
return "D(%s)" % `self.__dict__`
def dict(**kw):
return kw
class DTMLTestBase(unittest.TestCase):
doc_class = HTML
=== Added File Zope3/src/zope/documenttemplate/tests/testdt_if.py ===
##############################################################################
#
# Copyright (c) 2001 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
#
##############################################################################
"""Document Template Tests
$Id: testdt_if.py,v 1.1.2.1 2002/12/23 19:32:47 jim Exp $
"""
import unittest
from zope.documenttemplate import String
from zope.documenttemplate.tests.dtmltestbase import DTMLTestBase
class TestDT_If(DTMLTestBase):
def testBasic(self):
html = self.doc_class(
"""\
<dtml-if value>
The arguments were: <dtml-var value>
</dtml-if>
""")
result1 = "The arguments were: foo"
result2 = ""
self.assertEqual(html(value='foo').strip(), result1.strip())
self.assertEqual(html().strip(), result2.strip())
def testElse(self):
html = self.doc_class(
"""\
<dtml-if value>
The arguments were: <dtml-var value>
<dtml-else>
No arguments were given.
</dtml-if>
""")
result1 = "The arguments were: foo"
result2 = "No arguments were given."
self.assertEqual(html(value='foo').strip(), result1.strip())
self.assertEqual(html().strip(), result2.strip())
def testElIf(self):
html = self.doc_class(
"""\
<dtml-if value>
The arguments were: <dtml-var value>
<dtml-elif attribute>
The attributes were: <dtml-var attribute>
</dtml-if>
""")
result1 = "The arguments were: foo"
result2 = "The attributes were: bar"
self.assertEqual(html(value='foo', attribute='').strip(),
result1.strip())
self.assertEqual(html(value='', attribute='bar').strip(),
result2.strip())
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite(TestDT_If) )
return suite
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/documenttemplate/tests/testdt_in.py ===
##############################################################################
#
# Copyright (c) 2001 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
#
##############################################################################
"""Document Template Tests
$Id: testdt_in.py,v 1.1.2.1 2002/12/23 19:32:47 jim Exp $
"""
import unittest
from zope.documenttemplate import String
from zope.documenttemplate.tests.dtmltestbase import DTMLTestBase, dict, ObjectStub
class TestDT_In(DTMLTestBase):
def testMapping(self):
data = (
dict(name='jim', age=39),
dict(name='kak', age=29),
dict(name='will', age=8),
dict(name='andrew', age=5),
dict(name='chessie',age=2),
)
html="""
<dtml-in data mapping>
<dtml-var name>, <dtml-var age>
</dtml-in>
"""
expected = """
jim, 39
kak, 29
will, 8
andrew, 5
chessie, 2
"""
result = self.doc_class(html)(data=data)
self.assertEqual(result, expected)
def testObjectSequence(self):
seq = (ObjectStub(name=1), ObjectStub(name=2), ObjectStub(name=3))
html = """
<dtml-in seq>
<dtml-var name>
</dtml-in>
"""
expected = """
1
2
3
"""
result = self.doc_class(html)(seq=seq)
self.assertEqual(result, expected)
def testSequenceNamespace(self):
ns = {'prop_ids': ('name', 'id'), 'name': 'good', 'id': 'times'}
html = """:<dtml-in prop_ids><dtml-var sequence-item>=<dtml-var
expr="_[_['sequence-item']]">:</dtml-in>"""
result = self.doc_class(html)(None, ns)
expected = ":name=good:id=times:"
self.assertEqual(result, expected)
def testElse(self):
seq=(ObjectStub(name=1), ObjectStub(name=2), ObjectStub(name=3))
html="""
<dtml-in data mapping>
<dtml-var name>, <dtml-var age>
<dtml-else>
<dtml-in seq>
<dtml-var name>
</dtml-in>
</dtml-in>
"""
expected = """
1
2
3
"""
result = self.doc_class(html)(seq=seq, data={})
self.assertEqual(result, expected)
def testStringSyntax(self):
data=(
dict(name='jim', age=39),
dict(name='kak', age=29),
dict(name='will', age=8),
dict(name='andrew', age=5),
dict(name='chessie',age=2),
)
s="""
%(in data mapping)[
%(name)s, %(age)s
%(in)]
"""
expected = """
jim, 39
kak, 29
will, 8
andrew, 5
chessie, 2
"""
result = String(s)(data=data)
self.assertEqual(result, expected)
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite(TestDT_In) )
return suite
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/documenttemplate/tests/testdt_try.py ===
##############################################################################
#
# Copyright (c) 2001 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
#
##############################################################################
"""Document Template Tests
$Id: testdt_try.py,v 1.1.2.1 2002/12/23 19:32:47 jim Exp $
"""
import unittest
from zope.documenttemplate import String
from zope.documenttemplate.tests.dtmltestbase import DTMLTestBase
class TestDT_Try(DTMLTestBase):
def testBasic(self):
html = self.doc_class(
"""
<dtml-try>
foo = <dtml-var value>
<dtml-except>
There is no bar variable.
</dtml-try>
""")
result1 = "foo = bar"
result2 = "There is no bar variable."
self.assertEqual(html(value='bar').strip(), result1.strip())
self.assertEqual(html().strip(), result2.strip())
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite(TestDT_Try))
return suite
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/documenttemplate/tests/testdt_var.py ===
##############################################################################
#
# Copyright (c) 2001 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
#
##############################################################################
"""Document Template Tests
$Id: testdt_var.py,v 1.1.2.1 2002/12/23 19:32:47 jim Exp $
"""
# XXX Don't normalize whitespace in this file -- the tests depend on the
# whitespace in the triple quoted strings.
import unittest
from zope.documenttemplate import String
from zope.documenttemplate.tests.dtmltestbase import DTMLTestBase, dict, ObjectStub
class TestDT_Var(DTMLTestBase):
def testFmt(self):
html = self.doc_class (
'''<dtml-var spam fmt="$%.2f bob\'s your uncle"
null="spam%eggs!|">''')
self.assertEqual(html(spam=42), '$42.00 bob\'s your uncle')
self.assertEqual(html(spam=None), 'spam%eggs!|')
def testDefaultFmt(self):
#import Missing
html = self.doc_class (
"""
<dtml-var spam >
html: <dtml-var spam fmt=html-quote>
url: <dtml-var spam fmt=url-quote>
multi: <dtml-var spam fmt=multi-line>
dollars: <dtml-var spam fmt=whole-dollars>
cents: <dtml-var spam fmt=dollars-and-cents>
dollars,: <dtml-var spam fmt=dollars-with-commas>
cents,: <dtml-var spam fmt=dollars-and-cents-with-commas>
""")
result1 = (
"""
4200000
html: 4200000
url: 4200000
multi: 4200000
dollars: $4200000
cents: $4200000.00
dollars,: $4,200,000
cents,: $4,200,000.00
""")
result2 = (
"""
None
html: None
url: None
multi: None
dollars:
cents:
dollars,:
cents,:
""")
result3 = (
"""
<a href="spam">\nfoo bar
html: <a href="spam">\nfoo bar
url: %3Ca%20href%3D%22spam%22%3E%0Afoo%20bar
multi: <a href="spam"><br>\nfoo bar
dollars:
cents:
dollars,:
cents,:
""")
self.assertEqual(html(spam=4200000), result1)
self.assertEqual(html(spam=None), result2)
self.assertEqual(html(spam='<a href="spam">\nfoo bar'), result3)
def testRender(self):
# Test automatic rendering of callable objects
class C:
x = 1
def y(self): return self.x * 2
h = self.doc_class("The h method, <dtml-var x> <dtml-var y>")
h2 = self.doc_class("The h2 method")
res1 = self.doc_class("<dtml-var x>, <dtml-var y>, <dtml-var h>")(C())
res2 = self.doc_class(
"""
<dtml-var expr="_.render(i.x)">,
<dtml-var expr="_.render(i.y)">,
<dtml-var expr="_.render(i.h2)">""")(i=C())
expected = '1, 2, The h method, 1 2'
expected2 = (
"""
1,
2,
The h2 method""")
self.assertEqual(res1, expected)
self.assertEqual(res2, expected2)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDT_Var))
return suite
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/documenttemplate/tests/testdt_with.py ===
##############################################################################
#
# Copyright (c) 2001 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
#
##############################################################################
"""Document Template Tests
$Id: testdt_with.py,v 1.1.2.1 2002/12/23 19:32:47 jim Exp $
"""
import unittest
from zope.documenttemplate.tests.dtmltestbase import DTMLTestBase
class TestDT_With(DTMLTestBase):
def testBasic(self):
class person:
name='Jim'
height_inches=73
result = self.doc_class("""<dtml-with person>
Hi, my name is <dtml-var name>
My height is <dtml-var "height_inches*2.54"> centimeters.
</dtml-with>""")(person=person)
expected = """ Hi, my name is Jim
My height is 185.42 centimeters.
"""
self.assertEqual(result, expected)
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite(TestDT_With) )
return suite
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())