Skip to content

Commit

Permalink
test: Improve tests for submodule toolbox.enhanced
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaokang2022 committed Jan 6, 2025
1 parent 9fc52a5 commit 5b70488
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 38 additions & 0 deletions tests/test_toolbox/test_enhanced.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# pylint: disable=all

import importlib
import pathlib
import platform
import tkinter
import unittest
import unittest.mock

from tkintertools.toolbox import enhanced

Expand Down Expand Up @@ -40,5 +42,41 @@ def test_resize(self) -> None:
self.assertEqual(new_height, 100)


@unittest.skipIf(platform.system() == "Linux", "No display name.")
class TestPhotoImageNoPillow(unittest.TestCase):

def setUp(self) -> None:
with unittest.mock.patch.dict("sys.modules", {'PIL': None}):
importlib.reload(enhanced)

self.tk = tkinter.Tk()
self.image = enhanced.PhotoImage(file=pathlib.Path(__file__).parent.parent/"assets/images/logo.png")
self.width = self.image.width()
self.height = self.image.height()

def tearDown(self) -> None:
self.tk.destroy()
importlib.reload(enhanced)

def test_scale(self) -> None:
large_image = self.image.scale(1.5, 1.5)
small_image = self.image.scale(0.5, 0.5)
large_width = large_image.width()
large_height = large_image.height()
small_width = small_image.width()
small_height = small_image.height()
self.assertEqual(large_width, self.width*1.5)
self.assertEqual(large_height, self.height*1.5)
self.assertEqual(small_width, self.width*0.5)
self.assertEqual(small_height, self.height*0.5)

def test_resize(self) -> None:
new_image = self.image.resize(100, 100)
new_width = new_image.width()
new_height = new_image.height()
self.assertEqual(new_width, 100)
self.assertEqual(new_height, 100)


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions tkintertools/toolbox/enhanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
try:
from PIL import ImageTk
except ImportError:
pass
ImageTk = None


if globals().get("ImageTk") is None:
if ImageTk is None:

class PhotoImage(tkinter.PhotoImage):
"""Enhanced version of `tkinter.PhotoImage`."""
Expand Down

0 comments on commit 5b70488

Please sign in to comment.