Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b8a14d

Browse files
committed
Separate tests for gzip.GzipFile and gzip.open.
1 parent 7e12620 commit 1b8a14d

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

Lib/test/test_gzip.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def seek(self, *args):
3333
raise io.UnsupportedOperation
3434

3535

36-
class TestGzip(unittest.TestCase):
36+
class BaseTest(unittest.TestCase):
3737
filename = support.TESTFN
3838

3939
def setUp(self):
@@ -43,6 +43,7 @@ def tearDown(self):
4343
support.unlink(self.filename)
4444

4545

46+
class TestGzip(BaseTest):
4647
def test_write(self):
4748
with gzip.GzipFile(self.filename, 'wb') as f:
4849
f.write(data1 * 50)
@@ -115,14 +116,14 @@ def test_many_append(self):
115116
# Bug #1074261 was triggered when reading a file that contained
116117
# many, many members. Create such a file and verify that reading it
117118
# works.
118-
with gzip.open(self.filename, 'wb', 9) as f:
119+
with gzip.GzipFile(self.filename, 'wb', 9) as f:
119120
f.write(b'a')
120121
for i in range(0, 200):
121-
with gzip.open(self.filename, "ab", 9) as f: # append
122+
with gzip.GzipFile(self.filename, "ab", 9) as f: # append
122123
f.write(b'a')
123124

124125
# Try reading the file
125-
with gzip.open(self.filename, "rb") as zgfile:
126+
with gzip.GzipFile(self.filename, "rb") as zgfile:
126127
contents = b""
127128
while 1:
128129
ztxt = zgfile.read(8192)
@@ -374,10 +375,9 @@ def test_decompress(self):
374375
datac = gzip.compress(data)
375376
self.assertEqual(gzip.decompress(datac), data)
376377

377-
# Test the 'open' convenience function.
378378

379-
def test_open_binary(self):
380-
# Test explicit binary modes.
379+
class TestOpen(BaseTest):
380+
def test_binary_modes(self):
381381
uncompressed = data1 * 50
382382
with gzip.open(self.filename, "wb") as f:
383383
f.write(uncompressed)
@@ -392,7 +392,7 @@ def test_open_binary(self):
392392
file_data = gzip.decompress(f.read())
393393
self.assertEqual(file_data, uncompressed * 2)
394394

395-
def test_open_default_binary(self):
395+
def test_implicit_binary_modes(self):
396396
# Test implicit binary modes (no "b" or "t" in mode string).
397397
uncompressed = data1 * 50
398398
with gzip.open(self.filename, "w") as f:
@@ -408,8 +408,7 @@ def test_open_default_binary(self):
408408
file_data = gzip.decompress(f.read())
409409
self.assertEqual(file_data, uncompressed * 2)
410410

411-
def test_open_text(self):
412-
# Test text modes.
411+
def test_text_modes(self):
413412
uncompressed = data1.decode("ascii") * 50
414413
with gzip.open(self.filename, "wt") as f:
415414
f.write(uncompressed)
@@ -424,7 +423,7 @@ def test_open_text(self):
424423
file_data = gzip.decompress(f.read()).decode("ascii")
425424
self.assertEqual(file_data, uncompressed * 2)
426425

427-
def test_open_bad_params(self):
426+
def test_bad_params(self):
428427
# Test invalid parameter combinations.
429428
with self.assertRaises(ValueError):
430429
gzip.open(self.filename, "wbt")
@@ -435,7 +434,7 @@ def test_open_bad_params(self):
435434
with self.assertRaises(ValueError):
436435
gzip.open(self.filename, "rb", newline="\n")
437436

438-
def test_open_with_encoding(self):
437+
def test_encoding(self):
439438
# Test non-default encoding.
440439
uncompressed = data1.decode("ascii") * 50
441440
with gzip.open(self.filename, "wt", encoding="utf-16") as f:
@@ -446,15 +445,15 @@ def test_open_with_encoding(self):
446445
with gzip.open(self.filename, "rt", encoding="utf-16") as f:
447446
self.assertEqual(f.read(), uncompressed)
448447

449-
def test_open_with_encoding_error_handler(self):
448+
def test_encoding_error_handler(self):
450449
# Test with non-default encoding error handler.
451450
with gzip.open(self.filename, "wb") as f:
452451
f.write(b"foo\xffbar")
453452
with gzip.open(self.filename, "rt", encoding="ascii", errors="ignore") \
454453
as f:
455454
self.assertEqual(f.read(), "foobar")
456455

457-
def test_open_with_newline(self):
456+
def test_newline(self):
458457
# Test with explicit newline (universal newline mode disabled).
459458
uncompressed = data1.decode("ascii") * 50
460459
with gzip.open(self.filename, "wt") as f:
@@ -463,7 +462,7 @@ def test_open_with_newline(self):
463462
self.assertEqual(f.readlines(), [uncompressed])
464463

465464
def test_main(verbose=None):
466-
support.run_unittest(TestGzip)
465+
support.run_unittest(TestGzip, TestOpen)
467466

468467
if __name__ == "__main__":
469468
test_main(verbose=True)

0 commit comments

Comments
 (0)