Skip to content

Commit

Permalink
Fixed * and |
Browse files Browse the repository at this point in the history
  • Loading branch information
heraldofsolace committed Jun 1, 2024
1 parent 78d3205 commit 855678c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 3 additions & 4 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ def search_tag(self, text, subreddit="vim"):
if subreddit not in ["vim", "neovim"]:
subreddit = "vim"

text = text.replace('"', "quote").replace("*", "star").replace("|", "bar")
text_escaped = text.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
text_escaped = text_escaped.replace('"', "quote")

# Get all possible matches
possible_matches = self.cursor.execute(
"""SELECT * FROM tags WHERE tag LIKE (?) ESCAPE '\\' AND software=(?)""", ('%'+text_escaped+'%', subreddit)).fetchall()
Expand All @@ -74,7 +73,7 @@ def search_tag(self, text, subreddit="vim"):
if len(possible_matches) == 0:
return None
possible_matches.sort(key=lambda t: len(t[1])) # Sort by length

print(possible_matches)
# If there is an exact match, it might not be the first one due to case
# We keep checking as long as the lengths match
for match in possible_matches:
Expand Down Expand Up @@ -108,7 +107,7 @@ def search_tag(self, text, subreddit="vim"):

# Extract the matched part
matched_string = tag[match_index: match_index + len(text)]

print(matched_string)
# How many alphanumeric characters matched exactly.
matched_alpha_numeric_characters = 0
for i in range(len(text)):
Expand Down
15 changes: 15 additions & 0 deletions test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ def test_punctuation_retry(self):
for tag in tags:
self.assertIn(tag, reply);

def test_star_and_bar(self):
"""
Test that bot can find tags with '*' and '|'
"""

bot = Bot()
tags = ["g*", "|"]
text = "Test comment: " + \
','.join(list(map(lambda t: "`:h {}` ".format(t), tags)))

reply = bot.create_comment(text, None, "vim")
self.assertNotEqual(reply, '')
self.assertIn("https://vimhelp.org/pattern.txt.html#gstar", reply)
self.assertIn("https://vimhelp.org/motion.txt.html#bar", reply)

def test_url_encoding(self):
"""
Test that bot url encodes /
Expand Down

0 comments on commit 855678c

Please sign in to comment.