Skip to content

Commit

Permalink
test: Add tests for submodule core.containers and Improve other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaokang2022 committed Jan 7, 2025
1 parent 5cc9dab commit be02490
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 22 deletions.
Binary file added tests/assets/images/logo.ico
Binary file not shown.
26 changes: 13 additions & 13 deletions tests/test_animation/test_animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def test_fps(self) -> None:
def test_active(self) -> None:
an = animations.Animation(60, lambda _: None)
an.start()
self.assertEqual(an.active, True)
self.assertTrue(an.active)
an.stop()
self.assertEqual(an.active, False)
self.assertFalse(an.active)

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test_skip(self) -> None:
Expand All @@ -55,7 +55,7 @@ def test_skip(self) -> None:
self.assertEqual(an._count, 0)

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test_count(self) -> None: # TODO
def test_count(self) -> None:
an = animations.Animation(60, lambda _: None)
an2 = animations.Animation(60, lambda _: None, repeat=2)

Expand All @@ -78,7 +78,7 @@ def test_total_frames(self) -> None:
an.stop()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test_repeat(self) -> None: # TODO
def test_repeat(self) -> None:
an = animations.Animation(60, lambda _: None, repeat=0)
an2 = animations.Animation(60, lambda _: None, repeat=1)
an3 = animations.Animation(60, lambda _: None, repeat=2)
Expand Down Expand Up @@ -136,7 +136,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
animations.MoveWindow(self.tk, (99, 99), 1, fps=1).command(1)
animations.MoveWindow(self.top, (99, 99), 1, fps=1).command(1)

Expand All @@ -157,7 +157,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
animations.MoveWindow(self.tk, (-99, -99), 1, fps=1).command(1)
animations.MoveWindow(self.top, (-99, -99), 1, fps=1).command(1)

Expand All @@ -183,7 +183,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
self.assertWarns(UserWarning, lambda: animations.MoveTkWidget(self.widget, (99, 99), 99))
self.assertWarns(UserWarning, lambda: animations.MoveTkWidget(self.widget2, (99, 99), 99))
self.an = animations.MoveTkWidget(self.widget3, (99, 99), 99)
Expand All @@ -202,7 +202,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
self.an = animations.MoveWidget(self.widget, (99, 99), 99)
self.an.start()

Expand All @@ -219,7 +219,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
self.an = animations.MoveElement(self.widget.elements[0], (99, 99), 99)
self.an.start()

Expand All @@ -236,7 +236,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
self.an = animations.MoveItem(self.cv, self.item, (99, 99), 99)
self.an.start()

Expand All @@ -252,7 +252,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
self.an = animations.GradientTkWidget(self.widget, "fill", ("red", "#00FF00"), 99)
self.an.start()
self.assertRaises(ValueError, lambda: animations.GradientTkWidget(self.widget, "fill", ("", ""), 1000))
Expand All @@ -270,7 +270,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
self.an = animations.GradientItem(self.cv, self.item, "fill", ("red", "#0000FF"), 99)
self.an.start()
self.assertRaises(ValueError, lambda: animations.GradientItem(self.cv, self.item, "fill", ("", ""), 1000))
Expand All @@ -291,7 +291,7 @@ def tearDown(self) -> None:
self.tk.destroy()

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def test(self) -> None:
def test_init(self) -> None:
self.an1 = animations.ScaleFontSize(self.widget.texts[0], 10, 99)
self.an1.start()
self.an2 = animations.ScaleFontSize(self.widget.texts[0], 24.5, 99)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_animation/test_contollers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tkintertools.animation import controllers


class Test(unittest.TestCase):
class TestCase(unittest.TestCase):

def test_linear(self) -> None:
self.assertEqual(controllers.linear(0), 0)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_color/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tkintertools.color import convert


class Test(unittest.TestCase):
class TestCase(unittest.TestCase):

def test_rgb_to_hex(self) -> None:
self.assertEqual(convert.rgb_to_hex((255, 255, 255)), "#FFFFFF")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_color/test_hsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tkintertools.color import hsl


class Test(unittest.TestCase):
class TestCase(unittest.TestCase):

def test_contrast(self) -> None:
self.assertEqual(hsl.contrast((1, 1, 1)), (math.tau - 1, 0, 0))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_color/test_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tkintertools.color import rgb


class Test(unittest.TestCase):
class TestCase(unittest.TestCase):

def test_contrast(self) -> None:
self.assertEqual(rgb.contrast((0, 0, 0)), (255, 255, 255))
Expand Down
6 changes: 3 additions & 3 deletions tests/test_core/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def test_reset(self) -> None:
configs.Env.reset()

self.assertEqual(configs.Env.system, configs.Env.get_default_system())
self.assertEqual(configs.Env.is_dark, False)
self.assertEqual(configs.Env.default_animation, True)
self.assertEqual(configs.Env.auto_update, True)
self.assertFalse(configs.Env.is_dark)
self.assertTrue(configs.Env.default_animation)
self.assertTrue(configs.Env.auto_update)

def test_get_default_system(self) -> None:
with unittest.mock.patch('platform.system', return_value='Windows'):
Expand Down
167 changes: 167 additions & 0 deletions tests/test_core/test_containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# pylint: disable=all

import contextlib
import io
import unittest
import unittest.mock

from tkintertools.core import containers
from tkintertools.standard import widgets


class TestTk(unittest.TestCase):

