Skip to content

Commit 8aa649b

Browse files
authored
Merge pull request #6 from iyanmv/fix-regex
Fix regex
2 parents 765e530 + c107df6 commit 8aa649b

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

arxivbot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from maubot import Plugin
55
from maubot.handlers import command
66

7+
regex_expr = r'(https?://arxiv\.org/abs/(?:[a-z\-]*/)?[0-9]+\.?[0-9]+)'
78

89
class ArXivBot(Plugin):
910

@@ -49,7 +50,7 @@ async def _parse_arxiv(self, url):
4950
authors = ", ".join(authors)
5051
return {"title": title, "authors": authors, "date": date, "abstract": abstract, "pdf": pdf}
5152

52-
@command.passive(r'(https?://arxiv\.org/abs/(?:quant-ph/)?[0-9\.]+)', multiple=True, multiline=True)
53+
@command.passive(regex_expr, multiple=True, multiline=True)
5354
async def arxiv(self, evt, matches):
5455
for _, match in matches:
5556
d = await self._parse_arxiv(match)

maubot.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
maubot: 0.2.1
22
id: com.iyanmv.arxivbot
3-
version: 0.0.5
3+
version: 0.0.6
44
license: GPL-3.0-only
55
modules:
66
- arxivbot

test_regex.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import unittest
2+
import re
3+
4+
with open('arxivbot.py', 'r') as f:
5+
mod = f.readlines()
6+
regex_expr = mod[6].split("'")[1]
7+
8+
9+
class TestRegexExpressionOneMatch(unittest.TestCase):
10+
def setUp(self):
11+
self.r = re.compile(regex_expr)
12+
self.match = "https://arxiv.org/abs/1109.3195"
13+
def test_single_url(self):
14+
text = "https://arxiv.org/abs/1109.3195"
15+
self.assertEqual(self.r.search(text).group(), self.match)
16+
def test_single_url_period(self):
17+
text = "https://arxiv.org/abs/1109.3195."
18+
self.assertEqual(self.r.search(text).group(), self.match)
19+
def test_single_url_inside_text(self):
20+
text = "Hey! Check this paper: https://arxiv.org/abs/1109.3195. What do you think?"
21+
self.assertEqual(self.r.search(text).group(), self.match)
22+
def test_single_url_multiple_lines(self):
23+
text = "\nhttps://arxiv.org/abs/1109.3195\n\n"
24+
self.assertEqual(self.r.search(text).group(), self.match)
25+
def test_single_url_parenthesis(self):
26+
text = "This paper (https://arxiv.org/abs/1109.3195) looks interesting."
27+
self.assertEqual(self.r.search(text).group(), self.match)
28+
29+
class TestRegexExpressionOldURLs(unittest.TestCase):
30+
def setUp(self):
31+
self.r = re.compile(regex_expr)
32+
def test_quant_ph(self):
33+
text = "https://arxiv.org/abs/quant-ph/0512258"
34+
self.assertEqual(self.r.search(text).group(), text)
35+
def test_hep_ex(self):
36+
text = "https://arxiv.org/abs/hep-ex/0102001"
37+
self.assertEqual(self.r.search(text).group(), text)
38+
def test_math_ph(self):
39+
text = "https://arxiv.org/abs/math-ph/9810001"
40+
self.assertEqual(self.r.search(text).group(), text)
41+
def test_cs(self):
42+
text = "https://arxiv.org/abs/cs/9902001"
43+
self.assertEqual(self.r.search(text).group(), text)
44+
45+
class TestRegexExpressionMultipleMatches(unittest.TestCase):
46+
def setUp(self):
47+
self.r = re.compile(regex_expr)
48+
self.matches = ["https://arxiv.org/abs/quant-ph/0512258", "https://arxiv.org/abs/quant-ph/9806051",
49+
"https://arxiv.org/abs/0810.4372", "https://arxiv.org/abs/quant-ph/9810080"]
50+
def test_multiple_matches_simple(self):
51+
text = "https://arxiv.org/abs/quant-ph/0512258 https://arxiv.org/abs/quant-ph/9806051 "\
52+
+ "https://arxiv.org/abs/0810.4372 https://arxiv.org/abs/quant-ph/9810080"
53+
self.assertEqual(self.r.findall(text), self.matches)
54+
def test_multiple_matches_no_spaces(self):
55+
text = "https://arxiv.org/abs/quant-ph/0512258https://arxiv.org/abs/quant-ph/9806051"\
56+
+ "https://arxiv.org/abs/0810.4372https://arxiv.org/abs/quant-ph/9810080"
57+
self.assertEqual(self.r.findall(text), self.matches)
58+
def test_multiple_matches_paragraph(self):
59+
text = """https://arxiv.org/abs/quant-ph/0512258. Also, did you read this (https://arxiv.org/abs/quant-ph/9806051)?
60+
And what about this other one: https://arxiv.org/abs/0810.4372
61+
https://arxiv.org/abs/quant-ph/9810080"""
62+
self.assertEqual(self.r.findall(text), self.matches)
63+
64+
65+
if __name__ == '__main__':
66+
unittest.main()

0 commit comments

Comments
 (0)