From a414e7a3a57ea33f4eb8b6bece82795eee2afeff Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Tue, 27 Jun 2023 16:27:15 -0700 Subject: [PATCH 1/4] Simplified regex expressions --- src/reactpy_django/utils.py | 6 +++--- tests/test_app/tests/test_regex.py | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/reactpy_django/utils.py b/src/reactpy_django/utils.py index a9edbd3f..219f5c3e 100644 --- a/src/reactpy_django/utils.py +++ b/src/reactpy_django/utils.py @@ -27,9 +27,9 @@ _logger = logging.getLogger(__name__) _component_tag = r"(?Pcomponent)" -_component_path = r"(?P(\"[^\"'\s]+\")|('[^\"'\s]+'))" -_component_kwargs = r"(?P(.*?|\s*?)*)" -COMMENT_REGEX = re.compile(r"()") +_component_path = r"(?P\"[^\"'\s]+\"|'[^\"'\s]+')" +_component_kwargs = r"(?P[\s\S]*?)" +COMMENT_REGEX = re.compile(r"") COMPONENT_REGEX = re.compile( r"{%\s*" + _component_tag diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index 096107b5..a8b68d24 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -83,3 +83,9 @@ def test_comment_regex(self): raise self.failureException( f"Regex pattern {COMMENT_REGEX.pattern} failed to remove comment from {embedded_comment}" ) + + # Make sure back-to-back components are not merged into one match + double_component_match = COMPONENT_REGEX.search( + r'{% component "my.component" %} {% component "my.component" %}' + ) + self.assertTrue(double_component_match[0] == r'{% component "my.component" %}') From 7257dc069121cb4ba631da280b8754a1f930f82c Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Tue, 27 Jun 2023 16:33:21 -0700 Subject: [PATCH 2/4] fix types lint --- tests/test_app/tests/test_regex.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index a8b68d24..d0209ca0 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -88,4 +88,8 @@ def test_comment_regex(self): double_component_match = COMPONENT_REGEX.search( r'{% component "my.component" %} {% component "my.component" %}' ) + if not double_component_match: + raise self.failureException( + f"Regex pattern {COMPONENT_REGEX.pattern} failed to match component" + ) self.assertTrue(double_component_match[0] == r'{% component "my.component" %}') From 983af60ce6e289686a1356052bc25b38461ddad7 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Tue, 27 Jun 2023 16:42:34 -0700 Subject: [PATCH 3/4] simpified tests --- tests/test_app/tests/test_regex.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index d0209ca0..06b87961 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -79,10 +79,7 @@ def test_comment_regex(self): comment -->""", }: # noqa: W291 text = COMMENT_REGEX.sub("", embedded_comment) - if text.strip() != '{% component "my.component" %}': - raise self.failureException( - f"Regex pattern {COMMENT_REGEX.pattern} failed to remove comment from {embedded_comment}" - ) + self.assertEquals(text.strip(), '{% component "my.component" %}') # Make sure back-to-back components are not merged into one match double_component_match = COMPONENT_REGEX.search( From 41938a77005b3db4d09f1be084ab23d9b5a39c17 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Tue, 27 Jun 2023 17:00:29 -0700 Subject: [PATCH 4/4] remove loops from tests --- tests/test_app/tests/test_regex.py | 176 ++++++++++++++++++----------- 1 file changed, 110 insertions(+), 66 deletions(-) diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index 06b87961..cee32751 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -5,88 +5,132 @@ class RegexTests(TestCase): def test_component_regex(self): - for component in { - r'{%component "my.component"%}', - r'{%component "my.component"%}', - r"{%component 'my.component'%}", - r'{% component "my.component" %}', - r"{% component 'my.component' %}", - r'{% component "my.component" class="my_thing" %}', + # Real component matches + self.assertRegex(r'{%component "my.component"%}', COMPONENT_REGEX) + self.assertRegex(r'{%component "my.component"%}', COMPONENT_REGEX) + self.assertRegex(r"{%component 'my.component'%}", COMPONENT_REGEX) + self.assertRegex(r'{% component "my.component" %}', COMPONENT_REGEX) + self.assertRegex(r"{% component 'my.component' %}", COMPONENT_REGEX) + self.assertRegex( + r'{% component "my.component" class="my_thing" %}', COMPONENT_REGEX + ) + self.assertRegex( r'{% component "my.component" class="my_thing" attr="attribute" %}', + COMPONENT_REGEX, + ) + self.assertRegex( r"""{% + component + "my.component" + class="my_thing" + attr="attribute" - component - "my.component" - class="my_thing" - attr="attribute" + %}""", # noqa: W291 + COMPONENT_REGEX, + ) - %}""", # noqa: W291 - }: - self.assertRegex(component, COMPONENT_REGEX) + # Fake component matches + self.assertNotRegex(r'{% not_a_real_thing "my.component" %}', COMPONENT_REGEX) + self.assertNotRegex(r"{% component my.component %}", COMPONENT_REGEX) + self.assertNotRegex(r"""{% component 'my.component" %}""", COMPONENT_REGEX) + self.assertNotRegex(r'{ component "my.component" }', COMPONENT_REGEX) + self.assertNotRegex(r'{{ component "my.component" }}', COMPONENT_REGEX) + self.assertNotRegex(r"component", COMPONENT_REGEX) + self.assertNotRegex(r"{%%}", COMPONENT_REGEX) + self.assertNotRegex(r" ", COMPONENT_REGEX) + self.assertNotRegex(r"", COMPONENT_REGEX) + self.assertNotRegex(r'{% component " my.component " %}', COMPONENT_REGEX) + self.assertNotRegex( + r"""{% component "my.component COMPONENT_REGEX) + self.assertNotRegex( " %}""", + COMPONENT_REGEX, + ) + self.assertNotRegex(r'{{ component """ }}', COMPONENT_REGEX) + self.assertNotRegex(r'{{ component "" }}', COMPONENT_REGEX) - for fake_component in { - r'{% not_a_real_thing "my.component" %}', - r"{% component my.component %}", - r"""{% component 'my.component" %}""", - r'{ component "my.component" }', - r'{{ component "my.component" }}', - r"component", - r"{%%}", - r" ", - r"", - r'{% component " my.component " %}', - r"""{% component "my.component - " %}""", - r'{{ component """ }}', - r'{{ component "" }}', - }: - self.assertNotRegex(fake_component, COMPONENT_REGEX) + # Make sure back-to-back components are not merged into one match + double_component_match = COMPONENT_REGEX.search( + r'{% component "my.component" %} {% component "my.component" %}' + ) + self.assertTrue(double_component_match[0] == r'{% component "my.component" %}') # type: ignore def test_comment_regex(self): - for comment in { - r"", + # Real comment matches + self.assertRegex(r"", COMMENT_REGEX) + self.assertRegex( r"""""", + -->""", + COMMENT_REGEX, + ) + self.assertRegex( r"""""", + comment -->""", + COMMENT_REGEX, + ) + self.assertRegex( r"""""", + comment + -->""", + COMMENT_REGEX, + ) + self.assertRegex( r"""""", # noqa: W291 - }: - self.assertRegex(comment, COMMENT_REGEX) + a comment + another comments + drink some cement + -->""", # noqa: W291 + COMMENT_REGEX, + ) - for fake_comment in { - r"", - r"", - r'{% component "my.component" %}', - }: - self.assertNotRegex(fake_comment, COMMENT_REGEX) + # Fake comment matches + self.assertNotRegex(r"", COMMENT_REGEX) + self.assertNotRegex(r"", COMMENT_REGEX) + self.assertNotRegex(r'{% component "my.component" %}', COMMENT_REGEX) - for embedded_comment in { - r'{% component "my.component" %} ', - r' {% component "my.component" %}', - r' {% component "my.component" %} ', - r"""' + ).strip(), + '{% component "my.component" %}', + ) + self.assertEquals( + COMMENT_REGEX.sub( + "", r' {% component "my.component" %}' + ).strip(), + '{% component "my.component" %}', + ) + self.assertEquals( + COMMENT_REGEX.sub( + "", r' {% component "my.component" %} ' + ).strip(), + '{% component "my.component" %}', + ) + self.assertEquals( + COMMENT_REGEX.sub( + "", + r""" {% component "my.component" %} """, - }: # noqa: W291 - text = COMMENT_REGEX.sub("", embedded_comment) - self.assertEquals(text.strip(), '{% component "my.component" %}') + ).strip(), + '{% component "my.component" %}', + ) - # Make sure back-to-back components are not merged into one match - double_component_match = COMPONENT_REGEX.search( - r'{% component "my.component" %} {% component "my.component" %}' + # Components surrounded by comments + self.assertEquals( + COMMENT_REGEX.sub("", r''), + "", + ) + self.assertEquals( + COMMENT_REGEX.sub( + "", + r"""""", # noqa: W291 + ), + "", ) - if not double_component_match: - raise self.failureException( - f"Regex pattern {COMPONENT_REGEX.pattern} failed to match component" - ) - self.assertTrue(double_component_match[0] == r'{% component "my.component" %}')