Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c0ce270924
Fetching contributors…

Cannot retrieve contributors at this time

file 66 lines (57 sloc) 2.121 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


from exrex import generate, count, getone, CATEGORIES
from re import match

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))
     ,'a[b]?(c){0,1}': ['a', 'ac', 'ab', 'abc']
     ,'(a(b(c(d(e(f))))))': ['abcdef']
     ,'(a(b(c(d(e(f){1,2}))))){1,2}': ['abcdef', 'abcdeff', 'abcdefabcdef', 'abcdefabcdeff', 'abcdeffabcdef', 'abcdeffabcdeff']
     ,'[^a]': [x for x in CATEGORIES['category_any'] if x != 'a']
     ,'[^asdf]': [x for x in CATEGORIES['category_any'] if x not in 'asdf']
     }

BIGS = ['^a*$'
       ,'^[a-zA-Z]+$'
       ,'^(foo){3,}$'
       ,'([^/]+)(.*)'
       ,'[^/]+(.*)'
       ,'([^/]+).*'
       ,'([^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]|asdf)+'
       ]

def gen_test():
    for regex, result in RS.items():
        try:
            assert list(generate(regex)) == result
        except:
            print '[E] Assertion error! "%s"\n\t%r != %r' % (regex, list(generate(regex)), result)

def count_test():
    for regex, result in RS.items():
        c = count(regex)
        l = len(result)
        try:
            assert c == l
        except:
            print '[E] Assertion error! "%s"\n\t%d != %d' % (regex, c, l)

def getone_test(tries):
    for regex,_ in RS.items():
        for _ in range(tries):
            s = getone(regex)
            try:
                assert match(regex, s)
            except:
                print '[E] Assertion error! "%s"\n\t%s not match' % (regex, s)
    for regex in BIGS:
        for _ in range(tries):
            s = getone(regex)
            try:
                assert match(regex, s)
            except:
                print '[E] Assertion error! "%s"\n\t%s not match' % (regex, s)


if __name__ == '__main__':
    gen_test()
    print('[+] generation test passed')
    count_test()
    print('[+] length test passed')
    getone_test(200)
    print('[+] random generation test passed')
Something went wrong with that request. Please try again.