PageRenderTime 223ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/trac/mimeview/tests/api.py

https://github.com/dafrito/trac-mirror
Python | 220 lines | 201 code | 7 blank | 12 comment | 0 complexity | 4ce74dd374a652878361aff96277689f MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2006-2013 Edgewall Software
  4. # All rights reserved.
  5. #
  6. # This software is licensed as described in the file COPYING, which
  7. # you should have received as part of this distribution. The terms
  8. # are also available at http://trac.edgewall.org/wiki/TracLicense.
  9. #
  10. # This software consists of voluntary contributions made by many
  11. # individuals. For the exact contribution history, see the revision
  12. # history and logs, available at http://trac.edgewall.org/log/.
  13. import doctest
  14. import unittest
  15. from StringIO import StringIO
  16. import sys
  17. from trac.core import *
  18. from trac.test import EnvironmentStub
  19. from trac.mimeview import api
  20. from trac.mimeview.api import get_mimetype, IContentConverter, Mimeview, \
  21. _group_lines
  22. from genshi import Stream, Namespace
  23. from genshi.core import Attrs, TEXT, START, END
  24. from genshi.input import HTMLParser
  25. class GetMimeTypeTestCase(unittest.TestCase):
  26. def test_from_suffix_using_MIME_MAP(self):
  27. self.assertEqual('text/plain', get_mimetype('README', None))
  28. self.assertEqual('text/plain', get_mimetype('README.txt', None))
  29. def test_from_suffix_using_mimetypes(self):
  30. accepted = ('image/png', 'image/x-png')
  31. self.assertTrue(get_mimetype('doc/trac_logo.png', None) in accepted)
  32. def test_from_content_using_CONTENT_RE(self):
  33. self.assertEqual('text/x-python',
  34. get_mimetype('xxx', """
  35. #!/usr/bin/python
  36. # This is a python script
  37. """))
  38. self.assertEqual('text/x-python',
  39. get_mimetype('xxx', """
  40. #!/usr/bin/env python
  41. # This is a python script
  42. """))
  43. self.assertEqual('text/x-ksh',
  44. get_mimetype('xxx', """
  45. #!/bin/ksh
  46. # This is a shell script
  47. """))
  48. self.assertEqual('text/x-python',
  49. get_mimetype('xxx', """
  50. # -*- Python -*-
  51. # This is a python script
  52. """))
  53. self.assertEqual('text/x-ruby',
  54. get_mimetype('xxx', """
  55. # -*- mode: ruby -*-
  56. # This is a ruby script
  57. """))
  58. self.assertEqual('text/x-python',
  59. get_mimetype('xxx', ' ' * 2000 + '# vim: ft=python'))
  60. def test_from_content_using_is_binary(self):
  61. self.assertEqual('application/octet-stream',
  62. get_mimetype('xxx', "abc\0xyz"))
  63. class MimeviewTestCase(unittest.TestCase):
  64. def setUp(self):
  65. self.env = EnvironmentStub(default_data=False,
  66. enable=['%s.%s' % (self.__module__, c)
  67. for c in ['Converter0', 'Converter1', 'Converter2']])
  68. def tearDown(self):
  69. pass
  70. def test_get_supported_conversions(self):
  71. class Converter0(Component):
  72. implements(IContentConverter)
  73. def get_supported_conversions(self):
  74. yield 'key0', 'Format 0', 'c0', 'text/x-sample', 'text/html', 8
  75. class Converter2(Component):
  76. implements(IContentConverter)
  77. def get_supported_conversions(self):
  78. yield 'key2', 'Format 2', 'c2', 'text/x-sample', 'text/html', 2
  79. class Converter1(Component):
  80. implements(IContentConverter)
  81. def get_supported_conversions(self):
  82. yield 'key1', 'Format 1', 'c1', 'text/x-sample', 'text/html', 4
  83. mimeview = Mimeview(self.env)
  84. conversions = mimeview.get_supported_conversions('text/x-sample')
  85. self.assertEqual(Converter0(self.env), conversions[0][-1])
  86. self.assertEqual(Converter1(self.env), conversions[1][-1])
  87. self.assertEqual(Converter2(self.env), conversions[2][-1])
  88. class GroupLinesTestCase(unittest.TestCase):
  89. def test_empty_stream(self):
  90. # FIXME: this currently fails
  91. lines = list(_group_lines([]))
  92. self.assertEqual(len(lines), 0)
  93. def test_text_only_stream(self):
  94. input = [(TEXT, "test", (None, -1, -1))]
  95. lines = list(_group_lines(input))
  96. self.assertEquals(len(lines), 1)
  97. self.assertTrue(isinstance(lines[0], Stream))
  98. self.assertEquals(lines[0].events, input)
  99. def test_text_only_stream2(self):
  100. input = [(TEXT, "test\n", (None, -1, -1))]
  101. lines = list(_group_lines(input))
  102. self.assertEquals(len(lines), 1)
  103. self.assertTrue(isinstance(lines[0], Stream))
  104. self.assertEquals(lines[0].events, [(TEXT, "test", (None, -1, -1))])
  105. def test_simplespan(self):
  106. input = HTMLParser(StringIO(u"<span>test</span>"), encoding=None)
  107. lines = list(_group_lines(input))
  108. self.assertEquals(len(lines), 1)
  109. self.assertTrue(isinstance(lines[0], Stream))
  110. for (a, b) in zip(lines[0], input):
  111. self.assertEqual(a, b)
  112. def test_empty_text_stream(self):
  113. """
  114. http://trac.edgewall.org/ticket/4336
  115. """
  116. input = [(TEXT, "", (None, -1, -1))]
  117. lines = list(_group_lines(input))
  118. self.assertEquals(len(lines), 0)
  119. def test_newline_stream(self):
  120. input = [(TEXT, "\n", (None, -1, -1))]
  121. lines = list(_group_lines(input))
  122. self.assertEquals(len(lines), 1)
  123. def test_newline_stream2(self):
  124. input = [(TEXT, "\n\n\n", (None, -1, -1))]
  125. lines = list(_group_lines(input))
  126. self.assertEquals(len(lines), 3)
  127. def test_empty_text_in_span(self):
  128. """
  129. http://trac.edgewall.org/ticket/4336
  130. """
  131. ns = Namespace('http://www.w3.org/1999/xhtml')
  132. input = [(START, (ns.span, Attrs([])), (None, -1, -1)),
  133. (TEXT, "", (None, -1, -1)),
  134. (END, ns.span, (None, -1, -1)),
  135. ]
  136. lines = list(_group_lines(input))
  137. self.assertEqual(len(lines), 0)
  138. def test_newline(self):
  139. """
  140. If the text element does not end with a newline, it's not properly
  141. closed.
  142. """
  143. input = HTMLParser(StringIO(u'<span class="c">a\nb</span>'),
  144. encoding=None)
  145. expected = ['<span class="c">a</span>',
  146. '<span class="c">b</span>',
  147. ]
  148. lines = list(_group_lines(input))
  149. self.assertEquals(len(lines), len(expected))
  150. for a, b in zip(lines, expected):
  151. self.assertEquals(a.render('html'), b)
  152. def test_newline2(self):
  153. """
  154. Same as test_newline above, but make sure it behaves properly wrt
  155. the trailing \\n, especially given it's inside an element.
  156. """
  157. input = HTMLParser(StringIO(u'<span class="c">a\nb\n</span>'),
  158. encoding=None)
  159. expected = ['<span class="c">a</span>',
  160. '<span class="c">b</span>',
  161. ]
  162. lines = list(_group_lines(input))
  163. self.assertEquals(len(lines), len(expected))
  164. for a, b in zip(lines, expected):
  165. self.assertEquals(a.render('html'), b)
  166. def test_multinewline(self):
  167. """
  168. ditto.
  169. """
  170. input = HTMLParser(StringIO(u'<span class="c">\n\n\na</span>'),
  171. encoding=None)
  172. expected = ['<span class="c"></span>',
  173. '<span class="c"></span>',
  174. '<span class="c"></span>',
  175. '<span class="c">a</span>',
  176. ]
  177. lines = list(_group_lines(input))
  178. self.assertEquals(len(lines), len(expected))
  179. for a, b in zip(lines, expected):
  180. self.assertEquals(a.render('html'), b)
  181. def suite():
  182. suite = unittest.TestSuite()
  183. suite.addTest(doctest.DocTestSuite(api))
  184. suite.addTest(unittest.makeSuite(GetMimeTypeTestCase, 'test'))
  185. suite.addTest(unittest.makeSuite(MimeviewTestCase, 'test'))
  186. suite.addTest(unittest.makeSuite(GroupLinesTestCase, 'test'))
  187. return suite
  188. if __name__ == '__main__':
  189. unittest.main(defaultTest='suite')