diff --git a/plugins/hackernews.py b/plugins/hackernews.py index 1b53167..0ac7b6b 100644 --- a/plugins/hackernews.py +++ b/plugins/hackernews.py @@ -1,3 +1,5 @@ +import unittest + from util import http, hook @@ -7,8 +9,27 @@ def hackernews(match): entry = http.get_json(base_api + match.group(1) + ".json") if entry['type'] == "story": - return "{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry) + entry['title'] = http.unescape(entry['title']) + return u"{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry) if entry['type'] == "comment": - return '"{text}" -- {by}'.format(**entry) + entry['text'] = http.unescape(entry['text']) + return u'"{text}" -- {by}'.format(**entry) +class Test(unittest.TestCase): + def news(self, inp): + re = hackernews._hook[0][1][1]['re'] + return hackernews(re.search(inp)) + + def test_story(self): + assert 'Desalination' in self.news('https://news.ycombinator.com/item?id=9943431') + + def test_comment(self): + res = self.news('https://news.ycombinator.com/item?id=9943987') + assert 'kilowatt hours' in res + assert 'oaktowner' in res + + def test_comment_encoding(self): + res = self.news('https://news.ycombinator.com/item?id=9943897') + assert 'abominations' in res + assert '> ' in res # encoding