Skip to content

Commit b67e4aa

Browse files
committed
Add tests
1 parent a759b01 commit b67e4aa

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

‎Lib/test/test_textwrap.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
# $Id$
99
#
1010

11+
import re
1112
import unittest
1213

1314
from textwrap import TextWrapper, wrap, fill, dedent, indent, shorten
15+
from textwrap import _cached_regex as cached_regex
1416

1517

1618
class BaseTestCase(unittest.TestCase):
@@ -712,6 +714,44 @@ def test_do_not_break_long_words_or_on_hyphens(self):
712714
'ng_option_', 'indeed-', 'good-bye"']
713715
self.check_wrap(self.text2, 10, expected)
714716

717+
718+
class TextWrapperCachedRegexTestCase(BaseTestCase):
719+
def test_attr_access(self):
720+
wrapper = TextWrapper()
721+
# these names are not part of the public interface,
722+
# but are not prefixed with an underscore.
723+
for attr in 'wordsep_re', 'wordsep_simple_re', 'sentence_end_re':
724+
self.assertTrue(hasattr(wrapper, attr))
725+
self.assertIsInstance(getattr(wrapper, attr), re.Pattern)
726+
self.assertIsInstance(getattr(TextWrapper, attr), re.Pattern)
727+
728+
setattr(wrapper, attr, attr)
729+
self.assertEqual(getattr(wrapper, attr), attr)
730+
self.assertIsInstance(getattr(TextWrapper, attr), re.Pattern)
731+
732+
def test_cached_regex(self):
733+
class Spam:
734+
pat1 = cached_regex('pat1')
735+
pat2 = cached_regex('pat2')
736+
737+
# both patterns are instances of cached_regex
738+
self.assertIsInstance(Spam.__dict__['pat1'], cached_regex)
739+
self.assertIsInstance(Spam.__dict__['pat2'], cached_regex)
740+
741+
# the attribute is replaced with a compiled pattern when accessed
742+
self.assertEqual(Spam.pat1, re.compile('pat1'))
743+
self.assertEqual(Spam.__dict__['pat1'], re.compile('pat1'))
744+
745+
# including when accessed from an instance
746+
spam = Spam()
747+
self.assertEqual(spam.__dict__, {})
748+
self.assertIsInstance(spam.__class__.__dict__['pat2'], cached_regex)
749+
self.assertEqual(spam.pat2, re.compile('pat2'))
750+
self.assertEqual(Spam.pat2, re.compile('pat2'))
751+
self.assertEqual(spam.__class__.__dict__['pat2'], re.compile('pat2'))
752+
self.assertIs(spam.pat2, Spam.pat2)
753+
754+
715755
class IndentTestCases(BaseTestCase):
716756

717757
# called before each test method

0 commit comments

Comments
 (0)