FragsTest.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_fragment
  23. class TestFrags1(unittest.TestCase):
  24. """ Test some sample fragment documents """
  25. def test_frag_with_unclosed_tag(self):
  26. h = "<p>hello"
  27. expected = '''<p>
  28. hello
  29. </p>'''
  30. doc, err = tidy_fragment(h)
  31. self.assertEqual(doc, expected)
  32. def test_frag_with_incomplete_img_tag(self):
  33. h = "<img src='foo'>"
  34. expected = '''<img src='foo' alt="" />'''
  35. doc, err = tidy_fragment(h)
  36. self.assertEqual(doc, expected)
  37. def test_frag_with_entity(self):
  38. h = "&eacute;"
  39. expected = "&eacute;"
  40. doc, err = tidy_fragment(h)
  41. self.assertEqual(doc, expected)
  42. expected = "&#233;"
  43. doc, err = tidy_fragment(h, {'numeric-entities':1})
  44. self.assertEqual(doc, expected)
  45. def test_frag_with_unicode(self):
  46. h = u"unicode string ß"
  47. expected = h
  48. doc, err = tidy_fragment(h)
  49. self.assertEqual(doc, expected)
  50. def test_frag_with_unicode_subclass(self):
  51. class MyUnicode(unicode):
  52. pass
  53. h = MyUnicode(u"unicode string ß")
  54. expected = h
  55. doc, err = tidy_fragment(h)
  56. self.assertEqual(doc, expected)
  57. if __name__ == '__main__':
  58. unittest.main()