1 #
2 # Copyright (c) 2001 Richard Jones
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 #
11 # $Id: test_htmltemplate.py,v 1.2 2002-01-22 00:12:07 richard Exp $
13 import unittest, cgi
15 from roundup.htmltemplate import TemplateFunctions
16 from roundup import date
17 from roundup import password
18 from roundup.hyperdb import String, Password, Date, Interval, Link, Multilink
20 class Class:
21 def get(self, nodeid, attribute, default=None):
22 if attribute == 'string':
23 return 'Node %s: I am a string'%nodeid
24 elif attribute == 'date':
25 return date.Date('2000-01-01')
26 elif attribute == 'interval':
27 return date.Interval('-3d')
28 elif attribute == 'link':
29 return '1'
30 elif attribute == 'multilink':
31 return ['1', '2']
32 elif attribute == 'password':
33 return password.Password('sekrit')
34 elif attribute == 'key':
35 return 'the key'
36 elif attribute == 'html':
37 return '<html>hello, I am HTML</html>'
38 def list(self):
39 return ['1', '2']
40 def getprops(self):
41 return {'string': String(), 'date': Date(), 'interval': Interval(),
42 'link': Link('other'), 'multilink': Multilink('other'),
43 'password': Password(), 'html': String(), 'key': String()}
44 def labelprop(self):
45 return 'key'
47 class Database:
48 classes = {'other': Class()}
49 def getclass(self, name):
50 return Class()
51 def __getattr(self, name):
52 return Class()
54 class NodeCase(unittest.TestCase):
55 def setUp(self):
56 ''' Set up the harness for calling the individual tests
57 '''
58 self.tf = tf = TemplateFunctions()
59 tf.nodeid = '1'
60 tf.cl = Class()
61 tf.properties = tf.cl.getprops()
62 tf.db = Database()
64 # def do_plain(self, property, escape=0):
65 def testPlain_string(self):
66 s = 'Node 1: I am a string'
67 self.assertEqual(self.tf.do_plain('string'), s)
69 def testPlain_password(self):
70 self.assertEqual(self.tf.do_plain('password'), '*encrypted*')
72 def testPlain_html(self):
73 s = '<html>hello, I am HTML</html>'
74 self.assertEqual(self.tf.do_plain('html', escape=0), s)
75 s = cgi.escape(s)
76 self.assertEqual(self.tf.do_plain('html', escape=1), s)
78 def testPlain_date(self):
79 self.assertEqual(self.tf.do_plain('date'), '2000-01-01.00:00:00')
81 def testPlain_interval(self):
82 self.assertEqual(self.tf.do_plain('interval'), '- 3d')
84 def testPlain_link(self):
85 self.assertEqual(self.tf.do_plain('link'), 'the key')
87 def testPlain_multilink(self):
88 self.assertEqual(self.tf.do_plain('multilink'), '1, 2')
91 # def do_field(self, property, size=None, showid=0):
92 def testField_string(self):
93 self.assertEqual(self.tf.do_field('string'),
94 '<input name="string" value="Node 1: I am a string" size="30">')
95 self.assertEqual(self.tf.do_field('string', size=10),
96 '<input name="string" value="Node 1: I am a string" size="10">')
98 def testField_password(self):
99 self.assertEqual(self.tf.do_field('password'),
100 '<input type="password" name="password" size="30">')
101 self.assertEqual(self.tf.do_field('password', size=10),
102 '<input type="password" name="password" size="10">')
104 def testField_html(self):
105 self.assertEqual(self.tf.do_field('html'), '<input name="html" '
106 'value="<html>hello, I am HTML</html>" size="30">')
107 self.assertEqual(self.tf.do_field('html', size=10),
108 '<input name="html" value="<html>hello, I am '
109 'HTML</html>" size="10">')
111 def testField_date(self):
112 self.assertEqual(self.tf.do_field('date'),
113 '<input name="date" value="2000-01-01.00:00:00" size="30">')
114 self.assertEqual(self.tf.do_field('date', size=10),
115 '<input name="date" value="2000-01-01.00:00:00" size="10">')
117 def testField_interval(self):
118 self.assertEqual(self.tf.do_field('interval'),
119 '<input name="interval" value="- 3d" size="30">')
120 self.assertEqual(self.tf.do_field('interval', size=10),
121 '<input name="interval" value="- 3d" size="10">')
123 def testField_link(self):
124 self.assertEqual(self.tf.do_field('link'), '''<select name="link">
125 <option value="-1">- no selection -</option>
126 <option selected value="1">the key</option>
127 <option value="2">the key</option>
128 </select>''')
130 def testField_multilink(self):
131 self.assertEqual(self.tf.do_field('multilink'),
132 '<input name="multilink" size="30" value="the key,the key">')
133 self.assertEqual(self.tf.do_field('multilink', size=10),
134 '<input name="multilink" size="10" value="the key,the key">')
136 # def do_menu(self, property, size=None, height=None, showid=0):
137 def testMenu_link(self):
138 self.assertEqual(self.tf.do_menu('link'), '''<select name="link">
139 <option value="-1">- no selection -</option>
140 <option selected value="1">the key</option>
141 <option value="2">the key</option>
142 </select>''')
143 self.assertEqual(self.tf.do_menu('link', size=6),
144 '''<select name="link">
145 <option value="-1">- no selection -</option>
146 <option selected value="1">the...</option>
147 <option value="2">the...</option>
148 </select>''')
149 self.assertEqual(self.tf.do_menu('link', showid=1),
150 '''<select name="link">
151 <option value="-1">- no selection -</option>
152 <option selected value="1">other1: the key</option>
153 <option value="2">other2: the key</option>
154 </select>''')
156 def testMenu_multilink(self):
157 self.assertEqual(self.tf.do_menu('multilink', height=10),
158 '''<select multiple name="multilink" size="10">
159 <option selected value="1">the key</option>
160 <option selected value="2">the key</option>
161 </select>''')
162 self.assertEqual(self.tf.do_menu('multilink', size=6, height=10),
163 '''<select multiple name="multilink" size="10">
164 <option selected value="1">the...</option>
165 <option selected value="2">the...</option>
166 </select>''')
167 self.assertEqual(self.tf.do_menu('multilink', showid=1),
168 '''<select multiple name="multilink" size="2">
169 <option selected value="1">other1: the key</option>
170 <option selected value="2">other2: the key</option>
171 </select>''')
173 def suite():
174 return unittest.makeSuite(NodeCase, 'test')
177 #
178 # $Log: not supported by cvs2svn $
179 # Revision 1.1 2002/01/21 11:05:48 richard
180 # New tests for htmltemplate (well, it's a beginning)
181 #
182 #
183 #
184 # vim: set filetype=python ts=4 sw=4 et si