2017-08-23 15:35:12 -05:00
|
|
|
from osclib.comments import CommentAPI
|
|
|
|
import re
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2018-01-03 16:24:21 -06:00
|
|
|
COMMENT = 'short comment'
|
|
|
|
COMMENT_INFO = {'foo': 'bar', 'distro': 'openSUSE'}
|
2018-01-03 14:21:24 -06:00
|
|
|
|
2018-01-03 16:24:21 -06:00
|
|
|
class TestComment(unittest.TestCase):
|
2017-08-23 15:35:12 -05:00
|
|
|
def setUp(self):
|
|
|
|
self.api = CommentAPI('bogus')
|
2018-01-03 14:21:24 -06:00
|
|
|
self.bot = type(self).__name__
|
|
|
|
self.comments = {
|
|
|
|
1: {'comment': '<!-- {} -->\n\nshort comment'.format(self.bot)},
|
|
|
|
2: {'comment': '<!-- {} foo=bar distro=openSUSE -->\n\nshort comment'.format(self.bot)}
|
|
|
|
}
|
2017-08-23 15:35:12 -05:00
|
|
|
|
|
|
|
def test_truncate(self):
|
|
|
|
comment = "string of text"
|
|
|
|
for i in xrange(len(comment) + 1):
|
|
|
|
truncated = self.api.truncate(comment, length=i)
|
|
|
|
print(truncated)
|
|
|
|
self.assertEqual(len(truncated), i)
|
|
|
|
|
|
|
|
def test_truncate_pre(self):
|
|
|
|
comment = """
|
|
|
|
Some text.
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
bar
|
|
|
|
mar
|
|
|
|
car
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
## section 2
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
more
|
|
|
|
lines
|
|
|
|
than
|
|
|
|
you
|
|
|
|
can
|
|
|
|
handle
|
|
|
|
</pre>
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
for i in xrange(len(comment) + len('...\n</pre>')):
|
|
|
|
truncated = self.api.truncate(comment, length=i)
|
|
|
|
print('=' * 80)
|
|
|
|
print(truncated)
|
|
|
|
self.assertTrue(len(truncated) <= i, '{} <= {}'.format(len(truncated), i))
|
|
|
|
self.assertEqual(truncated.count('<pre>'), truncated.count('</pre>'))
|
|
|
|
self.assertFalse(len(re.findall(r'</?\w+[^\w>]', truncated)))
|
|
|
|
tag_count = truncated.count('<pre>') + truncated.count('</pre>')
|
|
|
|
self.assertEqual(tag_count, truncated.count('<'))
|
|
|
|
self.assertEqual(tag_count, truncated.count('>'))
|
2018-01-03 14:21:24 -06:00
|
|
|
|
|
|
|
def test_add_marker(self):
|
2018-01-03 16:24:21 -06:00
|
|
|
comment_marked = self.api.add_marker(COMMENT, self.bot)
|
2018-01-03 14:21:24 -06:00
|
|
|
self.assertEqual(comment_marked, self.comments[1]['comment'])
|
|
|
|
|
2018-01-03 16:24:21 -06:00
|
|
|
comment_marked = self.api.add_marker(COMMENT, self.bot, COMMENT_INFO)
|
2018-01-03 14:21:24 -06:00
|
|
|
self.assertEqual(comment_marked, self.comments[2]['comment'])
|
2018-01-03 14:32:29 -06:00
|
|
|
|
|
|
|
def test_remove_marker(self):
|
2018-01-03 16:24:21 -06:00
|
|
|
comment = self.api.remove_marker(COMMENT)
|
|
|
|
self.assertEqual(comment, COMMENT)
|
2018-01-03 14:32:29 -06:00
|
|
|
|
|
|
|
comment = self.api.remove_marker(self.comments[1]['comment'])
|
2018-01-03 16:24:21 -06:00
|
|
|
self.assertEqual(comment, COMMENT)
|
2018-01-03 14:32:29 -06:00
|
|
|
|
|
|
|
comment = self.api.remove_marker(self.comments[2]['comment'])
|
2018-01-03 16:24:21 -06:00
|
|
|
self.assertEqual(comment, COMMENT)
|
2018-01-03 14:34:05 -06:00
|
|
|
|
|
|
|
def test_comment_find(self):
|
|
|
|
comment, info = self.api.comment_find(self.comments, self.bot)
|
|
|
|
self.assertEqual(comment, self.comments[1])
|
|
|
|
|
2018-01-03 16:24:21 -06:00
|
|
|
comment, info = self.api.comment_find(self.comments, self.bot, COMMENT_INFO)
|
2018-01-03 14:34:05 -06:00
|
|
|
self.assertEqual(comment, self.comments[2])
|
2018-01-03 16:24:21 -06:00
|
|
|
self.assertEqual(info, COMMENT_INFO)
|
2018-01-03 14:34:05 -06:00
|
|
|
|
2018-01-03 16:24:21 -06:00
|
|
|
info_partial = dict(COMMENT_INFO)
|
2018-01-03 14:34:05 -06:00
|
|
|
del info_partial['foo']
|
|
|
|
comment, info = self.api.comment_find(self.comments, self.bot, info_partial)
|
|
|
|
self.assertEqual(comment, self.comments[2])
|
2018-01-03 16:24:21 -06:00
|
|
|
self.assertEqual(info, COMMENT_INFO)
|