Package trac :: Package util :: Package tests :: Module presentation

Source Code for Module trac.util.tests.presentation

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (C) 2006-2020 Edgewall Software 
  4  # Copyright (C) 2006 Christopher Lenz <[email protected]> 
  5  # All rights reserved. 
  6  # 
  7  # This software is licensed as described in the file COPYING, which 
  8  # you should have received as part of this distribution. The terms 
  9  # are also available at https://trac.edgewall.org/wiki/TracLicense. 
 10  # 
 11  # This software consists of voluntary contributions made by many 
 12  # individuals. For the exact contribution history, see the revision 
 13  # history and logs, available at https://trac.edgewall.org/log/. 
 14   
 15  import datetime 
 16  import doctest 
 17  import unittest 
 18   
 19  from trac.core import TracError 
 20  from trac.test import Mock 
 21  from trac.util import datefmt, presentation 
 22   
 23   
24 -class FiltersTestCase(unittest.TestCase):
25 - def test_htmlattr(self):
26 self.assertEqual( 27 (' autocomplete="on" checked="checked" class="my list"' 28 ' id="list-42"' 29 ' style="border-radius: 3px; background: #f7f7f7"'), 30 presentation.htmlattr_filter( 31 Mock(autoescape=False), 32 {'class': {'my': 1, 'list': True, 'empty': False}, 33 'missing': None, 'checked': 1, 'selected': False, 34 'autocomplete': True, 'id': 'list-%d' % 42, 35 'style': {'border-radius': '3px', 36 'background': '#f7f7f7'}}))
37
38 -class ToJsonTestCase(unittest.TestCase):
39
40 - def test_simple_types(self):
41 self.assertEqual('42', presentation.to_json(42)) 42 self.assertEqual('123.456', presentation.to_json(123.456)) 43 self.assertEqual('true', presentation.to_json(True)) 44 self.assertEqual('false', presentation.to_json(False)) 45 self.assertEqual('null', presentation.to_json(None)) 46 self.assertEqual('"String"', presentation.to_json('String')) 47 self.assertEqual('1551895815012345', 48 presentation.to_json(datetime.datetime( 49 2019, 3, 6, 18, 10, 15, 12345, datefmt.utc))) 50 self.assertEqual('1551895815012345', 51 presentation.to_json(datetime.datetime( 52 2019, 3, 6, 18, 10, 15, 12345))) 53 self.assertEqual(r'"a \" quote"', presentation.to_json('a " quote')) 54 self.assertEqual('''"a ' single quote"''', 55 presentation.to_json("a ' single quote")) 56 self.assertEqual(r'"\u003cb\u003e\u0026\u003c/b\u003e"', 57 presentation.to_json('<b>&</b>')) 58 self.assertEqual(r'"\n\r\u2028\u2029"', 59 presentation.to_json(u'\x0a\x0d\u2028\u2029'))
60
61 - def test_compound_types(self):
62 self.assertEqual('[1,2,[true,false]]', 63 presentation.to_json([1, 2, [True, False]])) 64 self.assertEqual(r'{"one":1,"other":[null,0],' 65 r'''"three":[3,"\u0026\u003c\u003e'"],''' 66 r'"two":2,"\u2028\n":"\u2029\r"}', 67 presentation.to_json({"one": 1, "two": 2, 68 "other": [None, 0], 69 "three": [3, "&<>'"], 70 u"\u2028\x0a": u"\u2029\x0d"}))
71 72
73 -class PaginatorTestCase(unittest.TestCase):
74
75 - def test_paginate(self):
76 """List of objects is paginated.""" 77 items = xrange(20) 78 paginator = presentation.Paginator(items, 1) 79 80 self.assertEqual(1, paginator.page) 81 self.assertEqual(10, paginator.max_per_page) 82 self.assertEqual(20, paginator.num_items) 83 self.assertEqual(2, paginator.num_pages) 84 self.assertEqual((10, 20), paginator.span)
85
87 """Out of range value for page raises a `TracError`.""" 88 items = list(xrange(20)) 89 90 self.assertRaises(TracError, presentation.Paginator, items, 2)
91 92
93 -def test_suite():
94 suite = unittest.TestSuite() 95 suite.addTest(doctest.DocTestSuite(presentation)) 96 suite.addTest(unittest.makeSuite(FiltersTestCase)) 97 suite.addTest(unittest.makeSuite(ToJsonTestCase)) 98 suite.addTest(unittest.makeSuite(PaginatorTestCase)) 99 return suite
100 101 102 if __name__ == '__main__': 103 unittest.main(defaultTest='test_suite') 104