1"""
2test case for re module
3"""
4
5import re
6import testsuite
7SUCCEED, FAIL, SYNTAX_ERROR = range(3)
8
9def RAISE():
10    raise("testing failed")
11
12def main():
13    print("begin re tests")
14
15    assert(re.__name__ != None)
16    assert(re.__doc__ != None)
17    assert(re.__file__ != None)
18
19    test_re_obj_search()
20    test_re_obj_match()
21    test_re_mod_search()
22    test_re_mod_match()
23    test_re_obj_split()
24    test_re_mod_split()
25    test_re_obj_findall()
26    test_re_mod_findall()
27    test_mat_obj_groups()
28    test_mat_obj_start()
29    test_mat_obj_end()
30    test_mat_obj_span()
31
32    print("OK: re tests passed")
33
34def test_re_obj_search(verbose = None):
35    """
36    some tests borrowed from cpython
37    testing re.compile(), reobj.search(), and matobj.group()
38    """
39    regex_tests = testsuite.search_regex_tests
40    for t in regex_tests:
41        pattern=s=outcome=repl=expected=None
42        if len(t)==5:
43            pattern, s, outcome, repl, expected = t
44        elif len(t)==3:
45            pattern, s, outcome = t
46        else:
47            raise ('Test tuples should have 3 or 5 fields',t)
48
49        try:
50            obj=re.compile(pattern)
51        except:
52            if outcome==SYNTAX_ERROR: continue    # Expected a syntax error
53            else:
54                # Regex syntax errors aren't yet reported, so for
55                # the official test suite they'll be quietly ignored.
56                pass
57        try:
58            matobj=obj.search(s)
59        except:
60            print('=== Unexpected exception:', obj, matobj, pattern, s)
61            RAISE()
62
63        if outcome==SYNTAX_ERROR:
64            # This should have been a syntax error; forget it.
65            pass
66        elif outcome==FAIL:
67            if matobj==None: pass   # No match, as expected
68            else: print('=== Succeeded incorrectly', obj, matobj, pattern, s)
69        elif outcome==SUCCEED:
70            if matobj!=None:
71                # Matched, as expected, so now we compute the
72                # result string and compare it to our expected result.
73                found=matobj.group(0)
74                repl = repl.replace("found", str(found))
75                for i in range(1,11):
76                    if "g"+str(i) in repl:
77                        gi = str(matobj.group(i))
78                        repl = repl.replace("g"+str(i), gi)
79                if len(t) == 5:
80                    repl = repl.replace('+', '')
81                    repl = repl.replace('\"', '')
82                    if repl!=expected:
83                        print( '=== grouping error', t,
84                                str(repl)+' should be '+str(expected))
85                        RAISE()
86            else:
87                print ('=== Failed incorrectly', t)
88
89def test_re_obj_match(verbose = None):
90    """
91    some tests borrowed from cpython
92    testing re.compile(), reobj.match() and matobj.group()
93    """
94    regex_tests = testsuite.match_regex_tests
95    for t in regex_tests:
96        pattern=s=outcome=repl=expected=None
97        if len(t)==5:
98            pattern, s, outcome, repl, expected = t
99        elif len(t)==3:
100            pattern, s, outcome = t
101        else:
102            raise ('Test tuples should have 3 or 5 fields',t)
103
104        try:
105            obj=re.compile(pattern)
106        except:
107            if outcome==SYNTAX_ERROR: continue    # Expected a syntax error
108            else:
109                # Regex syntax errors aren't yet reported, so for
110                # the official test suite they'll be quietly ignored.
111                pass
112        try:
113            matobj=obj.match(s)
114        except:
115            print('=== Unexpected exception:', obj, matobj, pattern, s)
116
117        if outcome==SYNTAX_ERROR:
118            # This should have been a syntax error; forget it.
119            pass
120        elif outcome==FAIL:
121            if matobj==None: pass   # No match, as expected
122            else: print('=== Succeeded incorrectly', obj, matobj, pattern, s)
123        elif outcome==SUCCEED:
124            if matobj!=None:
125                # Matched, as expected, so now we compute the
126                # result string and compare it to our expected result.
127                found=matobj.group(0)
128                repl = repl.replace("found", str(found))
129                for i in range(1,11):
130                    if "g"+str(i) in repl:
131                        gi = str(matobj.group(i))
132                        repl = repl.replace("g"+str(i), gi)
133                if len(t) == 5:
134                    repl = repl.replace('+', '')
135                    repl = repl.replace('\"', '')
136                    if repl!=expected:
137                        print( '=== grouping error', t,
138                                str(repl)+' should be '+str(expected))
139                        RAISE()
140            else:
141                print ('=== Failed incorrectly', obj, matobj, pattern, s)
142
143def test_re_mod_search(verbose = None):
144    """
145    some tests borrowed from cpython
146    testing re.search(), and matobj.group()
147    """
148    regex_tests = testsuite.search_regex_tests
149    for t in regex_tests:
150        pattern=s=outcome=repl=expected=None
151        if len(t)==5:
152            pattern, s, outcome, repl, expected = t
153        elif len(t)==3:
154            pattern, s, outcome = t
155        else:
156            raise ('Test tuples should have 3 or 5 fields',t)
157
158        try:
159            matobj=re.search(pattern, s)
160        except:
161            if outcome==SYNTAX_ERROR:
162                # This should have been a syntax error; forget it.
163                pass
164            else:
165                print('=== Unexpected exception:', matobj, pattern, s)
166
167        if outcome==FAIL:
168            if matobj==None: pass   # No match, as expected
169            else: print('=== Succeeded incorrectly', obj, matobj, pattern, s)
170        elif outcome==SUCCEED:
171            if matobj!=None:
172                # Matched, as expected, so now we compute the
173                # result string and compare it to our expected result.
174                found=matobj.group(0)
175                repl = repl.replace("found", str(found))
176                for i in range(1,11):
177                    if "g"+str(i) in repl:
178                        gi = str(matobj.group(i))
179                        repl = repl.replace("g"+str(i), gi)
180                if len(t) == 5:
181                    repl = repl.replace('+', '')
182                    repl = repl.replace('\"', '')
183                    if repl!=expected:
184                        print( '=== grouping error', t,
185                                str(repl)+' should be '+str(expected))
186                        RAISE()
187            else:
188                print ('=== Failed incorrectly', t)
189
190def test_re_mod_match(verbose = None):
191    """
192    some tests borrowed from cpython
193    testing re.match(), and matobj.group()
194    """
195    regex_tests = testsuite.match_regex_tests
196    for t in regex_tests:
197        pattern=s=outcome=repl=expected=None
198        if len(t)==5:
199            pattern, s, outcome, repl, expected = t
200        elif len(t)==3:
201            pattern, s, outcome = t
202        else:
203            raise ('Test tuples should have 3 or 5 fields',t)
204
205        try:
206            matobj=re.match(pattern, s)
207        except:
208            if outcome==SYNTAX_ERROR:
209                # This should have been a syntax error; forget it.
210                pass
211            else:
212                print('=== Unexpected exception:', matobj, pattern, s)
213
214        if outcome==FAIL:
215            if matobj==None: pass   # No match, as expected
216            else: print('=== Succeeded incorrectly', matobj, pattern, s)
217        elif outcome==SUCCEED:
218            if matobj!=None:
219                # Matched, as expected, so now we compute the
220                # result string and compare it to our expected result.
221                found=matobj.group(0)
222                repl = repl.replace("found", str(found))
223                for i in range(1,11):
224                    if "g"+str(i) in repl:
225                        gi = str(matobj.group(i))
226                        repl = repl.replace("g"+str(i), gi)
227                if len(t) == 5:
228                    repl = repl.replace('+', '')
229                    repl = repl.replace('\"', '')
230                    if repl!=expected:
231                        print( '=== grouping error', t,
232                                str(repl)+' should be '+str(expected))
233                        RAISE()
234            else:
235                print ('=== Failed incorrectly', t)
236
237def test_re_obj_split(verbose = None):
238    """
239    test re.compile(), and reobj.split()
240    """
241    regex_tests = testsuite.split_regex_tests
242    for t in regex_tests:
243        pattern, s, outcome, maxsplit, fields = t
244        try:
245            reobj = re.compile(pattern)
246        except:
247            if outcome==SYNTAX_ERROR:
248                # This should have been a syntax error; forget it.
249                pass
250            else:
251                print('=== Unexpected exception:', pattern, s,
252                                    outcome, maxsplit, fields)
253        try:
254            fldlst=reobj.split(s, maxsplit)
255        except:
256            if outcome == SYNTAX_ERROR:
257                continue
258            else:
259                print('=== Unexpected exception:', pattern, s,
260                                    outcome, maxsplit, fields)
261
262        if outcome==FAIL:
263            pass    # No match, as expected
264        elif outcome==SUCCEED:
265            if fldlst:
266                # Matched, as expected, so now we compute the
267                # result string and compare it to our expected result.
268                if verbose:
269                    fldstr = fieldstr = ""
270                    for item in fldlst:
271                        fldstr = fldstr + str(item) + " | "
272                    for item in fields:
273                        fieldstr = fieldstr + str(item) + " | "
274                    print(fldstr, "~~~", fieldstr)
275                if len(fields) != len(fldlst):
276                    print('=== Not coherent 1')
277                    RAISE()
278
279                for i in range(len(fields)):
280                    if fields[i] != fldlst[i]:
281                        if verbose:
282                            print('=== Not coherent 2', pattern, s,
283                                    outcome, maxsplit, fields, i,
284                                    fields[i],'(',len(fields[i]),')', ' | ',
285                                    fldlst[i],'(',len(fldlst[i]),')')
286                        else:
287                            print('=== Not coherent 2')
288                        RAISE()
289            else:
290                print ('=== Failed incorrectly', pattern, s,
291                        outcome, maxsplit, fields)
292
293def test_re_mod_split(verbose = None):
294    """
295    test re.split()
296    """
297    regex_tests = testsuite.split_regex_tests
298    for t in regex_tests:
299        pattern, s, outcome, maxsplit, fields = t
300        try:
301            fldlst=re.split(pattern, s, maxsplit)
302        except:
303            if outcome==SYNTAX_ERROR:
304                # This should have been a syntax error; forget it.
305                continue
306            else:
307                print('=== Unexpected exception:', pattern, s,
308                                    outcome, maxsplit, fields)
309
310        if outcome==FAIL:
311            pass    # No match, as expected
312        elif outcome==SUCCEED:
313            if fldlst:
314                # Matched, as expected, so now we compute the
315                # result string and compare it to our expected result.
316                if verbose:
317                    fldstr = fieldstr = ""
318                    for item in fldlst:
319                        fldstr = fldstr + str(item) + " | "
320                    for item in fields:
321                        fieldstr = fieldstr + str(item) + " | "
322                    print(fldstr, "~~~", fieldstr)
323
324                if len(fields) != len(fldlst):
325                    print('=== Not coherent 1')
326                    RAISE()
327
328                for i in range(len(fields)):
329                    if fields[i] != fldlst[i]:
330                        if verbose:
331                            print('=== Not coherent 2', pattern, s,
332                                    outcome, maxsplit, fields, i,
333                                    fields[i],'(',len(fields[i]),')', ' | ',
334                                    fldlst[i],'(',len(fldlst[i]),')')
335                        else:
336                            print('=== Not coherent 2')
337                        RAISE()
338            else:
339                print ('=== Failed incorrectly', pattern, s,
340                        outcome, maxsplit, fields)
341
342def test_re_obj_findall(verbose = None):
343    """
344    test re.compile(), and reobj.findall()
345    """
346    regex_tests = testsuite.findall_regex_tests
347    for t in regex_tests:
348        pattern, s, outcome, pos, fields = t
349        try:
350            reobj = re.compile(pattern)
351        except:
352            if outcome==SYNTAX_ERROR:
353                # This should have been a syntax error; forget it.
354                pass
355            else:
356                print('=== Unexpected exception:', pattern, s,
357                                    outcome, pos, fields)
358        try:
359            fldlst=reobj.findall(s, pos)
360        except:
361            if outcome == SYNTAX_ERROR:
362                continue
363            else:
364                print('=== Unexpected exception:', pattern, s,
365                                    outcome, pos, fields)
366
367        if outcome==FAIL:
368            pass    # No match, as expected
369        elif outcome==SUCCEED:
370            if fldlst:
371                # Matched, as expected, so now we compute the
372                # result string and compare it to our expected result.
373                if verbose:
374                    fldstr = fieldstr = ""
375                    for item in fldlst:
376                        fldstr = fldstr + str(item) + " | "
377                    for item in fields:
378                        fieldstr = fieldstr + str(item) + " | "
379                    print(fldstr, "~~~", fieldstr)
380
381                if len(fields) != len(fldlst):
382                    print('=== Not coherent 1')
383                    RAISE()
384
385                for i in range(len(fields)):
386                    if fields[i] != fldlst[i]:
387                        if verbose:
388                            print('=== Not coherent 2', pattern, s,
389                                    outcome, maxsplit, fields, i,
390                                    fields[i],'(',len(fields[i]),')', ' | ',
391                                    fldlst[i],'(',len(fldlst[i]),')')
392                        else:
393                            print('=== Not coherent 2')
394                        RAISE()
395            else:
396                print ('=== Failed incorrectly', pattern, s,
397                        outcome, pos, fields)
398
399def test_re_mod_findall(verbose = None):
400    """
401    test re.findall()
402    """
403    regex_tests = testsuite.mod_findall_regex_tests
404    for t in regex_tests:
405        pattern, s, outcome, pos, fields = t    # pos is not used
406        try:
407            fldlst=re.findall(pattern, s)
408        except:
409            if outcome==SYNTAX_ERROR:
410                # This should have been a syntax error; forget it.
411                continue
412            else:
413                print('=== Unexpected exception:', pattern, s,
414                                    outcome, pos, fields)
415
416        if outcome==FAIL:
417            pass    # No match, as expected
418        elif outcome==SUCCEED:
419            if fldlst:
420                # Matched, as expected, so now we compute the
421                # result string and compare it to our expected result.
422                if verbose:
423                    fldstr = fieldstr = ""
424                    for item in fldlst:
425                        fldstr = fldstr + str(item) + " | "
426                    for item in fields:
427                        fieldstr = fieldstr + str(item) + " | "
428                    print(fldstr, "~~~", fieldstr)
429
430                if len(fields) != len(fldlst):
431                    print('=== Not coherent 1')
432                    RAISE()
433
434                for i in range(len(fields)):
435                    if fields[i] != fldlst[i]:
436                        if verbose:
437                            print('=== Not coherent 2', pattern, s,
438                                    outcome, maxsplit, fields, i,
439                                    fields[i],'(',len(fields[i]),')', ' | ',
440                                    fldlst[i],'(',len(fldlst[i]),')')
441                        else:
442                            print('=== Not coherent 2')
443                        RAISE()
444            else:
445                print ('=== Failed incorrectly', pattern, s,
446                        outcome, pos, fields)
447
448def test_mat_obj_groups(verbose = None):
449    """
450    test re.search(), and matobj.groups()
451    'verbose' is for debugging, when 'verbose' is true, print extra info
452    """
453    regex_tests = testsuite.matobj_groups_regex_tests
454    for t in regex_tests:
455        pattern, s, outcome, fields, grpidx, start, end = t
456        try:
457            matobj=re.search(pattern, s)
458        except:
459            if outcome==SYNTAX_ERROR:
460                # This should have been a syntax error; forget it.
461                continue
462            else:
463                print('=== Unexpected exception 1:', pattern, s,
464                                    outcome,fields)
465
466        try:
467            if outcome==SUCCEED: assert(matobj != None)
468            fldlst = matobj.groups()
469        except:
470            if outcome==SYNTAX_ERROR:
471                # This should have been a syntax error; forget it.
472                continue
473            else:
474                print('=== Unexpected exception 2:', pattern, s,
475                                    outcome,fields)
476
477        if outcome==FAIL:
478            pass    # No match, as expected
479        elif outcome==SUCCEED:
480            if fldlst and fields:
481                # Matched, as expected, so now we compute the
482                # result string and compare it to our expected result.
483                if verbose:
484                    fldstr = fieldstr = ""
485                    for item in fldlst:
486                        fldstr = fldstr + str(item) + " | "
487                    for item in fields:
488                        fieldstr = fieldstr + str(item) + " | "
489                    print(fldstr, "~~~", fieldstr)
490
491                if len(fields) != len(fldlst):
492                    print('=== Not coherent 2')
493                    RAISE()
494
495                for i in range(len(fields)):
496                    if fields[i] != fldlst[i]:
497                        if verbose:
498                            print('=== Not coherent', pattern, s,
499                                    outcome,fields, i,
500                                    fields[i],'(',len(fields[i]),')', ' | ',
501                                    fldlst[i],'(',len(fldlst[i]),')')
502                        else:
503                            print('=== Not coherent')
504                        RAISE()
505            elif not len(fldlst) and not len(fields):
506                # output is empty, as expected
507                if verbose:
508                    print("output is empty, as expected")
509                continue
510            else:
511                if verbose:
512                    for item in fldlst:
513                        print(item)
514                    print("")
515                    for item in fields:
516                        print(item)
517                    print("")
518                print ('=== Failed incorrectly', pattern, s,
519                        outcome,fields,fldlst)
520
521def test_mat_obj_start(verbose = None):
522    """
523    test re.search(), and matobj.start()
524    'verbose' is for debugging, when 'verbose' is true, print extra info
525    """
526    regex_tests = testsuite.matobj_groups_regex_tests
527    for t in regex_tests:
528        pattern, s, outcome, fields, grpidx, start, end = t
529        try:
530            matobj=re.search(pattern, s)
531        except:
532            if outcome==SYNTAX_ERROR:
533                # This should have been a syntax error; forget it.
534                continue
535            else:
536                print('=== Unexpected exception 1:', pattern, s,
537                                    outcome,fields)
538
539        try:
540            if outcome==SUCCEED: assert(matobj != None)
541            fldlst = matobj.groups()
542        except:
543            if outcome==SYNTAX_ERROR:
544                # This should have been a syntax error; forget it.
545                continue
546            else:
547                print('=== Unexpected exception 2:', pattern, s,
548                                    outcome,fields)
549
550        if outcome==FAIL:
551            pass    # No match, as expected
552        elif outcome==SUCCEED:
553            if grpidx > 0:
554                if matobj.start(grpidx) == start:
555                    pass
556                else:
557                    if verbose:
558                        print ('=== Failed incorrectly', pattern, s,
559                            outcome,fields,fldlst)
560                    raise("testing failed")
561
562
563def test_mat_obj_end(verbose = None):
564    """
565    test re.search(), and matobj.end()
566    'verbose' is for debugging, when 'verbose' is true, print extra info
567    """
568    regex_tests = testsuite.matobj_groups_regex_tests
569    for t in regex_tests:
570        pattern, s, outcome, fields, grpidx, start, end = t
571        try:
572            matobj=re.search(pattern, s)
573        except:
574            if outcome==SYNTAX_ERROR:
575                # This should have been a syntax error; forget it.
576                continue
577            else:
578                print('=== Unexpected exception 1:', pattern, s,
579                                    outcome,fields)
580
581        try:
582            if outcome==SUCCEED: assert(matobj != None)
583            fldlst = matobj.groups()
584        except:
585            if outcome==SYNTAX_ERROR:
586                # This should have been a syntax error; forget it.
587                continue
588            else:
589                print('=== Unexpected exception 2:', pattern, s,
590                                    outcome,fields)
591
592        if outcome==FAIL:
593            pass    # No match, as expected
594        elif outcome==SUCCEED:
595            if grpidx > 0:
596                if matobj.end(grpidx) == end:
597                    pass
598                else:
599                    if verbose:
600                        print ('=== Failed incorrectly', pattern, s,
601                            outcome,fields,fldlst, matobj.end(grpidx), end)
602                    raise("testing failed")
603
604def test_mat_obj_span(verbose = None):
605    """
606    test re.search(), and matobj.span()
607    'verbose' is for debugging, when 'verbose' is true, print extra info
608    """
609    regex_tests = testsuite.matobj_groups_regex_tests
610    for t in regex_tests:
611        pattern, s, outcome, fields, grpidx, start, end = t
612        try:
613            matobj=re.search(pattern, s)
614        except:
615            if outcome==SYNTAX_ERROR:
616                # This should have been a syntax error; forget it.
617                continue
618            else:
619                print('=== Unexpected exception 1:', pattern, s,
620                                    outcome,fields)
621
622        try:
623            if outcome==SUCCEED: assert(matobj != None)
624            fldlst = matobj.groups()
625        except:
626            if outcome==SYNTAX_ERROR:
627                # This should have been a syntax error; forget it.
628                continue
629            else:
630                print('=== Unexpected exception 2:', pattern, s,
631                                    outcome,fields)
632
633        if outcome==FAIL:
634            pass    # No match, as expected
635        elif outcome==SUCCEED:
636            if (grpidx > 0):
637                spstart, spend = matobj.span(grpidx)
638                if spstart == start and spend == end:
639                    pass
640                else:
641                    if verbose:
642                        print ('=== Failed incorrectly', pattern, s,
643                            outcome,fields,fldlst)
644                    raise("testing failed")
645
646main()
647
648