-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
94 lines (65 loc) · 3.04 KB
/
tests.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import unittest
from censor import (Censor,
NoKeywordProvidedError,
BadKeywordProvidedError,
NoPatternProvidedError,
NonIterableKeywordsError)
class TestCensorDefault(unittest.TestCase):
def setUp(self):
self._censor = Censor()
def tearDown(self):
self._censor = None
def test_init(self):
self.assertEquals(0, len(self._censor._keywords))
self.assertEquals(0, len(self._censor._patterns))
self.assertEquals('*', self._censor._mask)
def test_keyword_addition(self):
kw = 'shit'
self._censor.add_keyword(kw)
self.assertEqual(kw, self._censor._keywords[0].pattern)
def test_keywords_addition(self):
kws = ['shit', 'ass', 'turd']
self._censor.add_keywords(kws)
for kw in self._censor._keywords:
self.assertIn(kw.pattern, kws)
def test_keyword_sorting(self):
kw1 = 'shit'
kw2 = 'bastard'
self._censor.add_keyword(kw1)
self._censor.add_keyword(kw2)
self.assertEqual(kw2, self._censor._keywords[0].pattern)
self.assertEqual(kw1, self._censor._keywords[1].pattern)
def test_pattern_addition(self):
pt = r'[0-9]+'
self._censor.add_pattern(pt)
self.assertEqual(pt, self._censor._patterns[0].pattern)
def test_none_keyword_exception(self):
self.assertRaises(NoKeywordProvidedError, self._censor.add_keyword, None)
def test_bad_keyword_exception(self):
self.assertRaises(BadKeywordProvidedError, self._censor.add_keyword, 0.1)
def test_none_keywords_exception(self):
self.assertRaises(NonIterableKeywordsError, self._censor.add_keywords, None)
def test_wrong_keywords_exception(self):
self.assertRaises(NonIterableKeywordsError, self._censor.add_keywords, 0.1)
def test_wrong_object_in_keywords_exception(self):
self.assertRaises(BadKeywordProvidedError, self._censor.add_keywords, ['shit', 0.1])
def test_none_pattern_exception(self):
self.assertRaises(NoPatternProvidedError, self._censor.add_pattern, None)
def test_censor(self):
self._censor.add_keyword('fuck')
self._censor.add_keyword('dick')
self._censor.add_keyword('mother-fucker')
self._censor.add_pattern(r'[0-9]+')
text_to_censor = '897546974 hello you fuck, how are you dick and mother-fucker? 125'
censored_expected = '********* hello you ****, how are you **** and *************? ***'
censored_actual = self._censor.censor(text_to_censor)
self.assertEqual(censored_expected, censored_actual)
def test_censor_invariant_case(self):
self._censor.add_keyword('fuck')
self._censor.add_keyword('sex')
text_to_censor = 'Fuck you! I will not have sEx now.'
censored_expected = '**** you! I will not have *** now.'
censored_actual = self._censor.censor(text_to_censor)
self.assertEquals(censored_expected, censored_actual)