Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions test/exception_handling/soft_error/start_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from os import linesep

import pytest
from _helper import operating_system
from _helper.timer import ensure_all_timer_threads_are_stopped
from colorist import Color

from timer import Timer


def test_timer_start_invalid_thread_type_soft_error_1(capfd: object) -> None:
custom_thread = "custom"

timer = ensure_all_timer_threads_are_stopped()
timer._threads = [custom_thread] # Triggers issue by invalid type as it should be a list[ThreadItem] type.
timer.start(thread=custom_thread)
terminal_output, _ = capfd.readouterr()
assert terminal_output == f"{Color.YELLOW}Timer: Something went wrong in the Timer's lookup module for thread {custom_thread.upper()}.{Color.OFF}\n"


def test_timer_start_invalid_thread_type_soft_error_2(capfd: object) -> None:
if operating_system.is_windows():
pytest.skip("Skipping test for Windows due to line separator issue.") # pragma: no cover
# TODO: Fix line separator issue on Windows.
return # pragma: no cover

custom_thread = "custom"

timer = ensure_all_timer_threads_are_stopped()
timer._threads = [custom_thread] # Triggers issue by invalid type as it should be a list[ThreadItem] type.
with Timer(thread=custom_thread):
pass
terminal_output, _ = capfd.readouterr()
assert terminal_output == \
f"{Color.YELLOW}Timer: Something went wrong in the Timer's lookup module for thread {custom_thread.upper()}.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer: Something went wrong in the Timer's lookup module for thread {custom_thread.upper()}.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer for thread {custom_thread.upper()} is not running. Use .start(thread='{custom_thread.upper()}') to start it.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer: Something went wrong in the Timer's stop thread controller for thread {custom_thread.upper()}.{Color.OFF}{linesep}"
66 changes: 55 additions & 11 deletions test/exception_handling/soft_error/stop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
from _helper.timer import ensure_all_timer_threads_are_stopped
from colorist import Color

from timer import Timer


def test_timer_stop_unknown_thread_soft_error_1(capfd: object) -> None:
if operating_system.is_windows():
pytest.skip("Skipping test for Windows due to line separator issue.") # pragma: no cover
# TODO: Fix line separator issue on Windows.
return # pragma: no cover

thread = "custom"
custom_thread = "custom"

timer = ensure_all_timer_threads_are_stopped()
timer.start()
time.sleep(0.1)
timer.stop(thread=thread)
timer.stop(thread=custom_thread)
terminal_output, _ = capfd.readouterr()
assert terminal_output == rf"{Color.YELLOW}Timer for thread {thread.upper()} is not running. Use .start(thread='{thread.upper()}') to start it.{Color.OFF}{linesep}Or maybe you aren't stopping the right thread? Currently open threads: NONE{linesep}"
assert terminal_output == \
f"{Color.YELLOW}Timer for thread {custom_thread.upper()} is not running. Use .start(thread='{custom_thread.upper()}') to start it.{Color.OFF}{linesep}" +\
f"Or maybe you aren't stopping the right thread? Currently open threads: NONE{linesep}"


def test_timer_stop_unknown_thread_soft_error_2(capfd: object) -> None:
Expand All @@ -29,15 +33,17 @@ def test_timer_stop_unknown_thread_soft_error_2(capfd: object) -> None:
# TODO: Fix line separator issue on Windows.
return # pragma: no cover

thread_1 = "custom_1"
thread_2 = "custom_2"
custom_thread_1 = "custom_1"
custom_thread_2 = "custom_2"

timer = ensure_all_timer_threads_are_stopped()
timer.start(thread=thread_1)
timer.start(thread=custom_thread_1)
time.sleep(0.1)
timer.stop(thread=thread_2)
timer.stop(thread=custom_thread_2)
terminal_output, _ = capfd.readouterr()
assert terminal_output == rf"{Color.YELLOW}Timer for thread {thread_2.upper()} is not running. Use .start(thread='{thread_2.upper()}') to start it.{Color.OFF}{linesep}Or maybe you aren't stopping the right thread? Currently open threads: {thread_1.upper()}{linesep}"
assert terminal_output == \
f"{Color.YELLOW}Timer for thread {custom_thread_2.upper()} is not running. Use .start(thread='{custom_thread_2.upper()}') to start it.{Color.OFF}{linesep}" +\
f"Or maybe you aren't stopping the right thread? Currently open threads: {custom_thread_1.upper()}{linesep}"


def test_timer_stop_not_started_thread_soft_error_1(capfd: object) -> None:
Expand All @@ -48,9 +54,47 @@ def test_timer_stop_not_started_thread_soft_error_1(capfd: object) -> None:


def test_timer_stop_not_started_thread_soft_error_2(capfd: object) -> None:
thread = "custom"
custom_thread = "custom"

timer = ensure_all_timer_threads_are_stopped()
timer.stop(thread=custom_thread)
terminal_output, _ = capfd.readouterr()
assert terminal_output == f"{Color.YELLOW}Timer for thread {custom_thread.upper()} is not running. Use .start(thread='{custom_thread.upper()}') to start it.{Color.OFF}\n"


def test_timer_stop_invalid_thread_type_soft_error_1(capfd: object) -> None:
if operating_system.is_windows():
pytest.skip("Skipping test for Windows due to line separator issue.") # pragma: no cover
# TODO: Fix line separator issue on Windows.
return # pragma: no cover

custom_thread = "custom"

timer = ensure_all_timer_threads_are_stopped()
timer._threads = [custom_thread] # Triggers issue by invalid type as it should be a list[ThreadItem] type.
timer.stop(thread=custom_thread)
terminal_output, _ = capfd.readouterr()
assert terminal_output == \
f"{Color.YELLOW}Timer: Something went wrong in the Timer's lookup module for thread {custom_thread.upper()}.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer for thread {custom_thread.upper()} is not running. Use .start(thread='{custom_thread.upper()}') to start it.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer: Something went wrong in the Timer's stop thread controller for thread {custom_thread.upper()}.{Color.OFF}{linesep}"


def test_timer_stop_invalid_thread_type_soft_error_2(capfd: object) -> None:
if operating_system.is_windows():
pytest.skip("Skipping test for Windows due to line separator issue.") # pragma: no cover
# TODO: Fix line separator issue on Windows.
return # pragma: no cover

custom_thread = "custom"

timer = ensure_all_timer_threads_are_stopped()
timer.stop(thread=thread)
timer._threads = [custom_thread] # Triggers issue by invalid type as it should be a list[ThreadItem] type.
with Timer(thread=custom_thread):
pass
terminal_output, _ = capfd.readouterr()
assert terminal_output == f"{Color.YELLOW}Timer for thread {thread.upper()} is not running. Use .start(thread='{thread.upper()}') to start it.{Color.OFF}\n"
assert terminal_output == \
f"{Color.YELLOW}Timer: Something went wrong in the Timer's lookup module for thread {custom_thread.upper()}.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer: Something went wrong in the Timer's lookup module for thread {custom_thread.upper()}.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer for thread {custom_thread.upper()} is not running. Use .start(thread='{custom_thread.upper()}') to start it.{Color.OFF}{linesep}" +\
f"{Color.YELLOW}Timer: Something went wrong in the Timer's stop thread controller for thread {custom_thread.upper()}.{Color.OFF}{linesep}"