simplere / test / test.py

from simplere import *
import re

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()
    
    assert 'ddd' not in sword
    assert 'it goes other ways' not in sword
    assert 'it goes other ways sometimes' in sword
    
    
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_from_sre():
    pat = re.compile(r'\b(s\w*)\b')
    repat = Re(pat)
    
    tests = 'some string with things in it ok?'
    assert tests in repat
    assert repat.findall(tests) == ['some', 'string']
    
    assert 'ddd' not in repat

def test_Glob():
    assert "alpha" in Glob("a*")
    assert "beta" not in Glob("a*")
    
    assert 'globtastic' in Glob('glob*')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.