Source

simplere / test / test.py

Full commit

from simplere import *

class YesItIs(ValueError): pass

def test_basic():
    tests = 'some string with things in it ok?'
    
    sword  = Re(r'\b(s\w*)\b')    
    
    if tests in sword:
        assert Re._[1] == 'some'
        assert Re._.end(1) == 4
        assert Re._._match.group(1) == Re._[1]
    else:
        raise YesItIs()
    
def test_findall():
    tests = 'some string with things in it ok?'
    
    sword  = Re(r'\b(s\w*)\b')    

    assert sword.findall(tests) == ['some', 'string']
    
    iterlist = [ m[1] for m in sword.finditer(tests) ]
    assert iterlist == ['some', 'string']
    
def test_attributes():
    
    tests = 'some string with things in it ok?'
    sword  = Re(r'\b(?P<word>s\w*)\b')
    
    if tests in sword:
        assert Re._.word == 'some'
    else:
        raise YesItIs()
    
    iterlist = [ m.word for m in sword.finditer(tests) ]
    assert iterlist == ['some', 'string']

    person = 'John Smith 48'
    if person in Re(r'(?P<name>[\w\s]*)\s+(?P<age>\d+)'):
        assert Re._.name == 'John Smith'
        assert int(Re._.age) == 48
        assert Re._.name == Re._._match.group('name')
        assert Re._.age  == Re._._match.group('age')
    else:
        raise YesItIs()
    
    for found in Re(r'pattern (\w+)').finditer('pattern is as pattern does'):
        assert isinstance(found, ReMatch)
        assert found[1] in ['is','does']
    
    found = Re(r'pattern (\w+)').findall('pattern is as pattern does')
    assert found == 'is does'.split()
    
def test_memoization():    
    testpat  = Re(r'\b(s\w*)\b')
    testpat1 = Re(r'\b(s\w*)\b')
    assert testpat is testpat1   # test memoization
    
    assert Glob('a*') is Glob('a*')

def test_Glob():
    assert "alpha" in Glob("a*")
    assert "beta" not in Glob("a*")
    
    assert 'globtastic' in Glob('glob*')