Skip to content

Commit

Permalink
Add search and match as Jinja tests (home-assistant#49229)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegat01 authored Apr 15, 2021
1 parent 38f0c20 commit 236d274
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 236d274

Please sign in to comment.