def test_init(self) -> None:
containers.Tk(icon="").destroy()
containers.Tk(icon="tests/assets/images/logo.ico").destroy()
containers.Tk(title=":)").destroy()

with unittest.mock.patch("platform.system", return_value="Darwin"):
containers.Tk(icon="tests/assets/images/logo.ico").destroy()

def test_ratios(self) -> None:
with containers.Tk() as tk:
self.assertEqual(tk.ratios, (1., 1.))
tk.wm_geometry(f"{tk.size[0]//2}x{tk.size[1]//2}")
tk.update_idletasks()
tk._zoom()
self.assertEqual(tk.ratios, (0.5, 0.5))

def test_alpha(self) -> None:
with containers.Tk() as tk:
self.assertEqual(tk.alpha(), 1.)
self.assertIsNone(tk.alpha(0.8))
self.assertEqual(tk.alpha(), 0.8)

def test_topmost(self) -> None:
with containers.Tk() as tk:
self.assertIsNone(tk.topmost())
self.assertTrue(tk.topmost(None))
self.assertIsNone(tk.topmost(False))
self.assertFalse(tk.topmost(None))

def test_fullscreen(self) -> None:
with containers.Tk() as tk:
self.assertIsNone(tk.fullscreen())
self.assertTrue(tk.fullscreen(None))
self.assertIsNone(tk.fullscreen(False))
self.assertFalse(tk.fullscreen(None))

def test_toolwindow(self) -> None:
with containers.Tk() as tk:
self.assertIsNone(tk.toolwindow())
self.assertTrue(tk.toolwindow(None))
self.assertIsNone(tk.toolwindow(False))
self.assertFalse(tk.toolwindow(None))

def test_transparentcolor(self) -> None:
with containers.Tk() as tk:
self.assertEqual(tk.transparentcolor(), None)
self.assertIsNone(tk.transparentcolor("red"))
self.assertEqual(tk.transparentcolor(), "red")
self.assertIsNone(tk.transparentcolor(""))
self.assertEqual(tk.transparentcolor(), None)

def test_center(self) -> None:
with containers.Tk() as tk:
tk.center()
with containers.Toplevel() as tl:
tl.center(refer=tk)

def test_theme(self) -> None:
with containers.Tk() as tk:
with containers.Toplevel(tk):
with containers.Canvas(tk):
tk.theme(True, include_children=True, include_canvases=True)

def test_at_exit(self) -> None:
a = None

def command() -> None:
nonlocal a
a = True

with containers.Tk() as tk:
tk.at_exit(command, ensure_destroy=False)
tk.call(tk.wm_protocol("WM_DELETE_WINDOW"))

self.assertTrue(a)

tk = containers.Tk() # NOTE: This does not need to be destroyed again
tk.at_exit(lambda: None)
tk.call(tk.wm_protocol("WM_DELETE_WINDOW"))

def callback_raise() -> None:
raise RuntimeError

with io.StringIO() as captured_output:
with contextlib.redirect_stderr(captured_output):
with containers.Tk() as tk:
tk.at_exit(callback_raise, ensure_destroy=False)
tk.call(tk.wm_protocol("WM_DELETE_WINDOW"))

self.assertTrue(bool(captured_output.getvalue()))

def test_zoom(self) -> None:
with containers.Tk() as tk:
with containers.Canvas(tk):
tk.ratios # trigger caching
tk.geometry(size=(23, 33))
tk.update_idletasks()
tk._zoom()

@unittest.mock.patch("tkintertools.core.containers.Tk.theme")
def test_wrap_method(self, mock_theme: unittest.mock.Mock) -> None:
with containers.Tk() as tk:
tk._wrap_method("wm_resizable")
tk.wm_resizable(False, False)
mock_theme.assert_called()


class TestToplevel(unittest.TestCase):

def setUp(self) -> None:
self.tk = containers.Tk()

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

def test_init(self) -> None:
self.tl = containers.Toplevel(grab=True, focus=True)


class TestCanvas(unittest.TestCase):

def test_init(self) -> None:
with containers.Tk() as tk:
containers.Canvas(tk, auto_update=True)

def test_ratios(self) -> None:
with containers.Tk() as tk:
with containers.Canvas(tk) as cv:
cv.place(width=100, height=100)
cv.update_idletasks()
self.assertEqual(cv.ratios, (1., 1.))
cv.place(width=50, height=50)
cv.update_idletasks()
self.assertEqual(cv.ratios, (0.5, 0.5))

def test_theme(self) -> None:
with containers.Tk() as tk:
with containers.Canvas(tk) as cv:
widgets.Button(cv, (0, 0), auto_update=True)
widgets.Button(cv, (0, 0), auto_update=False)
widgets.Button(cv, (0, 0), auto_update=True).disable()
with containers.Canvas(cv):
cv.theme(True)

def test_initialization(self) -> None:
with containers.Tk() as tk:
for anchor in "nw", "n", "w", "ne", "sw", "e", "s", "se", "center":
with containers.Canvas(tk) as cv:
cv.place(width=100, height=100, anchor=anchor)
cv._initialization()


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion tests/test_theme/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


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

def setUp(self) -> None:
self.tk = containers.Tk()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_toolbox/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_lock(self) -> None:
self.assertTrue(self.t.get())


class Test(unittest.TestCase):
class TestCase(unittest.TestCase):

@unittest.skipIf(platform.system() == "Linux", "No display name.")
def setUp(self) -> None:
Expand Down

0 comments on commit be02490

Please sign in to comment.