#!/usr/bin/python # Copyright (c) 2015 Eric Entzel # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import htmltruncate import unittest class TruncateTest(unittest.TestCase): cases = ( ('this word is bolded', 4, "this"), ('this word is bolded', 6, "this w"), ('this word is bolded', 8, "this wor"), ('this word is bolded', 10, "this word "), ('this word is bolded', 700, "this word is bolded"), ('the second tag is closed, but is the first one closed too?', 52, 'the second tag is closed, but is the first one close'), ("This is a test of the truncating feature in EDCZ™ please use with caution.", 65, "This is a test of the truncating feature in EDCZ™ please use with"), ("

Well here's another test of truncation with a little bit o markup and a bunch more stuff

", 65, "

Well here's another test of truncation with a little bit o markup

"), ("This is a test of the truncating feature in EDCZ™ please use with caution.", 65, "This is a test of the truncating feature in EDCZ™ please use with"), ("This is a test of the truncating features in EDCZ please use with caution more with.", 65, "This is a test of the truncating features in EDCZ please use with"), ("This is a test of the truncating features in EDCZ please use with caution", 65, "This is a test of the truncating features in EDCZ please use with"), ("This is a test of the truncating features in EDCZ please use with caution", 65, "This is a test of the truncating features in EDCZ please use with"), ("And this baby right here is the special last line that get's chopped a little shorter", 55, "And this baby right here is the special last line that ") ) def testTruncation(self): for input, count, output in self.cases: self.assertEqual( htmltruncate.truncate(input, count), output ) def testUnbalanced(self): self.assertRaises( htmltruncate.UnbalancedError, htmltruncate.truncate, 'I am a bad little string with unbalanced tags', 20 ) def testEntity(self): self.assertEqual( htmltruncate.truncate( "I'm one", 3 ), "I'm" ) def testSelfClosing(self): self.assertEqual( htmltruncate.truncate( "I need
a break", 11 ), "I need
a br" ) def testEllipsis(self): self.assertEqual( htmltruncate.truncate('this word is bolded', 10, '...' ), "this word ...") def testSurrounding(self): self.assertEqual( htmltruncate.truncate('

this paragraph should be cut in half

', 11, '...' ), "

this paragr...

") def testVoidElements(self): self.assertEqual( htmltruncate.truncate( "I need
a break
one more time.", 17 ), "I need
a break
on" ) if __name__ == "__main__": unittest.main()