|
8 | 8 | # $Id$ |
9 | 9 | # |
10 | 10 |
|
| 11 | +import re |
11 | 12 | import unittest |
12 | 13 |
|
13 | 14 | from textwrap import TextWrapper, wrap, fill, dedent, indent, shorten |
| 15 | +from textwrap import _cached_regex as cached_regex |
14 | 16 |
|
15 | 17 |
|
16 | 18 | class BaseTestCase(unittest.TestCase): |
@@ -712,6 +714,44 @@ def test_do_not_break_long_words_or_on_hyphens(self): |
712 | 714 | 'ng_option_', 'indeed-', 'good-bye"'] |
713 | 715 | self.check_wrap(self.text2, 10, expected) |
714 | 716 |
|
| 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 | + |
715 | 755 | class IndentTestCases(BaseTestCase): |
716 | 756 |
|
717 | 757 | # called before each test method |
|
0 commit comments