-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_abbrev.py
executable file
·59 lines (46 loc) · 1.78 KB
/
test_abbrev.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
from unittest import TestCase
import abbrev
class TestAbbrev(TestCase):
def test_function(self):
d = {'one': 1, 'two': 2, 'three': 3}
abr = abbrev(d)
assert abbrev(d, 'o') == abr('on') == abr('one') == 1
assert abbrev(d, 'two') == abr('tw') == 2
assert abbrev(d, 'th') == abr('three') == 3
def test_sequence(self):
d = 'one', 'two', 'three'
abr = abbrev(d)
assert abbrev(d, 'o') == abr('on') == abr('one') == 'one'
assert abbrev(d, 'two') == abr('tw') == 'two'
assert abbrev(d, 'th') == abr('three') == 'three'
def test_error(self):
abr = abbrev({'one': 1, 'two': 2, 'three': 3})
with self.assertRaises(KeyError) as m:
abr('four')
assert m.exception.args == ('four',)
with self.assertRaises(KeyError) as m:
abr('t')
assert m.exception.args == ('t', "was ambiguous: ('two', 'three')")
def test_unique(self):
abr = abbrev({'one': 1, 'two': 2, 'three': 3}, unique=False)
assert abr('t') == 2
assert abr('o') == 1
with self.assertRaises(KeyError):
abr('t', unique=True)
def test_multi(self):
d = {'one': 1, 'two': 2, 'three': 3}
abr = abbrev(d, multi=True)
assert abr('one') == (1,)
assert abr('on') == (1,)
assert abr('tw') == (2,)
assert abr('three') == (3,)
assert abr('t') == (2, 3)
assert abr('') == (1, 2, 3)
def test_default(self):
d = {'one': 1, 'two': 2, 'three': 3}
abr = abbrev(d, default=0)
assert abr('on') == abr('one') == 1
assert abr('two') == abr('tw') == 2
assert abr('th') == abr('three') == 3
assert abr('missing') == 0
assert abr('missing', multi=True) == []