Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_convert_ecma_regex_to_python():
assert ('some', []) == utilities.convert_ecma_regex_to_python('/some/')
assert (
('some/pattern', []) ==
utilities.convert_ecma_regex_to_python('/some/pattern/')
)
assert (
('^some \d+ pattern$', []) ==
utilities.convert_ecma_regex_to_python('/^some \d+ pattern$/')
)
regex, flags = utilities.convert_ecma_regex_to_python('/^regex \d/i')
assert '^regex \d' == regex
assert set([re.I]) == set(flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/m')
assert '^regex \d' == result.regex
assert set([re.M]) == set(result.flags)
utilities.convert_ecma_regex_to_python('/some/pattern/')
)
assert (
('^some \d+ pattern$', []) ==
utilities.convert_ecma_regex_to_python('/^some \d+ pattern$/')
)
regex, flags = utilities.convert_ecma_regex_to_python('/^regex \d/i')
assert '^regex \d' == regex
assert set([re.I]) == set(flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/m')
assert '^regex \d' == result.regex
assert set([re.M]) == set(result.flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/mi')
assert '^regex \d' == result.regex
assert set([re.M, re.I]) == set(result.flags)
with pytest.raises(ValueError):
utilities.convert_ecma_regex_to_python('/regex/wrong')
assert (
('python regex', []) ==
utilities.convert_ecma_regex_to_python('python regex')
)
assert (
('^another \d python regex$', []) ==
utilities.convert_ecma_regex_to_python('^another \d python regex$')
)
)
regex, flags = utilities.convert_ecma_regex_to_python('/^regex \d/i')
assert '^regex \d' == regex
assert set([re.I]) == set(flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/m')
assert '^regex \d' == result.regex
assert set([re.M]) == set(result.flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/mi')
assert '^regex \d' == result.regex
assert set([re.M, re.I]) == set(result.flags)
with pytest.raises(ValueError):
utilities.convert_ecma_regex_to_python('/regex/wrong')
assert (
('python regex', []) ==
utilities.convert_ecma_regex_to_python('python regex')
)
assert (
('^another \d python regex$', []) ==
utilities.convert_ecma_regex_to_python('^another \d python regex$')
)
result = utilities.convert_ecma_regex_to_python('python regex')
assert 'python regex' == result.regex
assert [] == result.flags
assert set([re.I]) == set(flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/m')
assert '^regex \d' == result.regex
assert set([re.M]) == set(result.flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/mi')
assert '^regex \d' == result.regex
assert set([re.M, re.I]) == set(result.flags)
with pytest.raises(ValueError):
utilities.convert_ecma_regex_to_python('/regex/wrong')
assert (
('python regex', []) ==
utilities.convert_ecma_regex_to_python('python regex')
)
assert (
('^another \d python regex$', []) ==
utilities.convert_ecma_regex_to_python('^another \d python regex$')
)
result = utilities.convert_ecma_regex_to_python('python regex')
assert 'python regex' == result.regex
assert [] == result.flags
def test_converters():
assert (
'/^ecma \d regex$/im' ==
utilities.convert_python_regex_to_ecma(
*utilities.convert_ecma_regex_to_python('/^ecma \d regex$/im'))
)
result = utilities.convert_ecma_regex_to_python(
utilities.convert_python_regex_to_ecma(
'^some \w python regex$', [re.I]))
assert '^some \w python regex$' == result.regex
assert [re.I] == result.flags
def test_converters():
assert (
'/^ecma \d regex$/im' ==
utilities.convert_python_regex_to_ecma(
*utilities.convert_ecma_regex_to_python('/^ecma \d regex$/im'))
)
result = utilities.convert_ecma_regex_to_python(
utilities.convert_python_regex_to_ecma(
'^some \w python regex$', [re.I]))
assert '^some \w python regex$' == result.regex
assert [re.I] == result.flags
result = utilities.convert_ecma_regex_to_python('/^regex \d/mi')
assert '^regex \d' == result.regex
assert set([re.M, re.I]) == set(result.flags)
with pytest.raises(ValueError):
utilities.convert_ecma_regex_to_python('/regex/wrong')
assert (
('python regex', []) ==
utilities.convert_ecma_regex_to_python('python regex')
)
assert (
('^another \d python regex$', []) ==
utilities.convert_ecma_regex_to_python('^another \d python regex$')
)
result = utilities.convert_ecma_regex_to_python('python regex')
assert 'python regex' == result.regex
assert [] == result.flags
def test_convert_ecma_regex_to_python():
assert ('some', []) == utilities.convert_ecma_regex_to_python('/some/')
assert (
('some/pattern', []) ==
utilities.convert_ecma_regex_to_python('/some/pattern/')
)
assert (
('^some \d+ pattern$', []) ==
utilities.convert_ecma_regex_to_python('/^some \d+ pattern$/')
)
regex, flags = utilities.convert_ecma_regex_to_python('/^regex \d/i')
assert '^regex \d' == regex
assert set([re.I]) == set(flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/m')
assert '^regex \d' == result.regex
assert set([re.M]) == set(result.flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/mi')
assert '^regex \d' == result.regex
assert set([re.M, re.I]) == set(result.flags)
with pytest.raises(ValueError):
utilities.convert_ecma_regex_to_python('/regex/wrong')
assert (
('python regex', []) ==
def test_convert_ecma_regex_to_python():
assert ('some', []) == utilities.convert_ecma_regex_to_python('/some/')
assert (
('some/pattern', []) ==
utilities.convert_ecma_regex_to_python('/some/pattern/')
)
assert (
('^some \d+ pattern$', []) ==
utilities.convert_ecma_regex_to_python('/^some \d+ pattern$/')
)
regex, flags = utilities.convert_ecma_regex_to_python('/^regex \d/i')
assert '^regex \d' == regex
assert set([re.I]) == set(flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/m')
assert '^regex \d' == result.regex
assert set([re.M]) == set(result.flags)
result = utilities.convert_ecma_regex_to_python('/^regex \d/mi')
assert '^regex \d' == result.regex
"""Init.
Note, that if given pattern is ECMA regex, given flags will be
**completely ignored** and taken from given regex.
:param string pattern: Pattern of regex.
:param bool flags: Flags used for the regex matching.
Allowed flag names are in the `FLAGS` attribute. The flag value
does not matter as long as it evaluates to True.
Flags with False values will be ignored.
Invalid flags will be ignored.
"""
if utilities.is_ecma_regex(pattern):
result = utilities.convert_ecma_regex_to_python(pattern)
self.pattern, self.flags = result
else:
self.pattern = pattern
self.flags = [self.FLAGS[key] for key, value in flags.items()
if key in self.FLAGS and value]