-
Notifications
You must be signed in to change notification settings - Fork 0
/
unittest_wc.py
349 lines (263 loc) · 18.1 KB
/
unittest_wc.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import unittest
import subprocess
from unittest.mock import patch
import wc
def cmd_command(flags, test_inputs):
return subprocess.check_output('python3 wc.py %s %s' % (flags, " ".join(test_inputs)), shell=True)
class SingleFileTestCases(unittest.TestCase):
def test_single_file_textfile1(self):
command = subprocess.check_output('python3 wc.py testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n'
self.assertEqual(command, expected_output)
def test_single_textfile2(self):
command = subprocess.check_output('python3 wc.py testinputs/test_2.txt', shell=True)
expected_output = b'\t 0\t 0\t 0\t testinputs/test_2.txt\n'
self.assertEqual(command, expected_output)
def test_single_textfile3(self):
command = subprocess.check_output('python3 wc.py testinputs/test_3.txt', shell=True)
expected_output = b'\t 1\t 8\t 43\t testinputs/test_3.txt\n'
return self.assertEqual(command, expected_output)
def test_python_file(self):
command = subprocess.check_output('python3 wc.py testinputs/test_1.py', shell=True)
expected_output = b'\t 6\t 28\t 492\t testinputs/test_1.py\n'
return self.assertEqual(command, expected_output)
class SingleFileMultipleFlagsTestCases(unittest.TestCase):
def test_single_file_with_word_flag(self):
command = subprocess.check_output('python3 wc.py -w testinputs/test_1.txt', shell=True)
expected_output = b'\t 21\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_line_flag(self):
command = subprocess.check_output('python3 wc.py -l testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t testinputs/test_1.txt\n'
self.assertEqual(command, expected_output)
def test_single_file_with_byte_flag(self):
command = subprocess.check_output('python3 wc.py -c testinputs/test_1.txt', shell=True)
expected_output = b'\t 355\t testinputs/test_1.txt\n'
return self.assertEqual(command,expected_output)
def test_single_file_with_max_line_length_flag(self):
command = subprocess.check_output('python3 wc.py -L testinputs/test_1.txt', shell=True)
expected_output = b'\t 74\t testinputs/test_1.txt\n'
return self.assertEqual(command,expected_output)
def test_single_file_with_line_bytes_flags(self):
command = subprocess.check_output('python3 wc.py -l -c testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 355\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_word_bytes_flags(self):
command = subprocess.check_output('python3 wc.py -w -c testinputs/test_1.txt', shell=True)
expected_output = b'\t 21\t 355\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_word_line_byte_flags(self):
command = subprocess.check_output('python3 wc.py -w -c -l testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_combined_line_word_flags(self):
command = subprocess.check_output('python3 wc.py -wl testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 21\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_combined_word_byte_flags(self):
command = subprocess.check_output('python3 wc.py -wc testinputs/test_1.txt', shell=True)
expected_output = b'\t 21\t 355\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_combined_line_byte_flags(self):
command = subprocess.check_output('python3 wc.py -lc testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 355\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_combined_line_byte_word_flags(self):
command = subprocess.check_output('python3 wc.py -lcw testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n'
return self.assertEqual(command, expected_output)
def test_single_file_with_combined_maxline_word_byte_line_flag(self):
command = subprocess.check_output('python3 wc.py -lcwL testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t 74\t testinputs/test_1.txt\n'
return self.assertEqual(command,expected_output)
def test_single_file_with_combined_maxline_line_flag(self):
command = subprocess.check_output('python3 wc.py -lL testinputs/test_1.txt', shell=True)
expected_output = b'\t 4\t 74\t testinputs/test_1.txt\n'
return self.assertEqual(command,expected_output)
def test_single_file_with_combined_maxline_byte_flag(self):
command = subprocess.check_output('python3 wc.py -Lc testinputs/test_1.txt', shell=True)
expected_output = b'\t 355\t 74\t testinputs/test_1.txt\n'
return self.assertEqual(command,expected_output)
def test_single_file_with_combined_maxline_maxline_flag(self):
command = subprocess.check_output('python3 wc.py -LL testinputs/test_1.txt', shell=True)
expected_output = b'\t 74\t testinputs/test_1.txt\n'
return self.assertEqual(command,expected_output)
def test_single_file_with_combined_maxline_word_flag(self):
command = subprocess.check_output('python3 wc.py -Lw testinputs/test_1.txt', shell=True)
expected_output = b'\t 21\t 74\t testinputs/test_1.txt\n'
return self.assertEqual(command,expected_output)
class MissingArgumentsTestCases(unittest.TestCase):
def test_empty_flag(self):
command = subprocess.check_output('python3 wc.py - testinputs/test_1.txt', shell=True)
expected_output = b"we don't handle that situation yet!\n\t 4\t 21\t 355\t testinputs/test_1.txt\n\t 4\t 21\t 355\t total\n"
return self.assertEqual(command, expected_output)
def test_missing_flag(self):
command = subprocess.check_output('python3 wc.py -', shell=True)
expected_output = b"we don't handle that situation yet!\n"
return self.assertEqual(command, expected_output)
class MultipleFilesNoFlagsTestCases(unittest.TestCase):
def test_multiple_files_without_flag(self):
command = subprocess.check_output('python3 wc.py testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n\t 1\t 8\t 43\t testinputs/test_3.txt\n\t 5\t 29\t 398\t total\n'
return self.assertEqual(command, expected_output)
class MultipleFilesWithFlagsTestCases(unittest.TestCase):
def test_multiple_files_with_line_flag(self):
command = subprocess.check_output('python3 wc.py -l testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t testinputs/test_1.txt\n\t 1\t testinputs/test_3.txt\n\t 5\t total\n'
return self.assertEqual(command, expected_output)
def test_multiple_files_with_line_word_flag(self):
command = subprocess.check_output('python3 wc.py -l -w testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 21\t testinputs/test_1.txt\n\t 1\t 8\t testinputs/test_3.txt\n\t 5\t 29\t total\n'
return self.assertEqual(command, expected_output)
def test_multiple_files_with_combined_line_word_flag(self):
command = subprocess.check_output('python3 wc.py -lw testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 21\t testinputs/test_1.txt\n\t 1\t 8\t testinputs/test_3.txt\n\t 5\t 29\t total\n'
return self.assertEqual(command, expected_output)
def test_multiple_files_with_combined_line_byte_flag(self):
command = subprocess.check_output('python3 wc.py -lc testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 355\t testinputs/test_1.txt\n\t 1\t 43\t testinputs/test_3.txt\n\t 5\t 398\t total\n'
return self.assertEqual(command, expected_output)
def test_multiple_files_with_line_word_byte_flag(self):
command = subprocess.check_output('python3 wc.py -l -w -c testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n\t 1\t 8\t 43\t testinputs/test_3.txt\n\t 5\t 29\t 398\t total\n'
return self.assertEqual(command, expected_output)
def test_multiple_files_with_combined_line_word_byte_flag(self):
command = subprocess.check_output('python3 wc.py -lwc testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n\t 1\t 8\t 43\t testinputs/test_3.txt\n\t 5\t 29\t 398\t total\n'
return self.assertEqual(command, expected_output)
@unittest.expectedFailure
def test_help_flag(self):
command = subprocess.check_output('python3 wc.py --help', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n\t 1\t 8\t 43\t testinputs/test_3.txt\n\t 5\t 29\t 398\t total\n'
return self.assertEqual(command,expected_output)
def test_version_flag(self):
command = subprocess.check_output('python3 wc.py --version', shell=True)
expected_output = b'wc.py 1.0\n'
return self.assertEqual(command,expected_output)
def test_multiple_files_with_combined_maxline_word_byte_line_flag(self):
command = subprocess.check_output('python3 wc.py -lcwL testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 21\t 355\t 74\t testinputs/test_1.txt\n\t 1\t 8\t 43\t 42\t testinputs/test_3.txt\n\t 5\t 29\t 398\t 42\t total\n'
return self.assertEqual(command,expected_output)
def test_multiple_files_with_combined_maxline_line_flag(self):
command = subprocess.check_output('python3 wc.py -lL testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 4\t 74\t testinputs/test_1.txt\n\t 1\t 42\t testinputs/test_3.txt\n\t 5\t 42\t total\n'
return self.assertEqual(command,expected_output)
def test_multiple_files_with_combined_maxline_word_flag(self):
command = subprocess.check_output('python3 wc.py -Lw testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 21\t 74\t testinputs/test_1.txt\n\t 8\t 42\t testinputs/test_3.txt\n\t 29\t 42\t total\n'
return self.assertEqual(command,expected_output)
def test_multiple_files_with_combined_maxline_byte_flag(self):
command = subprocess.check_output('python3 wc.py -Lc testinputs/test_1.txt testinputs/test_3.txt', shell=True)
expected_output = b'\t 355\t 74\t testinputs/test_1.txt\n\t 43\t 42\t testinputs/test_3.txt\n\t 398\t 42\t total\n'
return self.assertEqual(command,expected_output)
class BinaryFilesTestCases(unittest.TestCase):
@unittest.expectedFailure
def test_binary_file_with_text_file(self):
command = subprocess.check_output('python3 wc.py testinputs/test_1.txt testinputs/Bob_dylan.jpeg', shell=True)
expected_output = b'\t 4\t 21\t 355\t testinputs/test_1.txt\n\t 343\t 2728\t 60813 \t testinputs/Bob_dylan.jpeg\n\t 348\t 2749\t 61168\t total\n'
return self.assertEqual(command,expected_output)
@unittest.expectedFailure
def test_binary_file(self):
command = subprocess.check_output('python3 wc.py testinputs/Bob_dylan.jpeg', shell=True)
expected_output = b'\t 343\t 2728\t 60813 \t testinputs/Bob_dylan.jpeg\n'
return self.assertEqual(command, expected_output)
class UnicodeTestCases(unittest.TestCase):
def test_georgian_text(self):
command = subprocess.check_output('python3 wc.py testinputs/georgian.txt', shell=True)
expected_output = b'\t 7\t 44\t 1115\t testinputs/georgian.txt\n'
return self.assertEqual(command, expected_output)
def test_ethiopian_text(self):
command = subprocess.check_output('python3 wc.py testinputs/ethiopian.txt', shell=True)
expected_output = b'\t 18\t 83\t 1077\t testinputs/ethiopian.txt\n'
return self.assertEqual(command, expected_output)
def test_drawing_text(self):
command = subprocess.check_output('python3 wc.py testinputs/drawing_file.txt', shell=True)
expected_output = b'\t 7\t 101\t 1249\t testinputs/drawing_file.txt\n'
return self.assertEqual(command, expected_output)
def test_file_without_extension(self):
command = subprocess.check_output('python3 wc.py testinputs/no_extension', shell=True)
expected_output = b'\t 8\t 54\t 718\t testinputs/no_extension\n'
return self.assertEqual(command, expected_output)
def test_unicode_dot_py(self):
command = subprocess.check_output('python3 wc.py testinputs/test_unicode.py', shell=True)
expected_output = b'\t 4\t 26\t 654\t testinputs/test_unicode.py\n'
return self.assertEqual(command, expected_output)
class SdtInTestCases(unittest.TestCase):
@patch('builtins.input', return_value="Hi Arnold")
def test_one_line_stdin(self, input):
command = subprocess.check_output('python3 wc.py', shell=True)
expected_output = b'\t 0 \t 2 \t 9\n'
return self.assertEqual(command, expected_output)
class CharacterFlagCombinationSingleFileTestCases(unittest.TestCase):
test_inputs = ['testinputs/test_3.txt']
def test_char_lines_combination(self):
output = b'\t 1\t 43\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-ml", self.test_inputs), output)
def test_char_byte_combination(self):
output = b'\t 43\t 43\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-mc", self.test_inputs), output)
def test_char_word_combination(self):
output = b'\t 8\t 43\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-mw", self.test_inputs), output)
def test_char_char_combination(self):
output = b'\t 43\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-mm", self.test_inputs), output)
def test_char_line_byte_combination(self):
output = b'\t 1\t 43\t 43\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-mlc", self.test_inputs), output)
def test_char_line_word_combination(self):
output = b'\t 1\t 8\t 43\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-mlw", self.test_inputs), output)
def test_char_line_word_combination(self):
output = b'\t 1\t 43\t 42\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-mlL", self.test_inputs), output)
def test_char_line_word_byte_combination(self):
output = b'\t 1\t 8\t 43\t 43\t 42\t testinputs/test_3.txt\n'
return self.assertEqual(cmd_command("-mlLcw", self.test_inputs), output)
class CharacterFlagCombinationMultipleFilesTestCases(unittest.TestCase):
test_inputs = ['testinputs/test_3.txt', 'testinputs/test_1.txt']
def test_char_lines_combination(self):
output = b'\t 1\t 43\t testinputs/test_3.txt\n\t 4\t 355\t ' \
b'testinputs/test_1.txt\n\t 5\t 398\t total\n'
return self.assertEqual(cmd_command("-ml", self.test_inputs), output)
def test_char_byte_combination(self):
output = b'\t 43\t 43\t testinputs/test_3.txt\n\t 355\t 355\t ' \
b'testinputs/test_1.txt\n\t 398\t 398\t total\n'
return self.assertEqual(cmd_command("-mc", self.test_inputs), output)
def test_char_word_combination(self):
output = b'\t 8\t 43\t testinputs/test_3.txt\n\t 21\t 355\t ' \
b'testinputs/test_1.txt\n\t 29\t 398\t total\n'
return self.assertEqual(cmd_command("-mw", self.test_inputs), output)
def test_char_char_combination(self):
output = b'\t 43\t testinputs/test_3.txt\n\t 355\t ' \
b'testinputs/test_1.txt\n\t 398\t total\n'
return self.assertEqual(cmd_command("-mm", self.test_inputs), output)
def test_char_line_byte_combination(self):
output = b'\t 1\t 43\t 43\t testinputs/test_3.txt\n\t 4\t 355\t 355\t ' \
b'testinputs/test_1.txt\n\t 5\t 398\t 398\t total\n'
return self.assertEqual(cmd_command("-mlc", self.test_inputs), output)
def test_char_line_word_combination(self):
output = b'\t 1\t 8\t 43\t testinputs/test_3.txt\n\t 4\t 21\t 355\t ' \
b'testinputs/test_1.txt\n\t 5\t 29\t 398\t total\n'
return self.assertEqual(cmd_command("-mlw", self.test_inputs), output)
def test_char_line_word_combination(self):
output = b'\t 1\t 43\t 42\t testinputs/test_3.txt\n\t 4\t 355\t 74\t ' \
b'testinputs/test_1.txt\n\t 5\t 398\t 74\t total\n'
return self.assertEqual(cmd_command("-mlL", self.test_inputs), output)
def test_char_line_word_byte_combination(self):
output = b'\t 1\t 8\t 43\t 43\t 42\t ' \
b'testinputs/test_3.txt\n\t 4\t 21\t 355\t 355\t 74\t testinputs/test_1.txt\n\t 5\t 29\t 398\t 398\t 74\t total\n'
return self.assertEqual(cmd_command("-mlLcw", self.test_inputs), output)
class Files0FromTestCases(unittest.TestCase):
testinput = ['files0-from.txt']
def test_files0from(self):
output = b"\t 344\t 1490\t 60813\t testinputs/Bob_dylan.jpeg\n\t 7\t 101\t 1249\t " \
b"testinputs/drawing_file.txt\n\t 19\t 83\t 1077\t testinputs/ethiopian.txt\n\t 199\t 993\t 13245\t " \
b"testinputs/file_1.txt\n\t 7\t 44\t 1115\t testinputs/georgian.txt\n\t 1\t 1\t 2\t " \
b"testinputs/new_line.txt\n\t 8\t 54\t 718\t testinputs/no_extension\n\t 7\t 28\t 492\t t" \
b"estinputs/test_1.py\n\t 5\t 21\t 355\t testinputs/test_1.txt\n\t 1\t 0\t 0\t " \
b"testinputs/test_2.txt\n\t 2\t 8\t 43\t testinputs/test_3.txt\n\t 5\t 26\t 654\t " \
b"testinputs/test_unicode.py\nwe don't handle that situation yet!\n\t 605\t 2849\t 79763\t total\n"
return self.assertEqual(cmd_command("--files0-from", self.testinput), output)
if __name__ == '__main__':
unittest.main()