Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search and match as Jinja tests #49229

Merged
merged 3 commits into from Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/helpers/template.py
Expand Up @@ -1459,6 +1459,8 @@ def __init__(self, hass, limited=False, strict=False):
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
Expand Down
22 changes: 22 additions & 0 deletions tests/helpers/test_template.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down