Best Python code snippet using pyresttest_python
test_match.py
Source:test_match.py  
2from rpython.rlib.rsre import rsre_char, rsre_constants3from rpython.rlib.rsre.rpy import get_code, VERSION4from rpython.rlib.rsre.test.support import match, fullmatch, Position as P5def get_code_and_re(regexp):6    return get_code(regexp), re.compile(regexp)7def test_get_code_repetition():8    c1 = get_code(r"a+")9    c2 = get_code(r"a+")10    assert c1.pattern == c2.pattern11class TestMatch:12    def test_or(self):13        r = get_code(r"a|bc|def")14        assert match(r, "a")15        assert match(r, "bc")16        assert match(r, "def")17        assert not match(r, "ghij")18    def test_any(self):19        r = get_code(r"ab.cd")20        assert match(r, "abXcdef")21        assert not match(r, "ab\ncdef")22        assert not match(r, "abXcDef")23    def test_any_repetition(self):24        r = get_code(r"ab.*cd")25        assert match(r, "abXXXXcdef")26        assert match(r, "abcdef")27        assert not match(r, "abX\nXcdef")28        assert not match(r, "abXXXXcDef")29    def test_any_all(self):30        r = get_code(r"(?s)ab.cd")31        assert match(r, "abXcdef")32        assert match(r, "ab\ncdef")33        assert not match(r, "ab\ncDef")34    def test_any_all_repetition(self):35        r = get_code(r"(?s)ab.*cd")36        assert match(r, "abXXXXcdef")37        assert match(r, "abcdef")38        assert match(r, "abX\nXcdef")39        assert not match(r, "abX\nXcDef")40    def test_assert(self):41        r = get_code(r"abc(?=def)(.)")42        res = match(r, "abcdefghi")43        assert res is not None and res.get_mark(1) == P(4)44        assert not match(r, "abcdeFghi")45    def test_assert_not(self):46        r = get_code(r"abc(?!def)(.)")47        res = match(r, "abcdeFghi")48        assert res is not None and res.get_mark(1) == P(4)49        assert not match(r, "abcdefghi")50    def test_lookbehind(self):51        r = get_code(r"([a-z]*)(?<=de)")52        assert match(r, "ade")53        res = match(r, "adefg")54        assert res is not None and res.get_mark(1) == P(3)55        assert not match(r, "abc")56        assert not match(r, "X")57        assert not match(r, "eX")58    def test_negative_lookbehind(self):59        def found(s):60            res = match(r, s)61            assert res is not None62            return res.get_mark(1)63        r = get_code(r"([a-z]*)(?<!dd)")64        assert found("ade") == P(3)65        assert found("adefg") == P(5)66        assert found("abcdd") == P(4)67        assert found("abddd") == P(3)68        assert found("adddd") == P(2)69        assert found("ddddd") == P(1)70        assert found("abXde") == P(2)71    def test_at(self):72        r = get_code(r"abc$")73        assert match(r, "abc")74        assert not match(r, "abcd")75        assert not match(r, "ab")76    def test_repeated_set(self):77        r = get_code(r"[a0x]+f")78        assert match(r, "a0af")79        assert not match(r, "a0yaf")80    def test_category(self):81        r = get_code(r"[\sx]")82        assert match(r, "x")83        assert match(r, " ")84        assert not match(r, "n")85    def test_groupref(self):86        r = get_code(r"(xx+)\1+$")     # match non-prime numbers of x87        assert not match(r, "xx")88        assert not match(r, "xxx")89        assert     match(r, "xxxx")90        assert not match(r, "xxxxx")91        assert     match(r, "xxxxxx")92        assert not match(r, "xxxxxxx")93        assert     match(r, "xxxxxxxx")94        assert     match(r, "xxxxxxxxx")95    def test_groupref_ignore(self):96        r = get_code(r"(?i)(xx+)\1+$")     # match non-prime numbers of x97        assert not match(r, "xX")98        assert not match(r, "xxX")99        assert     match(r, "Xxxx")100        assert not match(r, "xxxXx")101        assert     match(r, "xXxxxx")102        assert not match(r, "xxxXxxx")103        assert     match(r, "xxxxxxXx")104        assert     match(r, "xxxXxxxxx")105    def test_groupref_exists(self):106        r = get_code(r"((a)|(b))c(?(2)d)$")107        assert not match(r, "ac")108        assert     match(r, "acd")109        assert     match(r, "bc")110        assert not match(r, "bcd")111        #112        r = get_code(r"((a)|(b))c(?(2)d|e)$")113        assert not match(r, "ac")114        assert     match(r, "acd")115        assert not match(r, "ace")116        assert not match(r, "bc")117        assert not match(r, "bcd")118        assert     match(r, "bce")119    def test_in_ignore(self):120        r = get_code(r"(?i)[a-f]")121        assert match(r, "b")122        assert match(r, "C")123        assert not match(r, "g")124        r = get_code(r"(?i)[a-f]+$")125        assert match(r, "bCdEf")126        assert not match(r, "g")127        assert not match(r, "aaagaaa")128    def test_not_literal(self):129        r = get_code(r"[^a]")130        assert match(r, "A")131        assert not match(r, "a")132        r = get_code(r"[^a]+$")133        assert match(r, "Bx123")134        assert not match(r, "--a--")135    def test_not_literal_ignore(self):136        r = get_code(r"(?i)[^a]")137        assert match(r, "G")138        assert not match(r, "a")139        assert not match(r, "A")140        r = get_code(r"(?i)[^a]+$")141        assert match(r, "Gx123")142        assert not match(r, "--A--")143    def test_repeated_single_character_pattern(self):144        r = get_code(r"foo(?:(?<=foo)x)+$")145        assert match(r, "foox")146    def test_flatten_marks(self):147        r = get_code(r"a(b)c((d)(e))+$")148        res = match(r, "abcdedede")149        assert res.flatten_marks() == map(P, [0, 9, 1, 2, 7, 9, 7, 8, 8, 9])150        assert res.flatten_marks() == map(P, [0, 9, 1, 2, 7, 9, 7, 8, 8, 9])151    def test_bug1(self):152        # REPEAT_ONE inside REPEAT153        r = get_code(r"(?:.+)?B")154        assert match(r, "AB") is not None155        r = get_code(r"(?:AA+?)+B")156        assert match(r, "AAAB") is not None157        r = get_code(r"(?:AA+)+?B")158        assert match(r, "AAAB") is not None159        r = get_code(r"(?:AA+?)+?B")160        assert match(r, "AAAB") is not None161        # REPEAT inside REPEAT162        r = get_code(r"(?:(?:xy)+)?B")163        assert match(r, "xyB") is not None164        r = get_code(r"(?:xy(?:xy)+?)+B")165        assert match(r, "xyxyxyB") is not None166        r = get_code(r"(?:xy(?:xy)+)+?B")167        assert match(r, "xyxyxyB") is not None168        r = get_code(r"(?:xy(?:xy)+?)+?B")169        assert match(r, "xyxyxyB") is not None170    def test_assert_group(self):171        r = get_code(r"abc(?=(..)f)(.)")172        res = match(r, "abcdefghi")173        assert res is not None174        assert res.span(2) == (P(3), P(4))175        assert res.span(1) == (P(3), P(5))176    def test_assert_not_group(self):177        r = get_code(r"abc(?!(de)f)(.)")178        res = match(r, "abcdeFghi")179        assert res is not None180        assert res.span(2) == (P(3), P(4))181        # this I definitely classify as Horrendously Implementation Dependent.182        # CPython answers (3, 5).183        assert res.span(1) == (-1, -1)184    def test_match_start(self):185        r = get_code(r"^ab")186        assert     match(r, "abc")187        assert not match(r, "xxxabc", start=3)188        assert not match(r, "xx\nabc", start=3)189        #190        r = get_code(r"(?m)^ab")191        assert     match(r, "abc")192        assert not match(r, "xxxabc", start=3)193        assert     match(r, "xx\nabc", start=3)194    def test_match_end(self):195        r = get_code("ab")196        assert     match(r, "abc")197        assert     match(r, "abc", end=333)198        assert     match(r, "abc", end=3)199        assert     match(r, "abc", end=2)200        assert not match(r, "abc", end=1)201        assert not match(r, "abc", end=0)202        assert not match(r, "abc", end=-1)203    def test_match_bug1(self):204        r = get_code(r'(x??)?$')205        assert match(r, "x")206    def test_match_bug2(self):207        r = get_code(r'(x??)??$')208        assert match(r, "x")209    def test_match_bug3(self):210        if VERSION == "2.7.5":211            py.test.skip("pattern fails to compile with exactly 2.7.5 "212                         "(works on 2.7.3 and on 2.7.trunk though)")213        r = get_code(r'([ax]*?x*)?$')214        assert match(r, "aaxaa")215    def test_bigcharset(self):216        for i in range(100):217            chars = [unichr(random.randrange(0x100, 0xD000))218                         for n in range(random.randrange(1, 25))]219            pattern = u'[%s]' % (u''.join(chars),)220            r = get_code(pattern)221            for c in chars:222                assert match(r, c)223            for i in range(200):224                c = unichr(random.randrange(0x0, 0xD000))225                res = match(r, c)226                if c in chars:227                    assert res is not None228                else:229                    assert res is None230    def test_simple_match_1(self):231        r = get_code(r"ab*bbbbbbbc")232        print r233        m = match(r, "abbbbbbbbbcdef")234        assert m235        assert m.match_end == P(11)236    def test_empty_maxuntil(self):237        r = get_code("\\{\\{((?:.*?)+)\\}\\}")238        m = match(r, "{{a}}{{b}}")239        assert m.group(1) == "a"240    def test_fullmatch_1(self):241        r = get_code(r"ab*c")242        assert not fullmatch(r, "abbbcdef")243        assert fullmatch(r, "abbbc")244    def test_fullmatch_2(self):245        r = get_code(r"a(b*?)")246        match = fullmatch(r, "abbb")247        assert match.group(1) == "bbb"248        assert not fullmatch(r, "abbbc")249    def test_fullmatch_3(self):250        r = get_code(r"a((bp)*?)c")251        match = fullmatch(r, "abpbpbpc")252        assert match.group(1) == "bpbpbp"253    def test_fullmatch_4(self):254        r = get_code(r"a((bp)*)c")255        match = fullmatch(r, "abpbpbpc")256        assert match.group(1) == "bpbpbp"257    def test_fullmatch_assertion(self):258        r = get_code(r"(?=a).b")259        assert fullmatch(r, "ab")260        r = get_code(r"(?!a)..")261        assert not fullmatch(r, "ab")262    def test_range_ignore(self):263        from rpython.rlib.unicodedata import unicodedb264        rsre_char.set_unicode_db(unicodedb)265        #266        r = get_code(u"[\U00010428-\U0001044f]", re.I)267        assert r.pattern.count(rsre_constants.OPCODE_RANGE) == 1268        if rsre_constants.V37:269            repl = rsre_constants.OPCODE37_RANGE_UNI_IGNORE270        else:271            repl = rsre_constants.OPCODE27_RANGE_IGNORE272        r.pattern[r.pattern.index(rsre_constants.OPCODE_RANGE)] = repl...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
