# Path: blog/utils.py
# def post_truncator(post, terminator):
#     """
#     Truncate a Post text to a page separator.
# 
#     The separator is inserted by TinyMCE ``pagebreak`` plugin
#     that is used to emulate "blog post cut" feature.
# 
#     Only the 1st separator is taken into account, other separators, if any,
#     are ignored.
# 
#     :param post: blog post
#     :param terminator: terminating string or html code (e.g. "Read more..")
#         that is appended to the end of the truncated post.
#     :return: properly terminated truncated post
#     """
#     post_parts = post.content.split(settings.TINYMCE_DEFAULT_CONFIG['pagebreak_separator'])
#     if len(post_parts) > 1:
#         truncated_html = post_parts[0]
#         post_digest = str(BeautifulSoup(truncated_html, 'html.parser'))
#         end_tag = re.search(r'</\w+?>$', post_digest, re.UNICODE | re.IGNORECASE).group(0)
#         if end_tag.lower() == '</p>':
#             return post_digest[:-4] + ' ' + terminator + '</p>'
#         else:
#             return post_digest + '<p>' + terminator + '</p>'
#     else:
#         return post.content


from collections import namedtuple
from unittest import TestCase
from .utils import post_truncator
# coding: utf-8
# Module: tests_utils
# Created on: 10.03.2016
# Author: Roman Miroshnychenko aka Roman V.M. (romanvm@yandex.ua)


FakePost = namedtuple('FakePost', ['content'])


class PostTruncatorTestCase(TestCase):
    def test_non_truncated_post(self):
        content = '<p>Lorem ipsum dolor sit amet.</p>'
        post = FakePost(content)
        result = post_truncator(post, '...')
        self.assertFalse('...' in result)

    def test_truncated_paragraph(self):
        content = '<p>Lorem ipsum dolor<!-- ***Blog Cut*** --> sit amet.</p>'
        post = FakePost(content)
        result = post_truncator(post, '...')
        self.assertTrue('...' in result)
        self.assertEqual(result[-4:], '</p>')

    def test_truncated_pre_block(self):
        content = '<p>Lorem ipsum</p><pre>dolor<!-- ***Blog Cut*** --> sit amet.</pre>'
        post = FakePost(content)

# Path: blog/utils.py
# def post_truncator(post, terminator):
#     """
#     Truncate a Post text to a page separator.
# 
#     The separator is inserted by TinyMCE ``pagebreak`` plugin
#     that is used to emulate "blog post cut" feature.
# 
#     Only the 1st separator is taken into account, other separators, if any,
#     are ignored.
# 
#     :param post: blog post
#     :param terminator: terminating string or html code (e.g. "Read more..")
#         that is appended to the end of the truncated post.
#     :return: properly terminated truncated post
#     """
#     post_parts = post.content.split(settings.TINYMCE_DEFAULT_CONFIG['pagebreak_separator'])
#     if len(post_parts) > 1:
#         truncated_html = post_parts[0]
#         post_digest = str(BeautifulSoup(truncated_html, 'html.parser'))
#         end_tag = re.search(r'</\w+?>$', post_digest, re.UNICODE | re.IGNORECASE).group(0)
#         if end_tag.lower() == '</p>':
#             return post_digest[:-4] + ' ' + terminator + '</p>'
#         else:
#             return post_digest + '<p>' + terminator + '</p>'
#     else:
#         return post.content


# Path: blog/tests_utils.py
from collections import namedtuple
from unittest import TestCase
from .utils import post_truncator

# coding: utf-8
# Module: tests_utils
# Created on: 10.03.2016
# Author: Roman Miroshnychenko aka Roman V.M. (romanvm@yandex.ua)


FakePost = namedtuple('FakePost', ['content'])


class PostTruncatorTestCase(TestCase):
    def test_non_truncated_post(self):
        content = '<p>Lorem ipsum dolor sit amet.</p>'
        post = FakePost(content)
        result = post_truncator(post, '...')
        self.assertFalse('...' in result)

    def test_truncated_paragraph(self):
        content = '<p>Lorem ipsum dolor<!-- ***Blog Cut*** --> sit amet.</p>'
        post = FakePost(content)
        result = post_truncator(post, '...')
        self.assertTrue('...' in result)
        self.assertEqual(result[-4:], '</p>')

    def test_truncated_pre_block(self):
        content = '<p>Lorem ipsum</p><pre>dolor<!-- ***Blog Cut*** --> sit amet.</pre>'
        post = FakePost(content)

        result = post_truncator(post, '...')