Add search and match as Jinja tests (#49229)

This commit is contained in:
Mike Degatano 2021-04-15 14:13:27 -04:00 committed by GitHub
parent 38f0c201c2
commit 236d274351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -1459,6 +1459,8 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
self.globals["urlencode"] = urlencode
self.globals["max"] = max
self.globals["min"] = min
self.tests["match"] = regex_match
self.tests["search"] = regex_search
if hass is None:
return

View File

@ -1003,6 +1003,17 @@ def test_regex_match(hass):
assert tpl.async_render() is True
def test_match_test(hass):
"""Test match test."""
tpl = template.Template(
r"""
{{ '123-456-7890' is match('(\\d{3})-(\\d{3})-(\\d{4})') }}
""",
hass,
)
assert tpl.async_render() is True
def test_regex_search(hass):
"""Test regex_search method."""
tpl = template.Template(
@ -1038,6 +1049,17 @@ def test_regex_search(hass):
assert tpl.async_render() is True
def test_search_test(hass):
"""Test search test."""
tpl = template.Template(
r"""
{{ '123-456-7890' is search('(\\d{3})-(\\d{3})-(\\d{4})') }}
""",
hass,
)
assert tpl.async_render() is True
def test_regex_replace(hass):
"""Test regex_replace method."""
tpl = template.Template(