exrex/tests.py

138 lines
4.7 KiB
Python
Raw Normal View History

2013-04-09 14:19:20 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2012-07-04 10:02:46 +00:00
2013-04-09 14:19:20 +00:00
# This file is part of exrex.
#
# exrex is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# exrex is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with exrex. If not, see < http://www.gnu.org/licenses/ >.
#
# (C) 2012- by Adam Tauber, <asciimoo@gmail.com>
2012-07-04 10:02:46 +00:00
2014-07-08 17:33:50 +00:00
from exrex import generate, count, getone, CATEGORIES, simplify
2013-04-09 14:19:20 +00:00
import re
2013-05-13 09:48:47 +00:00
from sys import exit, version_info
IS_PY3 = version_info[0] == 3
2012-07-04 10:02:46 +00:00
RS = {'[ab][cd]': ['ac', 'ad', 'bc', 'bd']
,'[12]{1,2}': ['1', '2', '11', '12', '21', '22']
,'((hai){2}|world)!': ['haihai!', 'world!']
,'[ab]{1,3}': ['a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bba', 'bbb']
2013-03-24 22:29:18 +00:00
,'\d': list(map(str, range(0, 10)))
2012-07-05 09:41:12 +00:00
,'a[b]?(c){0,1}': ['a', 'ac', 'ab', 'abc']
2012-07-10 09:12:26 +00:00
,'(a(b(c(d(e(f))))))': ['abcdef']
2013-04-02 09:40:24 +00:00
,'(a(b(c(d(e(f){1,2}))))){1,2}': ['abcdef', 'abcdeff', 'abcdefabcdef', 'abcdefabcdeff', 'abcdeffabcdef', 'abcdeffabcdeff']
2012-10-10 12:42:03 +00:00
,'[^a]': [x for x in CATEGORIES['category_any'] if x != 'a']
,'[^asdf]': [x for x in CATEGORIES['category_any'] if x not in 'asdf']
,'asdf': ['asdf']
2013-07-28 19:36:38 +00:00
,'(as|df)': ['as', 'df']
2013-04-09 14:19:20 +00:00
,'[áíő]': [u'á', u'í', u'ő']
2013-04-30 01:58:13 +00:00
,'(a|b)(1|2)\\1\\2\\1\\2': ['a1a1a1', 'a2a2a2', 'b1b1b1', 'b2b2b2']
2012-07-04 10:02:46 +00:00
}
BIGS = ['^a*$'
,'^[a-zA-Z]+$'
,'^(foo){3,}$'
,'^([^/]+)(.*)$'
,'^[^/]+(.*)$'
,'^([^/]+).*$'
,'^[^asdf]+$'
,'^([^0-9]{2,}|(a|s|d|f|g)+|[a-z]+)+$'
,'^([^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]|asdf)+$'
,'^(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M$'
2013-05-13 09:48:47 +00:00
,'(.*)\\1'
]
2012-07-04 10:02:46 +00:00
def gen_test():
for regex, result in RS.items():
try:
assert list(generate(regex)) == result
except:
2013-03-24 22:29:18 +00:00
print('[E] Assertion error! "%s"\n\t%r != %r' % (regex, list(generate(regex)), result))
return -1
return 0
2012-07-04 10:02:46 +00:00
def count_test():
for regex, result in RS.items():
c = count(regex)
l = len(result)
try:
assert c == l
except:
2013-05-13 09:48:47 +00:00
if IS_PY3:
print('[E] Assertion error! "%s"\n\t%d != %d' % (regex, c, l))
return -1
else:
print('[E] Assertion error! "%s"\n\t%d != %d' % (regex.decode('utf-8'), c, l))
return -1
return 0
2012-07-04 10:02:46 +00:00
2014-07-08 17:33:50 +00:00
def getone_test(tries=200):
for regex,_ in RS.items():
2013-03-24 22:29:18 +00:00
for _ in range(tries):
try:
s = getone(regex)
2013-05-13 09:48:47 +00:00
if IS_PY3:
assert re.match(regex, s, re.U)
else:
assert re.match(regex, s.encode('utf-8'), re.U)
2013-04-30 01:58:13 +00:00
except Exception:
2013-05-13 09:48:47 +00:00
if IS_PY3:
print('[E] Assertion error! "%s"\n\t%s not match' % (regex, s))
return -1
else:
print('[E] Assertion error! "%s"\n\t%s not match' % (regex.decode('utf-8'), s))
return -1
for regex in BIGS:
2013-03-24 22:29:18 +00:00
for _ in range(tries):
try:
s = getone(regex)
2013-05-13 09:48:47 +00:00
if IS_PY3:
assert re.match(regex, s, re.U)
else:
assert re.match(regex, s.encode('utf-8'), re.U)
except:
2013-05-13 09:48:47 +00:00
if IS_PY3:
print('[E] Assertion error! "%s"\n\t%s not match' % (regex, s))
return -1
else:
print('[E] Assertion error! "%s"\n\t%s not match' % (regex.decode('utf-8'), s))
return -1
return 0
2012-07-04 10:02:46 +00:00
2014-07-08 17:33:50 +00:00
def simplify_test():
for regex, result in RS.items():
new_regex = simplify(regex)
if not IS_PY3:
new_regex = new_regex.encode('utf-8')
r = list(generate(new_regex))
try:
assert r == result
except:
print('[E] Assertion error! "%s"\n\t%r != %r' % (regex, r, result))
return -1
2012-07-04 10:02:46 +00:00
if __name__ == '__main__':
2014-07-08 17:33:50 +00:00
tests = {'generation': gen_test,
'count': count_test,
'random generation': getone_test,
'simplification': simplify_test}
for i, (test_name, test) in enumerate(tests.items()):
errors = test()
if not errors:
print('[+] {0} test passed'.format(test_name))
else:
print('[-] {0} test failed'.format(test_name))
exit(i+1)