/
test_util.py
130 lines (105 loc) · 2.81 KB
/
test_util.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pytest
from nltk.util import everygrams, usage
def test_usage_with_self(capsys):
class MyClass:
# sorted alphabetically
def kwargs(self, a=1):
...
def no_args(self):
...
def pos_args(self, a, b):
...
def pos_args_and_kwargs(self, a, b, c=1):
...
usage(MyClass)
captured = capsys.readouterr()
assert captured.out == (
"MyClass supports the following operations:\n"
" - self.kwargs(a=1)\n"
" - self.no_args()\n"
" - self.pos_args(a, b)\n"
" - self.pos_args_and_kwargs(a, b, c=1)\n"
)
def test_usage_with_cls(capsys):
class MyClass:
# sorted alphabetically
@classmethod
def clsmethod(cls):
...
@classmethod
def clsmethod_with_args(cls, a, b, c=1):
...
usage(MyClass)
captured = capsys.readouterr()
assert captured.out == (
"MyClass supports the following operations:\n"
" - cls.clsmethod()\n"
" - cls.clsmethod_with_args(a, b, c=1)\n"
)
@pytest.fixture
def everygram_input():
"""Form test data for tests."""
return iter(["a", "b", "c"])
def test_everygrams_without_padding(everygram_input):
expected_output = [
("a",),
("a", "b"),
("a", "b", "c"),
("b",),
("b", "c"),
("c",),
]
output = list(everygrams(everygram_input))
assert output == expected_output
def test_everygrams_max_len(everygram_input):
expected_output = [
("a",),
("a", "b"),
("b",),
("b", "c"),
("c",),
]
output = list(everygrams(everygram_input, max_len=2))
assert output == expected_output
def test_everygrams_min_len(everygram_input):
expected_output = [
("a", "b"),
("a", "b", "c"),
("b", "c"),
]
output = list(everygrams(everygram_input, min_len=2))
assert output == expected_output
def test_everygrams_pad_right(everygram_input):
expected_output = [
("a",),
("a", "b"),
("a", "b", "c"),
("b",),
("b", "c"),
("b", "c", None),
("c",),
("c", None),
("c", None, None),
(None,),
(None, None),
(None,),
]
output = list(everygrams(everygram_input, max_len=3, pad_right=True))
assert output == expected_output
def test_everygrams_pad_left(everygram_input):
expected_output = [
(None,),
(None, None),
(None, None, "a"),
(None,),
(None, "a"),
(None, "a", "b"),
("a",),
("a", "b"),
("a", "b", "c"),
("b",),
("b", "c"),
("c",),
]
output = list(everygrams(everygram_input, max_len=3, pad_left=True))
assert output == expected_output