|
1 | 1 | from django.test import TestCase
|
2 | 2 |
|
3 |
| -from django_idom.utils import COMPONENT_REGEX |
| 3 | +from django_idom.utils import COMMENT_REGEX, COMPONENT_REGEX |
4 | 4 |
|
5 | 5 |
|
6 | 6 | class RegexTests(TestCase):
|
@@ -41,3 +41,45 @@ def test_component_regex(self):
|
41 | 41 | r'{{ component "" }}',
|
42 | 42 | }:
|
43 | 43 | self.assertNotRegex(fake_component, COMPONENT_REGEX)
|
| 44 | + |
| 45 | + def test_comment_regex(self): |
| 46 | + for comment in { |
| 47 | + r"<!-- comment -->", |
| 48 | + r"""<!-- comment |
| 49 | + -->""", |
| 50 | + r"""<!-- |
| 51 | + comment -->""", |
| 52 | + r"""<!-- |
| 53 | + comment |
| 54 | + -->""", |
| 55 | + r"""<!-- |
| 56 | + a comment |
| 57 | + another comments |
| 58 | + drink some cement |
| 59 | + -->""", # noqa: W291 |
| 60 | + }: |
| 61 | + self.assertRegex(comment, COMMENT_REGEX) |
| 62 | + |
| 63 | + for fake_comment in { |
| 64 | + r"<!-- a comment ", |
| 65 | + r"another comment -->", |
| 66 | + r"<! - - comment - - >", |
| 67 | + r'{% component "my.component" %}', |
| 68 | + }: |
| 69 | + self.assertNotRegex(fake_comment, COMMENT_REGEX) |
| 70 | + |
| 71 | + for embedded_comment in { |
| 72 | + r'{% component "my.component" %} <!-- comment -->', |
| 73 | + r'<!-- comment --> {% component "my.component" %}', |
| 74 | + r'<!-- comment --> {% component "my.component" %} <!-- comment -->', |
| 75 | + r"""<!-- comment |
| 76 | + --> {% component "my.component" %} |
| 77 | + <!-- comment --> |
| 78 | + <!-- |
| 79 | + comment -->""", |
| 80 | + }: # noqa: W291 |
| 81 | + text = COMMENT_REGEX.sub("", embedded_comment) |
| 82 | + if text.strip() != '{% component "my.component" %}': |
| 83 | + raise self.failureException( |
| 84 | + f"Regex pattern {COMMENT_REGEX.pattern} failed to remove comment from {embedded_comment}" |
| 85 | + ) |
0 commit comments