forked from gleitz/howdoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_howdoi.py
182 lines (148 loc) · 10.2 KB
/
test_howdoi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
"""Tests for Howdoi."""
import os
import unittest
import re
from howdoi import howdoi
from pyquery import PyQuery as pq
class HowdoiTestCase(unittest.TestCase):
def call_howdoi(self, query):
parser = howdoi.get_parser()
args = vars(parser.parse_args(query.split(' ')))
return howdoi.howdoi(args)
def setUp(self):
self.queries = ['format date bash',
'print stack trace python',
'convert mp4 to animated gif',
'create tar archive',
'cat']
self.pt_queries = ['abrir arquivo em python',
'enviar email em django',
'hello world em c']
self.bad_queries = ['moe',
'mel']
def tearDown(self):
pass
def test_get_link_at_pos(self):
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 1),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 2),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 1),
'/howdoi')
self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 2),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/', '/questions/142/'], 1),
'/questions/42/')
def test_answers(self):
for query in self.queries:
self.assertTrue(self.call_howdoi(query))
for query in self.bad_queries:
self.assertTrue(self.call_howdoi(query))
os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
for query in self.pt_queries:
self.assertTrue(self.call_howdoi(query))
def test_answers_bing(self):
os.environ['HOWDOI_SEARCH_ENGINE'] = 'bing'
for query in self.queries:
self.assertTrue(self.call_howdoi(query))
for query in self.bad_queries:
self.assertTrue(self.call_howdoi(query))
os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
for query in self.pt_queries:
self.assertTrue(self.call_howdoi(query))
os.environ['HOWDOI_SEARCH_ENGINE'] = ''
def test_answer_links_using_l_option(self):
for query in self.queries:
response = self.call_howdoi(query + ' -l')
self.assertNotEqual(re.match('http.?://.*questions/\d.*', response, re.DOTALL), None)
def test_answer_links_using_all_option(self):
for query in self.queries:
response = self.call_howdoi(query + ' -a')
self.assertNotEqual(re.match('.*http.?://.*questions/\d.*', response, re.DOTALL), None)
def test_position(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
not_first_answer = self.call_howdoi(query + ' -p5')
self.assertNotEqual(first_answer, not_first_answer)
def test_all_text(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -a')
self.assertNotEqual(first_answer, second_answer)
self.assertNotEqual(re.match('.*Answer from http.?://.*', second_answer, re.DOTALL), None)
def test_multiple_answers(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -n3')
self.assertNotEqual(first_answer, second_answer)
def test_unicode_answer(self):
assert self.call_howdoi('make a log scale d3')
assert self.call_howdoi('python unittest -n3')
assert self.call_howdoi('parse html regex -a')
assert self.call_howdoi('delete remote git branch -a')
def test_colorize(self):
query = self.queries[0]
normal = self.call_howdoi(query)
colorized = self.call_howdoi('-c ' + query)
self.assertTrue(normal.find('[39;') is -1)
self.assertTrue(colorized.find('[39;') is not -1)
def test_get_text_without_links(self):
html = '''\n <p>The halting problem is basically a\n formal way of asking if you can tell\n whether or not an arbitrary program\n will eventually halt.</p>\n \n <p>In other words, can you write a\n program called a halting oracle,\n HaltingOracle(program, input), which\n returns true if program(input) would\n eventually halt, and which returns\n false if it wouldn't?</p>\n \n <p>The answer is: no, you can't.</p>\n'''
paragraph = pq(html)
expected_output = '''The halting problem is basically a\n formal way of asking if you can tell\n whether or not an arbitrary program\n will eventually halt.\n\n \n \nIn other words, can you write a\n program called a halting oracle,\n HaltingOracle(program, input), which\n returns true if program(input) would\n eventually halt, and which returns\n false if it wouldn't?\n\n \n \nThe answer is: no, you can't.\n\n'''
actual_output = howdoi.get_text(paragraph)
self.assertEqual(actual_output, expected_output)
def test_get_text_with_one_link(self):
html = '<p>It\'s a <a href="http://paulirish.com/2010/the-protocol-relative-url/">protocol-relative URL</a> (typically HTTP or HTTPS). So if I\'m on <code>http://example.org</code> and I link (or include an image, script, etc.) to <code>//example.com/1.png</code>, it goes to <code>http://example.com/1.png</code>. If I\'m on <code>https://example.org</code>, it goes to <code>https://example.com/1.png</code>.</p>'
paragraph = pq(html)
expected_output = "It's a [protocol-relative URL](http://paulirish.com/2010/the-protocol-relative-url/) (typically HTTP or HTTPS). So if I'm on http://example.org and I link (or include an image, script, etc.) to //example.com/1.png, it goes to http://example.com/1.png. If I'm on https://example.org, it goes to https://example.com/1.png."
actual_output = howdoi.get_text(paragraph)
self.assertEqual(actual_output, expected_output)
def test_get_text_with_multiple_links_test_one(self):
html = 'Here\'s a quote from <a href="http://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style#Links" rel="nofollow noreferrer">wikipedia\'s manual of style</a> section on links (but see also <a href="http://en.wikipedia.org/wiki/Wikipedia:External_links" rel="nofollow noreferrer">their comprehensive page on External Links</a>)'
paragraph = pq(html)
expected_output = "Here's a quote from [wikipedia's manual of style](http://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style#Links) section on links (but see also [their comprehensive page on External Links](http://en.wikipedia.org/wiki/Wikipedia:External_links))"
actual_output = howdoi.get_text(paragraph)
self.assertEqual(actual_output, expected_output)
def test_get_text_with_multiple_links_test_two(self):
html = 'For example, if I were to reference <a href="http://www.apple.com/" rel="nofollow noreferrer">apple.com</a> as the subject of a sentence - or to talk about <a href="http://www.apple.com/" rel="nofollow noreferrer">Apple\'s website</a> as the topic of conversation. This being different to perhaps recommendations for reading <a href="https://ux.stackexchange.com/q/14872/6046">our article about Apple\'s website</a>.'
paragraph = pq(html)
expected_output = "For example, if I were to reference [apple.com](http://www.apple.com/) as the subject of a sentence - or to talk about [Apple's website](http://www.apple.com/) as the topic of conversation. This being different to perhaps recommendations for reading [our article about Apple's website](https://ux.stackexchange.com/q/14872/6046)."
actual_output = howdoi.get_text(paragraph)
self.assertEqual(actual_output, expected_output)
def test_get_text_with_link_but_with_copy_duplicating_the_href(self):
html ='<a href="https://github.com/jquery/jquery/blob/56136897f241db22560b58c3518578ca1453d5c7/src/manipulation.js#L451" rel="nofollow noreferrer">https://github.com/jquery/jquery/blob/56136897f241db22560b58c3518578ca1453d5c7/src/manipulation.js#L451</a>'
paragraph = pq(html)
expected_output = 'https://github.com/jquery/jquery/blob/56136897f241db22560b58c3518578ca1453d5c7/src/manipulation.js#L451'
actual_output = howdoi.get_text(paragraph)
self.assertEqual(actual_output, expected_output)
def test_get_text_with_a_link_but_copy_is_within_nested_div(self):
html = 'If the function is from a source file available on the filesystem, then <a href="https://docs.python.org/3/library/inspect.html#inspect.getsource" rel="noreferrer"><code>inspect.getsource(foo)</code></a> might be of help:'
paragraph = pq(html)
expected_output = 'If the function is from a source file available on the filesystem, then [inspect.getsource(foo)](https://docs.python.org/3/library/inspect.html#inspect.getsource) might be of help:'
actual_output = howdoi.get_text(paragraph)
self.assertEqual(actual_output, expected_output)
def test_get_questions(self):
links = ['https://stackoverflow.com/questions/tagged/cat', 'http://rads.stackoverflow.com/amzn/click/B007KAZ166', 'https://stackoverflow.com/questions/40108569/how-to-get-the-last-line-of-a-file-using-cat-command']
expected_output = ['https://stackoverflow.com/questions/40108569/how-to-get-the-last-line-of-a-file-using-cat-command']
actual_output = howdoi._get_questions(links)
self.assertSequenceEqual(actual_output, expected_output)
class HowdoiTestCaseEnvProxies(unittest.TestCase):
def setUp(self):
self.temp_get_proxies = howdoi.getproxies
def tearDown(self):
howdoi.getproxies = self.temp_get_proxies
def test_get_proxies1(self):
def getproxies1():
proxies = {'http': 'wwwproxy.company.com',
'https': 'wwwproxy.company.com',
'ftp': 'ftpproxy.company.com'}
return proxies
howdoi.getproxies = getproxies1
filtered_proxies = howdoi.get_proxies()
self.assertTrue('http://' in filtered_proxies['http'])
self.assertTrue('http://' in filtered_proxies['https'])
self.assertTrue('ftp' not in filtered_proxies.keys())
if __name__ == '__main__':
unittest.main()