|
| 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