Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added context managers #3

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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