# -*- coding: utf-8 -*- """ Unit-Tests für trixy_core.utils. """ import io import sys from unittest.mock import patch import pytest from trixy_core.utils.debug import ( pinfo, pdebug, perror, pwarn, set_debug_mode, is_debug_mode, Colors, _format_message ) from trixy_core.utils.version import ( VERSION, VERSION_STRING, MAJOR, MINOR, REVISION, PROTOCOL_VERSION, PROTOCOL_VERSION_STRING, PROTOCOL_MAJOR, PROTOCOL_MINOR ) class TestDebugFunctions: """Tests für Debug-Ausgabefunktionen.""" def test_set_debug_mode_enables(self): """Testet das Aktivieren des Debug-Modus.""" set_debug_mode(True) assert is_debug_mode() is True def test_set_debug_mode_disables(self): """Testet das Deaktivieren des Debug-Modus.""" set_debug_mode(False) assert is_debug_mode() is False def test_pinfo_outputs_to_stdout_in_debug_mode(self): """Testet pinfo-Ausgabe im Debug-Modus.""" set_debug_mode(True) with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: pinfo("Test message") output = mock_stdout.getvalue() assert "[INFO]" in output assert "Test message" in output def test_pdebug_outputs_to_stdout_in_debug_mode(self): """Testet pdebug-Ausgabe im Debug-Modus.""" set_debug_mode(True) with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: pdebug("Debug message") output = mock_stdout.getvalue() assert "[DEBUG]" in output assert "Debug message" in output def test_perror_outputs_to_stderr_in_debug_mode(self): """Testet perror-Ausgabe im Debug-Modus.""" set_debug_mode(True) with patch("sys.stderr", new_callable=io.StringIO) as mock_stderr: perror("Error message") output = mock_stderr.getvalue() assert "[ERROR]" in output assert "Error message" in output def test_pwarn_outputs_to_stdout_in_debug_mode(self): """Testet pwarn-Ausgabe im Debug-Modus.""" set_debug_mode(True) with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: pwarn("Warning message") output = mock_stdout.getvalue() assert "[WARN]" in output assert "Warning message" in output def test_format_message_with_args(self): """Testet Nachrichtenformatierung mit Argumenten.""" formatted = _format_message("INFO", Colors.GREEN, "Value: %s", "test") assert "Value: test" in formatted assert "[INFO]" in formatted def test_format_message_without_args(self): """Testet Nachrichtenformatierung ohne Argumente.""" formatted = _format_message("DEBUG", Colors.CYAN, "Simple message") assert "Simple message" in formatted def test_pinfo_with_format_args(self): """Testet pinfo mit Format-Argumenten.""" set_debug_mode(True) with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: pinfo("Count: %d, Name: %s", 42, "test") output = mock_stdout.getvalue() assert "Count: 42" in output assert "Name: test" in output class TestColors: """Tests für ANSI-Farbcodes.""" def test_reset_code(self): """Testet den Reset-Code.""" assert Colors.RESET == "\033[0m" def test_bold_code(self): """Testet den Bold-Code.""" assert Colors.BOLD == "\033[1m" def test_color_codes_are_strings(self): """Testet, dass alle Farbcodes Strings sind.""" assert isinstance(Colors.RED, str) assert isinstance(Colors.GREEN, str) assert isinstance(Colors.YELLOW, str) assert isinstance(Colors.BLUE, str) assert isinstance(Colors.CYAN, str) def test_bright_colors_exist(self): """Testet, dass helle Farbvarianten existieren.""" assert hasattr(Colors, "BRIGHT_RED") assert hasattr(Colors, "BRIGHT_GREEN") assert hasattr(Colors, "BRIGHT_YELLOW") class TestVersion: """Tests für Versionskonstanten.""" def test_version_tuple_format(self): """Testet das Version-Tuple-Format.""" assert isinstance(VERSION, tuple) assert len(VERSION) == 3 assert VERSION == (MAJOR, MINOR, REVISION) def test_version_string_format(self): """Testet das Version-String-Format.""" assert isinstance(VERSION_STRING, str) assert VERSION_STRING == f"{MAJOR}.{MINOR}.{REVISION}" def test_version_components_are_integers(self): """Testet, dass Versionskomponenten Integer sind.""" assert isinstance(MAJOR, int) assert isinstance(MINOR, int) assert isinstance(REVISION, int) def test_protocol_version_tuple(self): """Testet das Protokollversions-Tuple.""" assert isinstance(PROTOCOL_VERSION, tuple) assert len(PROTOCOL_VERSION) == 2 assert PROTOCOL_VERSION == (PROTOCOL_MAJOR, PROTOCOL_MINOR) def test_protocol_version_string(self): """Testet den Protokollversions-String.""" assert isinstance(PROTOCOL_VERSION_STRING, str) assert PROTOCOL_VERSION_STRING == f"{PROTOCOL_MAJOR}.{PROTOCOL_MINOR}" def test_version_is_positive(self): """Testet, dass Versionsnummern nicht negativ sind.""" assert MAJOR >= 0 assert MINOR >= 0 assert REVISION >= 0 assert PROTOCOL_MAJOR >= 0 assert PROTOCOL_MINOR >= 0