DocsTest.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2009 Jason Stitt
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. import unittest
  22. from tidylib import tidy_document
  23. DOC = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  24. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  25. <html xmlns="http://www.w3.org/1999/xhtml">
  26. <head>
  27. <title></title>
  28. </head>
  29. <body>
  30. %s
  31. </body>
  32. </html>
  33. '''
  34. class TestDocs1(unittest.TestCase):
  35. """ Test some sample documents """
  36. def test_doc_with_unclosed_tag(self):
  37. h = "<p>hello"
  38. expected = DOC % '''<p>
  39. hello
  40. </p>'''
  41. doc, err = tidy_document(h)
  42. self.assertEqual(doc, expected)
  43. def test_doc_with_incomplete_img_tag(self):
  44. h = "<img src='foo'>"
  45. expected = DOC % '''<img src='foo' alt="" />'''
  46. doc, err = tidy_document(h)
  47. self.assertEqual(doc, expected)
  48. def test_doc_with_entity(self):
  49. h = "&eacute;"
  50. expected = DOC % "&eacute;"
  51. doc, err = tidy_document(h)
  52. self.assertEqual(doc, expected)
  53. expected = DOC % "&#233;"
  54. doc, err = tidy_document(h, {'numeric-entities':1})
  55. self.assertEqual(doc, expected)
  56. def test_doc_with_unicode(self):
  57. h = u"unicode string ß"
  58. expected = unicode(DOC, 'utf-8') % h
  59. doc, err = tidy_document(h)
  60. self.assertEqual(doc, expected)
  61. def test_doc_with_unicode_subclass(self):
  62. class MyUnicode(unicode):
  63. pass
  64. h = MyUnicode(u"unicode string ß")
  65. expected = unicode(DOC, 'utf-8') % h
  66. doc, err = tidy_document(h)
  67. self.assertEqual(doc, expected)
  68. if __name__ == '__main__':
  69. unittest.main()