imagecms.py test drafting

This commit is contained in:
Deekshu Kare 2024-06-22 17:30:46 +02:00
parent 26848c48a7
commit b235d0e1c1
3 changed files with 12 additions and 29 deletions

View File

@ -7,8 +7,6 @@ from PIL import PdfParser
from PIL import SpiderImagePlugin from PIL import SpiderImagePlugin
from PIL import MpegImagePlugin from PIL import MpegImagePlugin
from PIL import ImageCms from PIL import ImageCms
from PIL import McIdasImagePlugin
from PIL import ImageTk
from PIL import ImageFile from PIL import ImageFile
pytest_plugins = ["Tests.helper"] pytest_plugins = ["Tests.helper"]
@ -21,7 +19,7 @@ def calculate_coverage(test_name):
"branches3": SpiderImagePlugin.branches, # isidora "branches3": SpiderImagePlugin.branches, # isidora
"branches4": MpegImagePlugin.BitStream.branches, # isidora "branches4": MpegImagePlugin.BitStream.branches, # isidora
"branches5": ImageCms.branches, # deekshu "branches5": ImageCms.branches, # deekshu
"branches6": ImageFile.branches, # deekshu "branches6": ImageFile.PyEncoder.branches, # deekshu
# Add more # Add more
} }

View File

@ -32,6 +32,10 @@ from ._typing import SupportsRead
branches = { branches = {
"1": False, "1": False,
"2": False, "2": False,
"3": False,
"4": False,
"5": False,
"6": False,
} }
try: try:
@ -242,17 +246,6 @@ _FLAGS = {
class ImageCmsProfile: class ImageCmsProfile:
branches = {
"1": False,
"2": False,
"3": False,
"4": False,
"5": False,
"6": False,
"7": False,
"8": False,
}
def __init__(self, profile: str | SupportsRead[bytes] | core.CmsProfile) -> None: def __init__(self, profile: str | SupportsRead[bytes] | core.CmsProfile) -> None:
""" """
:param profile: Either a string representing a filename, :param profile: Either a string representing a filename,
@ -262,28 +255,21 @@ class ImageCmsProfile:
""" """
if isinstance(profile, str): if isinstance(profile, str):
ImageCmsProfile.branches["1"] = True
if sys.platform == "win32": if sys.platform == "win32":
ImageCmsProfile.branches["2"] = True
profile_bytes_path = profile.encode() profile_bytes_path = profile.encode()
try: try:
ImageCmsProfile.branches["3"] = True
profile_bytes_path.decode("ascii") profile_bytes_path.decode("ascii")
except UnicodeDecodeError: except UnicodeDecodeError:
ImageCmsProfile.branches["4"] = True
with open(profile, "rb") as f: with open(profile, "rb") as f:
ImageCmsProfile.branches["5"] = True
self._set(core.profile_frombytes(f.read())) self._set(core.profile_frombytes(f.read()))
return return
self._set(core.profile_open(profile), profile) self._set(core.profile_open(profile), profile)
elif hasattr(profile, "read"): elif hasattr(profile, "read"):
ImageCmsProfile.branches["6"] = True
self._set(core.profile_frombytes(profile.read())) self._set(core.profile_frombytes(profile.read()))
elif isinstance(profile, core.CmsProfile): elif isinstance(profile, core.CmsProfile):
ImageCmsProfile.branches["7"] = True
self._set(profile) self._set(profile)
else: else:
ImageCmsProfile.branches["8"] = True
msg = "Invalid type for Profile" # type: ignore[unreachable] msg = "Invalid type for Profile" # type: ignore[unreachable]
raise TypeError(msg) raise TypeError(msg)
@ -397,10 +383,6 @@ _CmsProfileCompatible = Union[
class PyCMSError(Exception): class PyCMSError(Exception):
branches = {
"1": False,
"2": False,
}
"""(pyCMS) Exception class. """(pyCMS) Exception class.
This is used for all errors in the pyCMS API.""" This is used for all errors in the pyCMS API."""
@ -522,10 +504,8 @@ def getOpenProfile(
""" """
try: try:
PyCMSError.branches["1"] = True
return ImageCmsProfile(profileFilename) return ImageCmsProfile(profileFilename)
except (OSError, TypeError, ValueError) as v: except (OSError, TypeError, ValueError) as v:
PyCMSError.branches["2"] = True
raise PyCMSError(v) from v raise PyCMSError(v) from v
@ -593,22 +573,28 @@ def buildTransform(
""" """
if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3): if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3):
branches["1"] = True
msg = "renderingIntent must be an integer between 0 and 3" msg = "renderingIntent must be an integer between 0 and 3"
raise PyCMSError(msg) raise PyCMSError(msg)
if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG): if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
branches["2"] = True
msg = f"flags must be an integer between 0 and {_MAX_FLAG}" msg = f"flags must be an integer between 0 and {_MAX_FLAG}"
raise PyCMSError(msg) raise PyCMSError(msg)
try: try:
branches["3"] = True
if not isinstance(inputProfile, ImageCmsProfile): if not isinstance(inputProfile, ImageCmsProfile):
branches["4"] = True
inputProfile = ImageCmsProfile(inputProfile) inputProfile = ImageCmsProfile(inputProfile)
if not isinstance(outputProfile, ImageCmsProfile): if not isinstance(outputProfile, ImageCmsProfile):
branches["5"] = True
outputProfile = ImageCmsProfile(outputProfile) outputProfile = ImageCmsProfile(outputProfile)
return ImageCmsTransform( return ImageCmsTransform(
inputProfile, outputProfile, inMode, outMode, renderingIntent, flags=flags inputProfile, outputProfile, inMode, outMode, renderingIntent, flags=flags
) )
except (OSError, TypeError, ValueError) as v: except (OSError, TypeError, ValueError) as v:
branches["6"] = True
raise PyCMSError(v) from v raise PyCMSError(v) from v

View File

@ -188,7 +188,6 @@ class PhotoImage:
class BitmapImage: class BitmapImage:
""" """
A Tkinter-compatible bitmap image. This can be used everywhere Tkinter A Tkinter-compatible bitmap image. This can be used everywhere Tkinter
expects an image object. expects an image object.