WebHelpers / tests /

# -*- coding: utf-8 -*-
from util import WebHelpersTestCase
import unittest

from import eq_

from webhelpers.text import *

class TestTextHelper(WebHelpersTestCase):
    def test_excerpt(self):
        self.assertEqual("...lo my wo...",
                         excerpt("hello my world", "my", 3))
        self.assertEqual(" a beautiful morn...",
                         excerpt("This is a beautiful morning", "beautiful", 5))
        self.assertEqual("This is a...",
                         excerpt("This is a beautiful morning", "this", 5))
        self.assertEqual("...iful morning",
                         excerpt("This is a beautiful morning", "morning", 5))
                         excerpt("This is a beautiful morning", "day"))

    def test_excerpt_with_regex(self):
        self.assertEqual(' a beautiful! mor...',
                         excerpt('This is a beautiful! morning', 'beautiful', 5))
        self.assertEqual(' a beautiful? mor...',
                         excerpt('This is a beautiful? morning', 'beautiful', 5))

    def test_excerpt_with_utf8(self):
        self.assertEqual(u"...fficiency could not be ...",
                         excerpt(u"That's why efficiency could not be helped", 'could', 8))

    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))

    # @@MO wrap_paragraphs untested.