Source

simplere / test / test.py

Full commit

try:
    from testharness import import_from_parent, test_run
    # raise ImportError
    import_from_parent()
except ImportError:
    test_run = None

from simplere import *

def test_Re():
    _ = 1
    print "test_Re()"
    tests = 'some string with things in it ok?'
    testpat  = Re(r'\b(s\w*)\b')
    testpat1 = Re(r'\b(s\w*)\b')
    assert testpat is testpat1   # test memoization
    if tests in Re(r'\b(s\w*)\b').into(_):
        print Re._[1]
        print _
        print _[1]
        assert _[1] == 'some'
        assert _.end(1) == 4
        assert _._match.group(1) == _[1]
    print 'x'  * 6
    
    if tests in testpat:
        print _[1]
        assert _[1] == 'some'
        assert _.end(1) == 4
        assert _._match.group(1) == _[1]
    else:
        raise ValueError('yes it is!!!')
    
    found = testpat.findall(tests)
    assert found == ['some', 'string']
    
    person = 'John Smith 48'
    if person in Re(r'(?P<name>[\w\s]*)\s+(?P<age>\d+)'):
        assert _.name == 'John Smith'
        print _.name
        assert int(_.age) == 48
        assert _.name == _._match.group('name')
        assert _.age  == _._match.group('age')
    else:
        raise ValueError('yes it is!!!')
    
    for found in Re(r'pattern (\w+)').finditer('pattern is as pattern does'):
        print found[1]
        assert isinstance(found, ReMatch)
        assert found[1] in ['is','does']
    
    found = Re(r'pattern (\w+)').findall('pattern is as pattern does')
    print found
    assert found == 'is does'.split()
    
def test_Glob():
    assert "alpha" in Glob("a*")
    assert "beta" not in Glob("a*")
    
    if 'globtastic' in Glob('glob*'):
        print "Yes! You're right. It is!"
    else:
        raise ValueError('YES IT IS')
    
if __name__ == '__main__':
    if test_run:
        test_run()
    else:
        for f in [test_Re, test_Glob]:
            print "-" * 20, f.func_name, "-" * 20
            f()
        print "-" * 20, "done", "-" * 20