|  | 
|  | 1 | +#!/usr/bin/env python3 | 
|  | 2 | +# | 
|  | 3 | +# Copyright 2020 Google Inc. | 
|  | 4 | +# | 
|  | 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | +# you may not use this file except in compliance with the License. | 
|  | 7 | +# You may obtain a copy of the License at | 
|  | 8 | +# | 
|  | 9 | +#      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +# | 
|  | 11 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | +# See the License for the specific language governing permissions and | 
|  | 15 | +# limitations under the License. | 
|  | 16 | + | 
|  | 17 | +import re | 
|  | 18 | +import sys | 
|  | 19 | +import unittest | 
|  | 20 | + | 
|  | 21 | +import sre_yield | 
|  | 22 | + | 
|  | 23 | +PY36 = sys.version_info[0:2] == (3, 6) | 
|  | 24 | + | 
|  | 25 | +MAX_REPEAT_COUNT = sre_yield.MAX_REPEAT_COUNT | 
|  | 26 | + | 
|  | 27 | +DOT_STAR_ALL_REPR = r"\({repeat base=256 low=0 high=%d}, \d+\)" % MAX_REPEAT_COUNT | 
|  | 28 | + | 
|  | 29 | + | 
|  | 30 | +class ReprYieldTest(unittest.TestCase): | 
|  | 31 | +    """Test that returned objects have effective repr().""" | 
|  | 32 | + | 
|  | 33 | +    def testDotStar(self): | 
|  | 34 | +        parsed = sre_yield.AllStrings(".*", re.DOTALL) | 
|  | 35 | +        out = repr(parsed.raw) | 
|  | 36 | +        self.assertTrue(re.match(r"{combin \[%s\]}" % DOT_STAR_ALL_REPR, out)) | 
|  | 37 | + | 
|  | 38 | +        parsed = sre_yield.AllStrings(".*.*.*", re.DOTALL) | 
|  | 39 | +        out = repr(parsed.raw) | 
|  | 40 | + | 
|  | 41 | +        expected_re = r"{combin \[%s\]}" % ", ".join([DOT_STAR_ALL_REPR] * 3) | 
|  | 42 | +        self.assertTrue(re.match(expected_re, out)) | 
|  | 43 | + | 
|  | 44 | +    def testAlternatives(self): | 
|  | 45 | +        parsed = sre_yield.AllStrings(r"a|b") | 
|  | 46 | +        self.assertEqual( | 
|  | 47 | +            repr(parsed.raw), "{combin [({concat [(['a'], 1), (['b'], 1)]}, 2)]}" | 
|  | 48 | +        ) | 
|  | 49 | +        parsed = sre_yield.AllStrings(r"a||b") | 
|  | 50 | +        self.assertEqual( | 
|  | 51 | +            repr(parsed.raw), | 
|  | 52 | +            "{combin [({concat [({combin [(['a'], 1)]}, 1), ({combin []}, 1), ({combin [(['b'], 1)]}, 1)]}, 3)]}", | 
|  | 53 | +        ) | 
|  | 54 | + | 
|  | 55 | +    def testRepeat(self): | 
|  | 56 | +        parsed = sre_yield.AllStrings(r"\d{1}") | 
|  | 57 | +        self.assertEqual( | 
|  | 58 | +            repr(parsed.raw), "{combin [({repeat base=10 low=1 high=1}, 10)]}" | 
|  | 59 | +        ) | 
|  | 60 | +        parsed = sre_yield.AllStrings(r"\d{2}") | 
|  | 61 | +        self.assertEqual( | 
|  | 62 | +            repr(parsed.raw), "{combin [({repeat base=10 low=2 high=2}, 100)]}" | 
|  | 63 | +        ) | 
|  | 64 | + | 
|  | 65 | +    def testRepeatPlus(self): | 
|  | 66 | +        parsed = sre_yield.AllStrings(r"\d+") | 
|  | 67 | +        out = repr(parsed.raw) | 
|  | 68 | + | 
|  | 69 | +        expected_re = ( | 
|  | 70 | +            r"{combin \[\({repeat base=10 low=1 high=%d}, \d+\)\]}" % MAX_REPEAT_COUNT | 
|  | 71 | +        ) | 
|  | 72 | +        self.assertTrue(re.match(expected_re, out)) | 
|  | 73 | + | 
|  | 74 | +    def testRepeatMulti(self): | 
|  | 75 | +        parsed = sre_yield.AllStrings(r"\d{1} \d{1}") | 
|  | 76 | +        self.assertEqual( | 
|  | 77 | +            repr(parsed.raw), | 
|  | 78 | +            "{combin [({repeat base=10 low=1 high=1}, 10), ([' '], 1), ({repeat base=10 low=1 high=1}, 10)]}", | 
|  | 79 | +        ) | 
|  | 80 | + | 
|  | 81 | +    def testGroup(self): | 
|  | 82 | +        parsed = sre_yield.AllStrings(r"(?:\d{2})") | 
|  | 83 | +        expected = "{combin [({repeat base=10 low=2 high=2}, 100)]}" | 
|  | 84 | +        if PY36: | 
|  | 85 | +            expected = "{combin [(%s, 100)]}" % expected | 
|  | 86 | + | 
|  | 87 | +        self.assertEqual(repr(parsed.raw), expected) | 
|  | 88 | + | 
|  | 89 | +        parsed = sre_yield.AllStrings(r"(?:\d{,2})") | 
|  | 90 | +        expected = "{combin [({repeat base=10 low=0 high=2}, 111)]}" | 
|  | 91 | +        if PY36: | 
|  | 92 | +            expected = "{combin [(%s, 111)]}" % expected | 
|  | 93 | + | 
|  | 94 | +        self.assertEqual(repr(parsed.raw), expected) | 
|  | 95 | + | 
|  | 96 | +    def testBenchInput(self): | 
|  | 97 | +        parsed = sre_yield.AllStrings("[01]{,10}") | 
|  | 98 | +        self.assertEqual( | 
|  | 99 | +            repr(parsed.raw), "{combin [({repeat base=2 low=0 high=10}, 2047)]}" | 
|  | 100 | +        ) | 
|  | 101 | + | 
|  | 102 | +        parsed = sre_yield.AllStrings("(?:[a-z]{,10}){,1000}") | 
|  | 103 | +        out = repr(parsed.raw) | 
|  | 104 | +        expected_re = r"{combin \[\({repeat base=(\d+) low=0 high=1000}, (\d+)\)\]}" | 
|  | 105 | +        m = re.match(expected_re, out) | 
|  | 106 | +        self.assertTrue(m) | 
|  | 107 | +        self.assertEqual(int(m.group(1)), 146813779479511) | 
|  | 108 | + | 
|  | 109 | +        parsed = sre_yield.AllStrings("(?:[a-z]{,100}){,1000}") | 
|  | 110 | +        out = repr(parsed.raw) | 
|  | 111 | +        m = re.match(expected_re, out) | 
|  | 112 | +        self.assertTrue(m) | 
|  | 113 | +        self.assertEqual( | 
|  | 114 | +            int(m.group(1)), | 
|  | 115 | +            3268647867246256383381332100041691484373976788312974266629140102414955744756908184404049903032490380904202638084876187965749304595652472251351, | 
|  | 116 | +        ) | 
|  | 117 | +        self.assertTrue(re.match(expected_re, out)) | 
|  | 118 | + | 
|  | 119 | +    def testBenchInputSlow(self): | 
|  | 120 | +        parsed = sre_yield.AllStrings("(?:[a-z]{,100})") | 
|  | 121 | +        out = repr(parsed.raw) | 
|  | 122 | +        expected_re1 = r"{combin \[\({repeat base=(\d+) low=0 high=100}, (\d+)\)\]}" | 
|  | 123 | +        if PY36: | 
|  | 124 | +            expected_re = r"{combin \[\(%s, (\d+)\)\]}" % expected_re1 | 
|  | 125 | +        else: | 
|  | 126 | +            expected_re = expected_re1 | 
|  | 127 | + | 
|  | 128 | +        m = re.match(expected_re, out) | 
|  | 129 | +        self.assertTrue(m) | 
|  | 130 | +        base1 = m.group(1) | 
|  | 131 | +        repeat1 = m.group(2) | 
|  | 132 | +        self.assertEqual(int(base1), 26) | 
|  | 133 | +        if PY36: | 
|  | 134 | +            self.assertEqual(int(repeat1), int(m.group(3))) | 
|  | 135 | + | 
|  | 136 | +        parsed = sre_yield.AllStrings("(?:(?:[a-z]{,100}){,100}){,100}") | 
|  | 137 | +        out = repr(parsed.raw) | 
|  | 138 | + | 
|  | 139 | +        if PY36: | 
|  | 140 | +            expected_re = expected_re1 | 
|  | 141 | + | 
|  | 142 | +        m = re.match(expected_re, out) | 
|  | 143 | +        self.assertTrue(m) | 
|  | 144 | + | 
|  | 145 | +        base2 = m.group(1) | 
|  | 146 | +        repeat2 = m.group(2) | 
|  | 147 | +        self.assertEqual(len(base2), 14152) | 
|  | 148 | + | 
|  | 149 | +        self.assertGreater(int(base2), int(base1)) | 
|  | 150 | +        self.assertGreater(int(repeat2), int(repeat1)) | 
|  | 151 | + | 
|  | 152 | + | 
|  | 153 | +if __name__ == "__main__": | 
|  | 154 | +    unittest.main() | 
0 commit comments