|
|
1 |
|
|
|
2 |
# tests.py -- Unit tests for classes in djblets.util |
|
|
3 |
# |
|
|
4 |
# Copyright (c) 2007-2008 Christian Hammond |
|
|
5 |
# Copyright (c) 2007-2008 David Trowbridge |
|
|
6 |
# |
|
|
7 |
# Permission is hereby granted, free of charge, to any person obtaining |
|
|
8 |
# a copy of this software and associated documentation files (the |
|
|
9 |
# "Software"), to deal in the Software without restriction, including |
|
|
10 |
# without limitation the rights to use, copy, modify, merge, publish, |
|
|
11 |
# distribute, sublicense, and/or sell copies of the Software, and to |
|
|
12 |
# permit persons to whom the Software is furnished to do so, subject to |
|
|
13 |
# the following conditions: |
|
|
14 |
# |
|
|
15 |
# The above copyright notice and this permission notice shall be included |
|
|
16 |
# in all copies or substantial portions of the Software. |
|
|
17 |
# |
|
|
18 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
|
19 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
|
20 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
|
21 |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
|
22 |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
|
23 |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
|
24 |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
import datetime |
|
|
28 |
import unittest |
|
|
29 |
|
|
|
30 |
from django.conf import settings |
|
|
31 |
from django.template import Token, TOKEN_TEXT, TemplateSyntaxError |
|
|
32 |
from django.utils.html import strip_spaces_between_tags |
|
|
33 |
|
|
|
34 |
from djblets.util.testing import TestCase, TagTest |
|
|
35 |
from djblets.util.templatetags import djblets_deco |
|
|
36 |
from djblets.util.templatetags import djblets_email |
|
|
37 |
from djblets.util.templatetags import djblets_utils |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
def normalize_html(s): |
|
|
41 |
return strip_spaces_between_tags(s).strip() |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
class BoxTest(TagTest): |
|
|
45 |
def testPlain(self): |
|
|
46 |
"""Testing box tag""" |
|
|
47 |
node = djblets_deco.box(self.parser, Token(TOKEN_TEXT, 'box')) |
|
|
48 |
context = {} |
|
|
49 |
|
|
|
50 |
self.assertEqual(normalize_html(node.render(context)), |
|
|
51 |
'<div class="box-container"><div class="box">' + |
|
|
52 |
'<div class="box-inner">\ncontent\n ' + |
|
|
53 |
'</div></div></div>') |
|
|
54 |
|
|
|
55 |
def testClass(self): |
|
|
56 |
"""Testing box tag (with extra class)""" |
|
|
57 |
node = djblets_deco.box(self.parser, Token(TOKEN_TEXT, 'box "class"')) |
|
|
58 |
context = {} |
|
|
59 |
|
|
|
60 |
self.assertEqual(normalize_html(node.render(context)), |
|
|
61 |
'<div class="box-container"><div class="box class">' + |
|
|
62 |
'<div class="box-inner">\ncontent\n ' + |
|
|
63 |
'</div></div></div>') |
|
|
64 |
|
|
|
65 |
def testError(self): |
|
|
66 |
"""Testing box tag (invalid usage)""" |
|
|
67 |
self.assertRaises(TemplateSyntaxError, |
|
|
68 |
lambda: djblets_deco.box(self.parser, |
|
|
69 |
Token(TOKEN_TEXT, |
|
|
70 |
'box "class" "foo"'))) |
|
|
71 |
|
|
|
72 |
|
|
|
73 |
class ErrorBoxTest(TagTest): |
|
|
74 |
def testPlain(self): |
|
|
75 |
"""Testing errorbox tag""" |
|
|
76 |
node = djblets_deco.errorbox(self.parser, |
|
|
77 |
Token(TOKEN_TEXT, 'errorbox')) |
|
|
78 |
|
|
|
79 |
context = {} |
|
|
80 |
|
|
|
81 |
self.assertEqual(normalize_html(node.render(context)), |
|
|
82 |
'<div class="errorbox">\ncontent\n</div>') |
|
|
83 |
|
|
|
84 |
def testId(self): |
|
|
85 |
"""Testing errorbox tag (with id)""" |
|
|
86 |
node = djblets_deco.errorbox(self.parser, |
|
|
87 |
Token(TOKEN_TEXT, 'errorbox "id"')) |
|
|
88 |
|
|
|
89 |
context = {} |
|
|
90 |
|
|
|
91 |
self.assertEqual(normalize_html(node.render(context)), |
|
|
92 |
'<div class="errorbox" id="id">\ncontent\n</div>') |
|
|
93 |
|
|
|
94 |
|
|
|
95 |
def testError(self): |
|
|
96 |
"""Testing errorbox tag (invalid usage)""" |
|
|
97 |
self.assertRaises(TemplateSyntaxError, |
|
|
98 |
lambda: djblets_deco.errorbox(self.parser, |
|
|
99 |
Token(TOKEN_TEXT, |
|
|
100 |
'errorbox "id" ' + |
|
|
101 |
'"foo"'))) |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
class AgeIdTest(TagTest): |
|
|
105 |
def setUp(self): |
|
|
106 |
TagTest.setUp(self) |
|
|
107 |
|
|
|
108 |
self.now = datetime.datetime.now() |
|
|
109 |
|
|
|
110 |
self.context = { |
|
|
111 |
'now': self.now, |
|
|
112 |
'minus1': self.now - datetime.timedelta(1), |
|
|
113 |
'minus2': self.now - datetime.timedelta(2), |
|
|
114 |
'minus3': self.now - datetime.timedelta(3), |
|
|
115 |
'minus4': self.now - datetime.timedelta(4), |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
def testNow(self): |
|
|
119 |
"""Testing ageid tag (now)""" |
|
|
120 |
self.assertEqual(djblets_utils.ageid(self.now), 'age1') |
|
|
121 |
|
|
|
122 |
def testMinus1(self): |
|
|
123 |
"""Testing ageid tag (yesterday)""" |
|
|
124 |
self.assertEqual(djblets_utils.ageid(self.now - datetime.timedelta(1)), |
|
|
125 |
'age2') |
|
|
126 |
|
|
|
127 |
def testMinus2(self): |
|
|
128 |
"""Testing ageid tag (two days ago)""" |
|
|
129 |
self.assertEqual(djblets_utils.ageid(self.now - datetime.timedelta(2)), |
|
|
130 |
'age3') |
|
|
131 |
|
|
|
132 |
def testMinus3(self): |
|
|
133 |
"""Testing ageid tag (three days ago)""" |
|
|
134 |
self.assertEqual(djblets_utils.ageid(self.now - datetime.timedelta(3)), |
|
|
135 |
'age4') |
|
|
136 |
|
|
|
137 |
def testMinus4(self): |
|
|
138 |
"""Testing ageid tag (four days ago)""" |
|
|
139 |
self.assertEqual(djblets_utils.ageid(self.now - datetime.timedelta(4)), |
|
|
140 |
'age5') |
|
|
141 |
|
|
|
142 |
def testNotDateTime(self): |
|
|
143 |
"""Testing ageid tag (non-datetime object)""" |
|
|
144 |
class Foo: |
|
|
145 |
def __init__(self, now): |
|
|
146 |
self.day = now.day |
|
|
147 |
self.month = now.month |
|
|
148 |
self.year = now.year |
|
|
149 |
|
|
|
150 |
self.assertEqual(djblets_utils.ageid(Foo(self.now)), 'age1') |
|
|
151 |
|
|
|
152 |
|
|
|
153 |
class TestEscapeSpaces(unittest.TestCase): |
|
|
154 |
def test(self): |
|
|
155 |
"""Testing escapespaces filter""" |
|
|
156 |
self.assertEqual(djblets_utils.escapespaces('Hi there'), |
|
|
157 |
'Hi there') |
|
|
158 |
self.assertEqual(djblets_utils.escapespaces('Hi there'), |
|
|
159 |
'Hi there') |
|
|
160 |
self.assertEqual(djblets_utils.escapespaces('Hi there\n'), |
|
|
161 |
'Hi there<br />') |
|
|
162 |
|
|
|
163 |
|
|
|
164 |
class TestHumanizeList(unittest.TestCase): |
|
|
165 |
def test0(self): |
|
|
166 |
"""Testing humanize_list filter (length 0)""" |
|
|
167 |
self.assertEqual(djblets_utils.humanize_list([]), '') |
|
|
168 |
|
|
|
169 |
def test1(self): |
|
|
170 |
"""Testing humanize_list filter (length 1)""" |
|
|
171 |
self.assertEqual(djblets_utils.humanize_list(['a']), 'a') |
|
|
172 |
|
|
|
173 |
def test2(self): |
|
|
174 |
"""Testing humanize_list filter (length 2)""" |
|
|
175 |
self.assertEqual(djblets_utils.humanize_list(['a', 'b']), 'a and b') |
|
|
176 |
|
|
|
177 |
def test3(self): |
|
|
178 |
"""Testing humanize_list filter (length 3)""" |
|
|
179 |
self.assertEqual(djblets_utils.humanize_list(['a', 'b', 'c']), |
|
|
180 |
'a, b and c') |
|
|
181 |
|
|
|
182 |
def test4(self): |
|
|
183 |
"""Testing humanize_list filter (length 4)""" |
|
|
184 |
self.assertEqual(djblets_utils.humanize_list(['a', 'b', 'c', 'd']), |
|
|
185 |
'a, b, c, and d') |
|
|
186 |
|
|
|
187 |
|
|
|
188 |
class TestIndent(unittest.TestCase): |
|
|
189 |
def test(self): |
|
|
190 |
"""Testing indent filter""" |
|
|
191 |
self.assertEqual(djblets_utils.indent('foo'), ' foo') |
|
|
192 |
self.assertEqual(djblets_utils.indent('foo', 3), ' foo') |
|
|
193 |
self.assertEqual(djblets_utils.indent('foo\nbar'), ' foo\n bar') |
|
|
194 |
|
|
|
195 |
|
|
|
196 |
class QuotedEmailTagTest(TagTest): |
|
|
197 |
def testInvalid(self): |
|
|
198 |
"""Testing quoted_email tag (invalid usage)""" |
|
|
199 |
self.assertRaises(TemplateSyntaxError, |
|
|
200 |
lambda: djblets_email.quoted_email(self.parser, |
|
|
201 |
Token(TOKEN_TEXT, 'quoted_email'))) |
|
|
202 |
|
|
|
203 |
|
|
|
204 |
class CondenseTagTest(TagTest): |
|
|
205 |
def getContentText(self): |
|
|
206 |
return "foo\nbar\n\n\n\n\n\n\nfoobar!" |
|
|
207 |
|
|
|
208 |
def testPlain(self): |
|
|
209 |
"""Testing condense tag""" |
|
|
210 |
node = djblets_email.condense(self.parser, |
|
|
211 |
Token(TOKEN_TEXT, 'condense')) |
|
|
212 |
self.assertEqual(node.render({}), "foo\nbar\n\n\nfoobar!") |
|
|
213 |
|
|
|
214 |
|
|
|
215 |
class QuoteTextFilterTest(unittest.TestCase): |
|
|
216 |
def testPlain(self): |
|
|
217 |
"""Testing quote_text filter (default level)""" |
|
|
218 |
self.assertEqual(djblets_email.quote_text("foo\nbar"), |
|
|
219 |
"> foo\n> bar") |
|
|
220 |
|
|
|
221 |
def testLevel2(self): |
|
|
222 |
"""Testing quote_text filter (level 2)""" |
|
|
223 |
self.assertEqual(djblets_email.quote_text("foo\nbar", 2), |
|
|
224 |
"> > foo\n> > bar") |