Commit 5961fbc5 authored by Tres Seaver's avatar Tres Seaver

Remove sclerotic junk from DocumentTemplate tests.

parent ea3b194f
...@@ -13,59 +13,19 @@ ...@@ -13,59 +13,19 @@
"""Document Template Tests """Document Template Tests
""" """
__rcs_id__='$Id$'
__version__='$Revision: 1.15 $'[11:-2]
import sys, os, cgi
import unittest import unittest
if __name__=='__main__': class DTMLTests(unittest.TestCase):
here = os.curdir
else:
from 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
from DocumentTemplate.DT_HTML import HTML, String
from ExtensionClass import Base
class D:
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, **kw):
for k, v in kw.items(): self.__dict__[k]=v
def __repr__(self): return "D(%s)" % `self.__dict__`
def d(**kw): return kw def _get_doc_class(self):
from DocumentTemplate.DT_HTML import HTML
docutils_include_warning = '''\ return HTML
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 1)</p> doc_class = property(_get_doc_class,)
<p>&quot;include&quot; directive disabled.</p>'''
docutils_raw_warning = '''\
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 1)</p>
<p>&quot;raw&quot; directive disabled.</p>'''
class PukeError(Exception):
"""Exception raised in test code."""
class DTMLTests (unittest.TestCase):
doc_class = HTML
def testBatchingEtc(self): def testBatchingEtc(self):
def item(key,**kw): return (key,kw) def item(key, **kw):
def item2(key,**kw): return kw return (key, kw)
class item_class:
def __init__(self,key,**kw):
for k in kw.keys(): self.__dict__[k]=kw[k]
items=( items=(
item( 1,dealer='Bay Chevy', make='Chevrolet', item( 1,dealer='Bay Chevy', make='Chevrolet',
...@@ -176,12 +136,11 @@ class DTMLTests (unittest.TestCase): ...@@ -176,12 +136,11 @@ class DTMLTests (unittest.TestCase):
self.assertEqual(res,expected) self.assertEqual(res,expected)
def testSequenceSummaries(self): def testSequenceSummaries(self):
def d(**kw): return kw data=(dict(name='jim', age=38),
data=(d(name='jim', age=38), # dict(name='kak', age=40),
# d(name='kak', age=40), dict(name='will', age=7),
d(name='will', age=7), dict(name='drew', age=4),
d(name='drew', age=4), dict(name='ches', age=1),
d(name='ches', age=1),
) )
html = self.doc_class('<dtml-in data mapping>' html = self.doc_class('<dtml-in data mapping>'
'<dtml-if sequence-end>' '<dtml-if sequence-end>'
...@@ -222,6 +181,7 @@ class DTMLTests (unittest.TestCase): ...@@ -222,6 +181,7 @@ class DTMLTests (unittest.TestCase):
assert res == expected, res assert res == expected, res
def testSimpleString(self): def testSimpleString(self):
from DocumentTemplate.DT_HTML import String
dt = String('%(name)s') dt = String('%(name)s')
res = dt(name='Chris') res = dt(name='Chris')
expected = 'Chris' expected = 'Chris'
...@@ -229,6 +189,7 @@ class DTMLTests (unittest.TestCase): ...@@ -229,6 +189,7 @@ class DTMLTests (unittest.TestCase):
def testStringDateFormatting(self): def testStringDateFormatting(self):
import DateTime import DateTime
from DocumentTemplate.DT_HTML import String
html = String("%(name capitalize spacify)s is " html = String("%(name capitalize spacify)s is "
"%(date fmt=year)s/%(date fmt=month)s/%(date fmt=day)s") "%(date fmt=year)s/%(date fmt=month)s/%(date fmt=day)s")
res = html(date=DateTime.DateTime("2001-04-27"), res = html(date=DateTime.DateTime("2001-04-27"),
...@@ -357,6 +318,7 @@ foo bar ...@@ -357,6 +318,7 @@ foo bar
self.assert_(docutils_include_warning in result) self.assert_(docutils_include_warning in result)
def test_fmt_reST_raw_directive_disabled(self): def test_fmt_reST_raw_directive_disabled(self):
from cgi import escape
EXPECTED = '<h1>HELLO WORLD</h1>' EXPECTED = '<h1>HELLO WORLD</h1>'
source = '.. raw:: html\n\n %s\n' % EXPECTED source = '.. raw:: html\n\n %s\n' % EXPECTED
html = self.doc_class('<dtml-var name="foo" fmt="restructured-text">') html = self.doc_class('<dtml-var name="foo" fmt="restructured-text">')
...@@ -367,7 +329,7 @@ foo bar ...@@ -367,7 +329,7 @@ foo bar
# verbatimly in the rendered output. Instead a warning # verbatimly in the rendered output. Instead a warning
# message is presented: # message is presented:
self.assert_(EXPECTED not in result) self.assert_(EXPECTED not in result)
self.assert_(cgi.escape(EXPECTED) in result) self.assert_(escape(EXPECTED) in result)
self.assert_(docutils_raw_warning in result) self.assert_(docutils_raw_warning in result)
def test_fmt_reST_raw_directive_file_option_raises(self): def test_fmt_reST_raw_directive_file_option_raises(self):
...@@ -395,6 +357,7 @@ foo bar ...@@ -395,6 +357,7 @@ foo bar
self.assert_(docutils_raw_warning in result) self.assert_(docutils_raw_warning in result)
def testPropogatedError(self): def testPropogatedError(self):
from ExtensionClass import Base
class foo: class foo:
def __len__(self): return 9 def __len__(self): return 9
...@@ -434,7 +397,8 @@ foo bar ...@@ -434,7 +397,8 @@ foo bar
assert 0, 'Puke error not propogated' assert 0, 'Puke error not propogated'
def testRenderCallable(self): def testRenderCallable(self):
"Test automatic rendering of callable objects" #Test automatic rendering of callable objects
from ExtensionClass import Base
class C (Base): class C (Base):
__allow_access_to_unprotected_subobjects__ = 1 __allow_access_to_unprotected_subobjects__ = 1
x=1 x=1
...@@ -483,7 +447,9 @@ foo bar ...@@ -483,7 +447,9 @@ foo bar
def testNoItemPush(self): def testNoItemPush(self):
data=d(sec='B', name='XXX', sub=(d(name='b1'),d(name='b2',sec='XXX'))) data = dict(sec='B',
name='XXX',
sub=(dict(name='b1'), dict(name='b2',sec='XXX')))
html = """ html = """
<dtml-with data mapping><dtml-in sub no_push_item> <dtml-with data mapping><dtml-in sub no_push_item>
<dtml-var sec>.<dtml-with sequence-item mapping><dtml-var name></dtml-with> <dtml-var sec>.<dtml-with sequence-item mapping><dtml-var name></dtml-with>
...@@ -496,11 +462,11 @@ foo bar ...@@ -496,11 +462,11 @@ foo bar
def testBasicHTMLIn(self): def testBasicHTMLIn(self):
data=( data=(
d(name='jim', age=39), dict(name='jim', age=39),
d(name='kak', age=29), dict(name='kak', age=29),
d(name='will', age=8), dict(name='will', age=8),
d(name='andrew', age=5), dict(name='andrew', age=5),
d(name='chessie',age=2), dict(name='chessie',age=2),
) )
html=""" html="""
...@@ -519,7 +485,7 @@ foo bar ...@@ -519,7 +485,7 @@ foo bar
assert result == expected, result assert result == expected, result
def testBasicHTMLIn2(self): def testBasicHTMLIn2(self):
xxx=(D(name=1), D(name=2), D(name=3)) xxx=(Dummy(name=1), Dummy(name=2), Dummy(name=3))
html = """ html = """
<!--#in xxx--> <!--#in xxx-->
<!--#var name --> <!--#var name -->
...@@ -543,7 +509,7 @@ foo bar ...@@ -543,7 +509,7 @@ foo bar
assert result == expected, result assert result == expected, result
def testHTMLInElse(self): def testHTMLInElse(self):
xxx=(D(name=1), D(name=2), D(name=3)) xxx=(Dummy(name=1), Dummy(name=2), Dummy(name=3))
html=""" html="""
<!--#in data mapping--> <!--#in data mapping-->
<!--#var name-->, <!--#var age--> <!--#var name-->, <!--#var age-->
...@@ -562,12 +528,13 @@ foo bar ...@@ -562,12 +528,13 @@ foo bar
assert result == expected, result assert result == expected, result
def testBasicStringIn(self): def testBasicStringIn(self):
from DocumentTemplate.DT_HTML import String
data=( data=(
d(name='jim', age=39), dict(name='jim', age=39),
d(name='kak', age=29), dict(name='kak', age=29),
d(name='will', age=8), dict(name='will', age=8),
d(name='andrew', age=5), dict(name='andrew', age=5),
d(name='chessie',age=2), dict(name='chessie',age=2),
) )
s=""" s="""
%(in data mapping)[ %(in data mapping)[
...@@ -584,13 +551,37 @@ foo bar ...@@ -584,13 +551,37 @@ foo bar
result = String(s)(data=data) result = String(s)(data=data)
assert expected == result, result assert expected == result, result
def read_file(name):
import os
from DocumentTemplate import tests
here = tests.__path__[0]
f = open(os.path.join(here, name), 'r')
res = f.read()
f.close()
return res
class Dummy:
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, **kw):
self.__dict__.update(kw)
def __repr__(self):
return "Dummy(%s)" % `self.__dict__`
docutils_include_warning = '''\
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 1)</p>
<p>&quot;include&quot; directive disabled.</p>'''
docutils_raw_warning = '''\
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 1)</p>
<p>&quot;raw&quot; directive disabled.</p>'''
class PukeError(Exception):
"""Exception raised in test code."""
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( DTMLTests ) ) suite.addTest( unittest.makeSuite( DTMLTests ) )
return suite return suite
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
main()
...@@ -12,16 +12,8 @@ ...@@ -12,16 +12,8 @@
############################################################################## ##############################################################################
"""Document Template Tests """Document Template Tests
""" """
__rcs_id__='$Id$'
__version__='$Revision: 1.4 $'[11:-2]
import sys, os
import unittest import unittest
from DocumentTemplate.DT_HTML import HTML, String
from ExtensionClass import Base
class force_str: class force_str:
# A class whose string representation is not always a plain string: # A class whose string representation is not always a plain string:
def __init__(self,s): def __init__(self,s):
...@@ -31,7 +23,10 @@ class force_str: ...@@ -31,7 +23,10 @@ class force_str:
class DTMLUnicodeTests (unittest.TestCase): class DTMLUnicodeTests (unittest.TestCase):
doc_class = HTML def _get_doc_class(self):
from DocumentTemplate.DT_HTML import HTML
return HTML
doc_class = property(_get_doc_class,)
def testAA(self): def testAA(self):
html=self.doc_class('<dtml-var a><dtml-var b>') html=self.doc_class('<dtml-var a><dtml-var b>')
...@@ -91,9 +86,3 @@ def test_suite(): ...@@ -91,9 +86,3 @@ def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( DTMLUnicodeTests ) ) suite.addTest( unittest.makeSuite( DTMLUnicodeTests ) )
return suite return suite
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
main()
...@@ -21,7 +21,6 @@ class Test_Raise(unittest.TestCase): ...@@ -21,7 +21,6 @@ class Test_Raise(unittest.TestCase):
self.assertEqual(raiser.expr, None) self.assertEqual(raiser.expr, None)
def test_ctor_w_expr(self): def test_ctor_w_expr(self):
from DocumentTemplate.DT_Util import Eval
raiser = self._makeOne(expr='SyntaxError') raiser = self._makeOne(expr='SyntaxError')
self.assertEqual(raiser.__name__, 'SyntaxError') self.assertEqual(raiser.__name__, 'SyntaxError')
self.assertEqual(raiser.expr.expr, 'SyntaxError') self.assertEqual(raiser.expr.expr, 'SyntaxError')
......
...@@ -16,7 +16,6 @@ $Id$ ...@@ -16,7 +16,6 @@ $Id$
""" """
import unittest, doctest import unittest, doctest
from DocumentTemplate import DT_Var
class TestNewlineToBr(doctest.DocTestCase): class TestNewlineToBr(doctest.DocTestCase):
...@@ -25,6 +24,7 @@ class TestNewlineToBr(doctest.DocTestCase): ...@@ -25,6 +24,7 @@ class TestNewlineToBr(doctest.DocTestCase):
newline_to_br should work identically with either DOS-style or newline_to_br should work identically with either DOS-style or
Unix-style newlines. Unix-style newlines.
>>> from DocumentTemplate import DT_Var
>>> text = ''' >>> text = '''
... line one ... line one
... line two ... line two
...@@ -46,6 +46,7 @@ class TestNewlineToBr(doctest.DocTestCase): ...@@ -46,6 +46,7 @@ class TestNewlineToBr(doctest.DocTestCase):
def test_newline_to_br_tainted(self): def test_newline_to_br_tainted(self):
""" """
>>> from DocumentTemplate import DT_Var
>>> text = ''' >>> text = '''
... <li>line one</li> ... <li>line one</li>
... <li>line two</li> ... <li>line two</li>
......
...@@ -17,7 +17,6 @@ $Id$ ...@@ -17,7 +17,6 @@ $Id$
import unittest import unittest
from DocumentTemplate.ustr import ustr
class force_str: class force_str:
...@@ -40,45 +39,70 @@ class Bar(unicode): ...@@ -40,45 +39,70 @@ class Bar(unicode):
class UnicodeTests(unittest.TestCase): class UnicodeTests(unittest.TestCase):
def testPlain(self): def test_bare_string_literall(self):
from DocumentTemplate.ustr import ustr
a = ustr('hello') a = ustr('hello')
assert a=='hello', `a` self.assertEqual(a, 'hello')
def test_with_force_str(self):
from DocumentTemplate.ustr import ustr
a = ustr(force_str('hello')) a = ustr(force_str('hello'))
assert a=='hello', `a` self.assertEqual(a, 'hello')
def test_with_non_ascii_char(self):
from DocumentTemplate.ustr import ustr
a = ustr(chr(200)) a = ustr(chr(200))
assert a==chr(200), `a` self.assertEqual(a, chr(200))
def test_with_force_str_non_ascii_char(self):
from DocumentTemplate.ustr import ustr
a = ustr(force_str(chr(200))) a = ustr(force_str(chr(200)))
assert a==chr(200), `a` self.assertEqual(a, chr(200))
def test_with_int(self):
from DocumentTemplate.ustr import ustr
a = ustr(22) a = ustr(22)
assert a=='22', `a` self.assertEqual(a, '22')
def test_with_list(self):
from DocumentTemplate.ustr import ustr
a = ustr([1,2,3]) a = ustr([1,2,3])
assert a=='[1, 2, 3]', `a` self.assertEqual(a, '[1, 2, 3]')
def testUnicode(self): def test_w_unicode_literal(self):
from DocumentTemplate.ustr import ustr
a = ustr(u'hello') a = ustr(u'hello')
assert a=='hello', `a` self.assertEqual(a, 'hello')
def test_w_force_str_unicode_literal(self):
from DocumentTemplate.ustr import ustr
a = ustr(force_str(u'hello')) a = ustr(force_str(u'hello'))
assert a=='hello', `a` self.assertEqual(a, 'hello')
def test_w_unichr(self):
from DocumentTemplate.ustr import ustr
a = ustr(unichr(200)) a = ustr(unichr(200))
assert a==unichr(200), `a` self.assertEqual(a, unichr(200))
def test_w_force_str_unichr(self):
from DocumentTemplate.ustr import ustr
a = ustr(force_str(unichr(200))) a = ustr(force_str(unichr(200)))
assert a==unichr(200), `a` self.assertEqual(a, unichr(200))
def testExceptions(self): def test_w_unichr_in_exception(self):
from DocumentTemplate.ustr import ustr
a = ustr(ValueError(unichr(200))) a = ustr(ValueError(unichr(200)))
assert a==unichr(200), `a` self.assertEqual(a, unichr(200))
def testCustomStrings(self): def testCustomStrings(self):
from DocumentTemplate.ustr import ustr
a = ustr(Foo('foo')) a = ustr(Foo('foo'))
self.failUnlessEqual(type(a), Foo) self.assertEqual(type(a), Foo)
a = ustr(Bar('bar')) a = ustr(Bar('bar'))
self.failUnlessEqual(type(a), Bar) self.assertEqual(type(a), Bar)
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( UnicodeTests ) ) suite.addTest( unittest.makeSuite( UnicodeTests ) )
return suite return suite
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment