[Zope-Checkins] CVS: Zope3/lib/python/Zope/DocumentTemplate/tests - DTMLTestBase.py:1.1 __init__.py:1.1 testDT_If.py:1.1 testDT_In.py:1.1 testDT_Try.py:1.1 testDT_Var.py:1.1 testDT_With.py:1.1

Stephan Richter srichter@cbu.edu
Tue, 25 Jun 2002 11:37:18 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/DocumentTemplate/tests
In directory cvs.zope.org:/tmp/cvs-serv4274/lib/python/Zope/DocumentTemplate/tests

Added Files:
	DTMLTestBase.py __init__.py testDT_If.py testDT_In.py 
	testDT_Try.py testDT_Var.py testDT_With.py 
Log Message:
RDB/SQL Sprint team (Vika, Michael and Stephan):

Alright, to make nice dynamic SQL methods we really want DTML ported for many reasons. This is the first cut at the problem. The DTML code is based on the Zope 2.3 release version. We have implemented some initial tests for the directives and ripped out all of the old ZMI and security cruft.


=== Added File Zope3/lib/python/Zope/DocumentTemplate/tests/DTMLTestBase.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: DTMLTestBase.py,v 1.1 2002/06/25 15:37:17 srichter Exp $
"""

import os
import unittest
from Zope.DocumentTemplate.DT_HTML import HTML, String


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/lib/python/Zope/DocumentTemplate/tests/__init__.py ===


=== Added File Zope3/lib/python/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 2002/06/25 15:37:17 srichter Exp $
"""

import unittest
from Zope.DocumentTemplate import String
from 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/lib/python/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 2002/06/25 15:37:17 srichter Exp $
"""

import unittest
from Zope.DocumentTemplate import String
from 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/lib/python/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 2002/06/25 15:37:17 srichter Exp $
"""

import unittest
from Zope.DocumentTemplate import String
from 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/lib/python/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 2002/06/25 15:37:17 srichter Exp $
"""

import unittest
from Zope.DocumentTemplate import String
from 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:     &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:  
            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 obnjects"
       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/lib/python/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 2002/06/25 15:37:17 srichter Exp $
"""

import unittest
from 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())