tests.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/python
  2. # Copyright (c) 2015 Eric Entzel
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. # The above copyright notice and this permission notice shall be included in all
  10. # copies or substantial portions of the Software.
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. # SOFTWARE.
  18. import htmltruncate
  19. import unittest
  20. class TruncateTest(unittest.TestCase):
  21. cases = ( ('this <b>word</b> is bolded', 4, "this"),
  22. ('this <b>word</b> is bolded', 6, "this <b>w</b>"),
  23. ('this <b>word</b> is bolded', 8, "this <b>wor</b>"),
  24. ('this <b>word</b> is bolded', 10, "this <b>word</b> "),
  25. ('this <b>word</b> is bolded', 700, "this <b>word</b> is bolded"),
  26. ('the second tag <span class="hello">is closed, but is the <a href="/test.html">first one</a> closed too?</span>', 52,
  27. 'the second tag <span class="hello">is closed, but is the <a href="/test.html">first one</a> close</span>'),
  28. ("This is a test of the truncating feature in EDCZ&trade; please use with caution.", 65,
  29. "This is a test of the truncating feature in EDCZ&trade; please use with"),
  30. ("<p>Well here's another test of truncation <span>with</span> a little bit o markup and a bunch more stuff</p>", 65,
  31. "<p>Well here's another test of truncation <span>with</span> a little bit o markup</p>"),
  32. ("This is a test of the truncating feature in EDCZ&trade; please <span>use</span> with caution.", 65,
  33. "This is a test of the truncating feature in EDCZ&trade; please <span>use</span> with"),
  34. ("This is a test of the truncating features in EDCZ please use <span>with</span> caution <span>more with</span>.", 65,
  35. "This is a test of the truncating features in EDCZ please use <span>with</span>"),
  36. ("This is a test of the truncating features in EDCZ please use <span>with caution</span>", 65,
  37. "This is a test of the truncating features in EDCZ please use <span>with</span>"),
  38. ("<span>This</span> is a test of the truncating features in EDCZ please use <span>with caution</span>", 65,
  39. "<span>This</span> is a test of the truncating features in EDCZ please use <span>with</span>"),
  40. ("And this baby right here is the special last line that get's chopped a little shorter", 55,
  41. "And this baby right here is the special last line that ") )
  42. def testTruncation(self):
  43. for input, count, output in self.cases:
  44. self.assertEqual( htmltruncate.truncate(input, count), output )
  45. def testUnbalanced(self):
  46. self.assertRaises( htmltruncate.UnbalancedError, htmltruncate.truncate, 'I am a <b>bad</strong> little string with unbalanced tags', 20 )
  47. def testEntity(self):
  48. self.assertEqual( htmltruncate.truncate( "I&apos;m one", 3 ), "I&apos;m" )
  49. def testSelfClosing(self):
  50. self.assertEqual( htmltruncate.truncate( "I need<br /> a break", 11 ), "I need<br /> a br" )
  51. def testEllipsis(self):
  52. self.assertEqual( htmltruncate.truncate('this <b>word</b> is bolded', 10, '...' ), "this <b>word</b> ...")
  53. def testSurrounding(self):
  54. self.assertEqual( htmltruncate.truncate('<p>this paragraph should be cut in half</p>', 11, '...' ), "<p>this paragr...</p>")
  55. def testVoidElements(self):
  56. self.assertEqual( htmltruncate.truncate( "<a href='http://www.example.com'>I need<br> a break</a> one more time.", 17 ), "<a href='http://www.example.com'>I need<br> a break</a> on" )
  57. if __name__ == "__main__":
  58. unittest.main()