[Zope3-checkins] CVS: Zope3/src/zope/documenttemplate/tests - __init__.py:1.2 dtmltestbase.py:1.2 testdt_if.py:1.2 testdt_in.py:1.2 testdt_try.py:1.2 testdt_var.py:1.2 testdt_with.py:1.2

Jim Fulton jim@zope.com
Wed, 25 Dec 2002 09:14:08 -0500


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

Added Files:
	__init__.py dtmltestbase.py testdt_if.py testdt_in.py 
	testdt_try.py testdt_var.py testdt_with.py 
Log Message:
Grand renaming:

- Renamed most files (especially python modules) to lower case.

- Moved views and interfaces into separate hierarchies within each
  project, where each top-level directory under the zope package
  is a separate project.

- Moved everything to src from lib/python.

  lib/python will eventually go away. I need access to the cvs
  repository to make this happen, however.

There are probably some bits that are broken. All tests pass
and zope runs, but I haven't tried everything. There are a number
of cleanups I'll work on tomorrow.



=== Zope3/src/zope/documenttemplate/tests/__init__.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:08 2002
+++ Zope3/src/zope/documenttemplate/tests/__init__.py	Wed Dec 25 09:13:37 2002
@@ -0,0 +1,2 @@
+#
+# This file is necessary to make this directory a package.


=== Zope3/src/zope/documenttemplate/tests/dtmltestbase.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:08 2002
+++ Zope3/src/zope/documenttemplate/tests/dtmltestbase.py	Wed Dec 25 09:13:37 2002
@@ -0,0 +1,50 @@
+##############################################################################
+#
+# 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$
+"""
+
+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


=== Zope3/src/zope/documenttemplate/tests/testdt_if.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:08 2002
+++ Zope3/src/zope/documenttemplate/tests/testdt_if.py	Wed Dec 25 09:13:37 2002
@@ -0,0 +1,86 @@
+##############################################################################
+#
+# 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$
+"""
+
+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())


=== Zope3/src/zope/documenttemplate/tests/testdt_in.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:08 2002
+++ Zope3/src/zope/documenttemplate/tests/testdt_in.py	Wed Dec 25 09:13:37 2002
@@ -0,0 +1,129 @@
+##############################################################################
+#
+# 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$
+"""
+
+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())


=== Zope3/src/zope/documenttemplate/tests/testdt_try.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:08 2002
+++ Zope3/src/zope/documenttemplate/tests/testdt_try.py	Wed Dec 25 09:13:37 2002
@@ -0,0 +1,49 @@
+##############################################################################
+#
+# 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$
+"""
+
+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())


=== Zope3/src/zope/documenttemplate/tests/testdt_var.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:08 2002
+++ Zope3/src/zope/documenttemplate/tests/testdt_var.py	Wed Dec 25 09:13:37 2002
@@ -0,0 +1,131 @@
+##############################################################################
+#
+# 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$
+"""
+
+# 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
+
+            """)
+
+        # Caution:  Some of these lines have significant trailing whitespace.
+        # Necessary trailing blanks are explicitly forced via \x20.
+        result2 = (
+            """
+                      None
+            html:     None
+            url:      None
+            multi:    None
+            dollars:\x20\x20
+            cents:\x20\x20\x20\x20
+            dollars,:\x20
+            cents,:\x20\x20\x20
+
+            """)
+
+        result3 = (
+            """
+                      <a href="spam">\nfoo bar
+            html:     &lt;a href=&quot;spam&quot;&gt;\nfoo bar
+            url:      %3Ca%20href%3D%22spam%22%3E%0Afoo%20bar
+            multi:    <a href="spam"><br>\nfoo bar
+            dollars:\x20\x20
+            cents:\x20\x20\x20\x20
+            dollars,:\x20
+            cents,:\x20\x20\x20
+
+            """)
+
+        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())


=== Zope3/src/zope/documenttemplate/tests/testdt_with.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:08 2002
+++ Zope3/src/zope/documenttemplate/tests/testdt_with.py	Wed Dec 25 09:13:37 2002
@@ -0,0 +1,47 @@
+##############################################################################
+#
+# 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$
+"""
+
+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())