Skip to content

Commit

Permalink
fix pattern and test for comment removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Coelho authored and zx80 committed May 29, 2024
1 parent cd1f278 commit 0935455
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions aiosql/query_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
r'(?P<dquote>"(""|[^"])+")|'
# one-line comment
r"(?P<oneline>--.*?$)|"
# Multiline comments, excluding SQL hints
r"|(?P<multiline>/\*(?!\+)[\s\S]*?\*/)",
# multiline comments, excluding SQL hints
r"|(?P<multiline>/\*(?!\+[\s\S]*?\*/)[\s\S]*?\*/)",
re.DOTALL | re.MULTILINE,
)

Expand Down
31 changes: 16 additions & 15 deletions tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_var_pattern_handles_empty_sql_string_literals():
assert groupdicts[3] == expected_var_match


# must remove only OK comments
# must remove *only* OK comments
COMMENTED = """
KO
-- KO
Expand All @@ -99,33 +99,34 @@ def test_var_pattern_handles_empty_sql_string_literals():
* OK
*/
-- /* KO
-- */
-- KO */
/* OK
-- OK
' OK ' "OK "
*/
KO
/* OK */ -- KO 'KO'
-- KO */
/*+ OK */
/*+ KO (hints must be kept!) */
"""


def test_comments():
n = 0
for m in _UNCOMMENT.finditer(COMMENTED):
n += 1
matches = m.groupdict()
for ma in _UNCOMMENT.finditer(COMMENTED):
matches = ma.groupdict()
s, d, c, m = matches["squote"], matches["dquote"], matches["oneline"], matches["multiline"]
assert s or d or c or m
if m:
assert "KO" not in m
if s:
assert "OK" not in s
if d:
assert "OK" not in d
if c:
assert "OK" not in c
# assert s or d or c or m, f"bad match: {m} {matches}"
if s or d or c or m:
n += 1
if m:
assert "OK" in m and "KO" not in m
if s:
assert "KO" in s and "OK" not in s
if d:
assert "KO" in d and "OK" not in d
if c:
assert "KO" in c and "OK" not in c
assert n == 13


Expand Down

0 comments on commit 0935455

Please sign in to comment.