-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
42 lines (30 loc) · 1.63 KB
/
test.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
import timeit
from palindrome import Palindrome
from unittest import TestCase, main
class TestPalindrome(TestCase):
def test_is_valid_if_symmetric(self):
assert Palindrome('amor roma').is_valid()
def test_is_invalid_if_not_symmetric(self):
assert not Palindrome('ramor romaa').is_valid()
def test_is_valid_regardless_of_casing(self):
assert Palindrome('Amor Roma').is_valid()
def test_is_valid_as_it_ignores_non_alphabetical_characters(self):
assert Palindrome('No ‘x’ in Nixon').is_valid()
def test_is_valid_even_if_arabic(self):
assert Palindrome('توت').is_valid() # toot in arabic
def test_is_invalid_if_empty_string(self):
assert not Palindrome('').is_valid()
def test_remembers_valid_palindromes_in_cache(self):
Palindrome('this goes in the cache ehcac eht ni seog siht').is_valid()
assert Palindrome.cache['thisgoesinthecacheehcacehtniseogsiht'] == True
def test_remembers_invalid_palindromes_in_cache(self):
Palindrome('this also goes in the cache').is_valid()
assert Palindrome.cache['thisalsogoesinthecache'] == False
def test_raises_type_error_if_non_string_is_supplied(self):
with self.assertRaises(TypeError):
Palindrome(['a', 'm', 'm', 'a']).is_valid()
def test_it_is_fast(self):
timing_test = 'Palindrome("Are we not pure? “No sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man; a prisoner up to new era.").is_valid()'
timeit.timeit(timing_test, 'from palindrome import Palindrome', number=1000)
if __name__ == '__main__':
main(buffer=True)