forked from reingart/exercism
-
Notifications
You must be signed in to change notification settings - Fork 4
/
strain_test.py
46 lines (34 loc) · 1.43 KB
/
strain_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
43
44
45
46
import unittest
from strain import keep, discard
class StrainTest(unittest.TestCase):
def test_empty_sequence(self):
self.assertEqual(keep([], lambda x: x % 2 == 0), [])
def test_empty_keep(self):
inp = [2, 4, 6, 8, 10]
out = []
self.assertEqual(keep(inp, lambda x: x % 2 == 1), out)
def test_empty_discard(self):
inp = [2, 4, 6, 8, 10]
out = []
self.assertEqual(discard(inp, lambda x: x % 2 == 0), out)
def test_keep_everything(self):
inp = [2, 4, 6, 8, 10]
self.assertEqual(keep(inp, lambda x: x % 2 == 0), inp)
def test_discard_endswith(self):
inp = ['dough', 'cash', 'plough', 'though', 'through', 'enough']
out = ['cash']
self.assertEqual(discard(inp, lambda x: str.endswith(x, 'ough')), out)
def test_keep_z(self):
inp = ['zebra', 'arizona', 'apple', 'google', 'mozilla']
out = ['zebra', 'arizona', 'mozilla']
self.assertEqual(keep(inp, lambda x: 'z' in x), out)
def test_keep_discard(self):
inp = ['1,2,3', 'one', 'almost!', 'love']
self.assertEqual(discard(keep(inp, str.isalpha), str.isalpha), [])
def test_keep_plus_discard(self):
inp = ['1,2,3', 'one', 'almost!', 'love']
out = ['one', 'love', '1,2,3', 'almost!']
self.assertEqual(
keep(inp, str.isalpha) + discard(inp, str.isalpha), out)
if __name__ == '__main__':
unittest.main()