Commits

Mike Orr committed e9da35e

Tests for webhelpers.text

  • Participants
  • Parent commits f4861f6

Comments (0)

Files changed (2)

File tests/test_text.py

 from util import WebHelpersTestCase
 import unittest
 
+from nose.tools import eq_
+
 from webhelpers.text import *
 
 class TestTextHelper(WebHelpersTestCase):
     def test_truncate(self):
         self.assertEqual("Hello World!", truncate("Hello World!", 12))
         self.assertEqual("Hello Wor...", truncate("Hello World!!", 12))
+        self.assertEqual("Hello...", truncate("Hello World!!", 12, whole_word=True))
 
+    def test_strip_leading_whitespace(self):
+        s = "    def fn(x):\n        return x\n"
+        control = "def fn(x):\nreturn x\n"
+        eq_(control, strip_leading_whitespace(s))
 
-if __name__ == '__main__':
-    suite = [unittest.makeSuite(TestTextHelper)]
-    for testsuite in suite:
-        unittest.TextTestRunner(verbosity=1).run(testsuite)
+    # @@MO wrap_paragraphs untested.

File webhelpers/text.py

 import re
 import textwrap
 
-__all__ = ["truncate", "excerpt"]
+__all__ = [
+    "truncate", 
+    "excerpt",
+    "plural",
+    "chop_at",
+    "lchop",
+    "rchop",
+    "strip_leading_whitespace",
+    "wrap_paragraphs",
+    ]
 
 def truncate(text, length=30, indicator='...', whole_word=False):
     """Truncate ``text`` with replacement characters.
         i -= 1
     while i >= 0 and text[i].isspace():
         i -= 1
-    if i < short_length:
-        i += 1   # Set to one after the last char we want to keep.
+    #if i < short_length:
+    #    i += 1   # Set to one after the last char we want to keep.
     if i <= 0:
         # Entire text before break is one word, or we miscalculated.
         return text[:short_length] + indicator
     """Truncate string ``s`` at the first occurence of ``sub``.
 
     If ``inclusive`` is true, truncate just after ``sub`` rather than at it.
+
+    >>> chop_at("plutocratic brats", "rat")
+    'plutoc'
+    >>> chop_at("plutocratic brats", "rat", True)
+    'plutocrat'
     """
     pos = s.find(sub)
     if pos == -1:
     return s[:pos]
 
 def lchop(s, sub):
-    """Chop ``sub`` off the front of ``s`` if present."""
+    """Chop ``sub`` off the front of ``s`` if present.
+    
+    >>> lchop("##This is a comment.##", "##")
+    'This is a comment.##'
+    """
     if s.startswith(sub):
         s = s[len(sub):]
     return s
     
 def rchop(s, sub):
-    """Chop ``sub`` off the end of ``s`` if present."""
+    """Chop ``sub`` off the end of ``s`` if present.
+    
+    >>> rchop("##This is a comment.##", "##")
+    '##This is a comment.'
+    """
     if s.endswith(sub):
         s = s[:-len(sub)]
     return s