############################################################################## # # Copyright (c) 2001 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 # ############################################################################## """Document Template Tests $Id: testdt_var.py 70129 2006-09-12 15:26:09Z yusei $ """ # **************************************************************************** # *** Don't normalize whitespace in this file -- the tests depend on the *** # *** whitespace in the triple quoted strings. *** # **************************************************************************** import unittest from zope.documenttemplate.tests.dtmltestbase import DTMLTestBase class TestDT_Var(DTMLTestBase): def testFmt(self): html = self.doc_class ( u'''''') self.assertEqual(html(spam=42), u'$42.00 bob\'s your uncle') self.assertEqual(html(spam=None), u'spam%eggs!|') def testDefaultFmt(self): html = self.doc_class ( u""" html: url: multi: dollars: cents: dollars: cents: stx: """) result1 = ( u""" 4200000 html: 4200000 url: 4200000 multi: 4200000 dollars: $4200000 cents: $4200000.00 dollars: $4,200,000 cents: $4,200,000.00 stx:

4200000

""") # Caution: Some of these lines have significant trailing whitespace. # Necessary trailing blanks are explicitly forced via \x20. result2 = ( u""" None html: None url: None multi: None dollars:\x20\x20 cents:\x20\x20\x20\x20 dollars:\x20\x20 cents:\x20\x20\x20\x20 stx:

None

""") result3 = ( u""" \nfoo bar html: <a href="spam">\nfoo bar url: %3Ca%20href%3D%22spam%22%3E%0Afoo%20bar multi:
\nfoo bar dollars:\x20\x20 cents:\x20\x20\x20\x20 dollars:\x20\x20 cents:\x20\x20\x20\x20 stx:

\nfoo bar

""") self.assertEqual(html(spam=4200000).strip(), result1.strip()) self.assertEqual(html(spam=None).strip(), result2.strip()) self.assertEqual(html(spam=u'\nfoo bar').strip(), result3.strip()) def testRender(self): # Test automatic rendering of callable objects class C(object): x = 1 def y(self): return self.x * 2 h = self.doc_class(u"The h method, ") h2 = self.doc_class(u"The h2 method") res1 = self.doc_class(u", , ")(C()) res2 = self.doc_class( u""" , , """)(i=C()) expected = u'1, 2, The h method, 1 2' expected2 = ( u""" 1, 2, The h2 method""") self.assertEqual(res1, expected) self.assertEqual(res2, expected2) def testNonAsciiUnicode(self): html = self.doc_class( u""" English : Hello world! Japanese : Chinese : Korea : """) expected = ( u""" English : Hello world! Japanese : \u3053\u3093\u306b\u3061\u306f \u4e16\u754c! Chinese : \u4f60\u597d\uff0c\u4e16\u754c\uff01 Korea : \uc548\ub155, \uc138\uc0c1! """) self.assertEqual(html(japanese=u'\u3053\u3093\u306b\u3061\u306f \u4e16\u754c!', chinese=u'\u4f60\u597d\uff0c\u4e16\u754c\uff01', korean=u'\uc548\ub155, \uc138\uc0c1!'), expected) def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestDT_Var)) return suite if __name__ == '__main__': unittest.TextTestRunner().run(test_suite())