exrex/tests.py

44 lines
1.2 KiB
Python
Raw Normal View History

2012-07-04 10:02:46 +00:00
from exrex import generate, count, getone
from re import match
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']
,'\d': 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']
,'(a(b(c(d(e(f){1,2}))))){1,2}': ['abcdef', 'abcdeff', 'abcdefabcdef', 'abcdefabcdeff', 'abcdeffabcdef', 'abcdeffabcdeff']
2012-07-04 10:02:46 +00:00
}
BIGS = ['^a*$'
,'^[a-zA-Z]+$'
,'^(foo){3,}$'
]
2012-07-04 10:02:46 +00:00
def gen_test():
for regex, result in RS.items():
assert list(generate(regex)) == result
def count_test():
for regex, result in RS.items():
2012-07-10 09:12:26 +00:00
assert count(regex) == len(result)
2012-07-04 10:02:46 +00:00
def getone_test():
for regex,_ in RS.items():
assert match(regex, getone(regex))
for regex in BIGS:
assert match(regex, getone(regex))
2012-07-04 10:02:46 +00:00
if __name__ == '__main__':
gen_test()
print('[+] generation test passed')
2012-07-10 09:12:26 +00:00
count_test()
print('[+] length test passed')
getone_test()
print('[+] random generation test passed')