-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhashtag.py
More file actions
52 lines (37 loc) · 1.4 KB
/
hashtag.py
File metadata and controls
52 lines (37 loc) · 1.4 KB
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
"""
hashtag.py
Simple example of test code using Python's built-in unittest module.
"""
import unittest
class BadInputError(Exception):
pass
def split_hashtag(hashtag):
"""
Split a hashtag into a string of space-separated words
if the hashtag uses camelCase.
e.g. '#thisKindOfHashTag' --> 'this Kind Of Hash Tag'
"""
if not type(hashtag) == str or not hashtag.startswith('#'):
raise BadInputError('split_hashtag needs a string starting "#"')
out = []
for c in hashtag[1:]:
if c.isupper() and out != []:
out.append(' ')
out.append(c)
return ''.join(out)
class TestHashTagSplitter(unittest.TestCase):
def test_simple_camelCase_hashtag(self):
self.assertEqual(split_hashtag('#thisKindOfHashTag'),
'this Kind Of Hash Tag')
def test_simple_CamelCase_hashtag(self):
self.assertEqual(split_hashtag('#ThisKindOfHashTag'),
'This Kind Of Hash Tag')
def testEmptyInputs(self):
self.assertEqual(split_hashtag('#'), '')
def testBadInputs(self):
self.assertRaises(BadInputError, split_hashtag, '')
self.assertRaises(BadInputError, split_hashtag, 'no hash')
self.assertRaises(BadInputError, split_hashtag, 1)
self.assertRaises(BadInputError, split_hashtag, None)
if __name__ == '__main__':
unittest.main()