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 72328a8 commit e92893f
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions Tests/test_file_ppm.py
Expand Up @@ -112,17 +112,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 @@ -144,44 +146,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 @@ -204,7 +211,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 @@ -213,7 +221,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 @@ -222,7 +231,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 e92893f

Please sign in to comment.