Skip to content

Commit

Permalink
Added context managers
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 22, 2021
1 parent da503f6 commit 5e63097
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions Tests/test_file_ppm.py
Expand Up @@ -114,17 +114,19 @@ def test_plain_pbm_truncated_data(tmp_path):
with open(path, "wb") as f:
f.write(b"P1\n128 128\n")

with pytest.raises(ValueError):
Image.open(path).load()
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_pbm_invalid_data(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P1\n128 128\n1009")

with pytest.raises(ValueError):
Image.open(path).load()
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_data_with_comments(tmp_path):
Expand All @@ -146,44 +148,49 @@ def test_plain_ppm_truncated_data(tmp_path):
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n")

with pytest.raises(ValueError):
Image.open(path).load()
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_invalid_data(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n100A")

with pytest.raises(ValueError):
Image.open(path).load()
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_half_token_too_long(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n012345678910")

with pytest.raises(ValueError):
Image.open(path).load()
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_token_too_long(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n012345678910 0")

with pytest.raises(ValueError):
Image.open(path).load()
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_value_too_large(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n256")

with pytest.raises(ValueError):
Image.open(path).load()
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_not_ppm(tmp_path):
Expand All @@ -206,7 +213,8 @@ def test_nondecimal_header(tmp_path):
f.write(b"P6\n128\x00")

with pytest.raises(ValueError):
Image.open(path)
with Image.open(path):
pass


def test_header_token_too_long(tmp_path):
Expand All @@ -215,7 +223,8 @@ def test_header_token_too_long(tmp_path):
f.write(b"P6\n 012345678910")

with pytest.raises(ValueError):
Image.open(path)
with Image.open(path):
pass


def test_too_many_colors(tmp_path):
Expand All @@ -224,7 +233,8 @@ def test_too_many_colors(tmp_path):
f.write(b"P6\n1 1\n1000\n")

with pytest.raises(ValueError):
Image.open(path)
with Image.open(path):
pass


def test_truncated_header(tmp_path):
Expand Down

0 comments on commit 5e63097

Please sign in to comment.