def test_upTo(): res=ExpressiveRegex()\ .upTo(4).digit\ .toRegexString() assert res=='\\d{,4}' res=ExpressiveRegex()\ .upTo(1).digit\ .toRegexString() assert res=='\\d?'
def test_atLeast(): res=ExpressiveRegex()\ .atLeast(4).digit\ .toRegexString() assert res=='\\d{4,}' res=ExpressiveRegex()\ .atLeast(0).digit\ .toRegexString() assert res=='\\d*' res=ExpressiveRegex()\ .atLeast(1).digit\ .toRegexString() assert res=='\\d+'
def test_between(): res=ExpressiveRegex()\ .between(2,4).digit\ .toRegexString() assert res=='\\d{2,4}' res=ExpressiveRegex()\ .between(0,1).digit\ .toRegexString() assert res=='\\d?' res=ExpressiveRegex()\ .between(0,4).digit\ .toRegexString() assert res=='\\d{,4}'
def test_anythingButChars(): expression = "".join( random.choices(string.ascii_letters, k=random.randint(1, len(string.ascii_letters)))) res=ExpressiveRegex()\ .anythingButChars(expression)\ .toRegexString() assert res == '[^' + expression + ']'
def test_setOfLiterals(): res=ExpressiveRegex()\ .setOfLiterals\ .char('-')\ .range(1,4)\ .anyOfChars('dfs')\ .end()\ .toRegexString() assert res=='[\\-1-4dfs]'
def test_char_and_rawChar(): for i in string.ascii_letters: res=ExpressiveRegex()\ .char(i)\ .toRegexString() assert res == i res=ExpressiveRegex()\ .rawChar(i)\ .toRegexString() assert res == i for i in string.digits: res=ExpressiveRegex()\ .char(i)\ .toRegexString() assert res == i res=ExpressiveRegex()\ .rawChar(i)\ .toRegexString() assert res == i for i in specialChars: res=ExpressiveRegex()\ .char(i)\ .toRegexString() assert res == '\\' + i res=ExpressiveRegex()\ .rawChar(i)\ .toRegexString() assert res == i
def test_string_and_rawString(): expression = "".join( random.choices(string.ascii_letters, k=random.randint(1, len(string.ascii_letters)))) res=ExpressiveRegex()\ .string(expression)\ .toRegexString() assert res == expression res=ExpressiveRegex()\ .rawString(expression)\ .toRegexString() assert res == expression expression = "".join( random.choices(specialChars, k=random.randint(1, len(specialChars)))) fix_expression = "".join(('\\' + i for i in expression)) res=ExpressiveRegex()\ .string(expression)\ .toRegexString() assert res == fix_expression res=ExpressiveRegex()\ .rawString(expression)\ .toRegexString() assert res == expression
def test_oneOrMoreLazy(): res=ExpressiveRegex()\ .oneOrMoreLazy.digit\ .toRegexString() assert res=='\\d+?'
def test_optional(): res=ExpressiveRegex()\ .optional.digit\ .toRegexString() assert res=='\\d?'
def test_space(): res=ExpressiveRegex()\ .space\ .toRegexString() assert res == ' '
def test_nonWhitespaceChar(): res=ExpressiveRegex()\ .nonWhitespaceChar\ .toRegexString() assert res == '\\S'
def test_whitespaceChar(): res=ExpressiveRegex()\ .whitespaceChar\ .toRegexString() assert res == '\\s'
def test_nonWord(): res=ExpressiveRegex()\ .nonWord\ .toRegexString() assert res == '\\W'
def test_word(): res=ExpressiveRegex()\ .word\ .toRegexString() assert res == '\\w'
def test_nonDigit(): res=ExpressiveRegex()\ .nonDigit\ .toRegexString() assert res == '\\D'
def test_digit(): res=ExpressiveRegex()\ .digit\ .toRegexString() assert res == '\\d'
def test_exactly(): res=ExpressiveRegex()\ .exactly(4).digit\ .toRegexString() assert res=='\\d{4}'
def test_betweenLazy(): res=ExpressiveRegex()\ .betweenLazy(2,4).digit\ .toRegexString() assert res=='\\d{2,4}?'
def test_newline(): res=ExpressiveRegex()\ .newline\ .toRegexString() assert res == '\\n'
def test_anythingButRange(): exp=ExpressiveRegex()\ .anythingButRange(0,9)\ .toRegex() for d in string.digits: res = exp.search(d) assert res is None exclud = [2] exp=ExpressiveRegex()\ .anythingButRange(0,9, exclude=exclud)\ .toRegex() for d in string.digits: res = exp.search(d) if int(d) in exclud: assert res is not None else: assert res is None exclud = [2, 4] exp=ExpressiveRegex()\ .anythingButRange(0,9, exclude=exclud)\ .toRegex() exclud = set(exclud) for d in string.digits: res = exp.search(d) if int(d) in exclud: assert res is not None else: assert res is None exp=ExpressiveRegex()\ .anythingButRange('a','z')\ .toRegex() for d in string.ascii_lowercase: res = exp.search(d) assert res is None exclud = 'a' exp=ExpressiveRegex()\ .anythingButRange('a','z', exclude=exclud)\ .toRegex() exclud = set(exclud) for d in string.ascii_lowercase: res = exp.search(d) if d in exclud: assert res is not None else: assert res is None exclud = 'abz' exp=ExpressiveRegex()\ .anythingButRange('a','z', exclude=exclud)\ .toRegex() exclud = set(exclud) for d in string.ascii_lowercase: res = exp.search(d) if d in exclud: assert res is not None else: assert res is None exclud = 'a' exp=ExpressiveRegex()\ .anythingButRange('a','z', exclude=exclud)\ .toRegex() exclud = set(exclud) for d in string.ascii_lowercase: res = exp.search(d) if d in exclud: assert res is not None else: assert res is None exclud = 'AFX' exp=ExpressiveRegex()\ .anythingButRange('A','Z', exclude=exclud)\ .toRegex() exclud = set(exclud) for d in string.ascii_uppercase: res = exp.search(d) if d in exclud: assert res is not None else: assert res is None
def test_carriageReturn(): res=ExpressiveRegex()\ .carriageReturn\ .toRegexString() assert res == '\\r'
def test_tab(): res=ExpressiveRegex()\ .tab\ .toRegexString() assert res == '\\t'
def test_zeroOrMore(): res=ExpressiveRegex()\ .zeroOrMore.digit\ .toRegexString() assert res=='\\d*'
def test_anyChar(): res=ExpressiveRegex()\ .anyChar\ .toRegexString() assert res == '.'