|
| 1 | +""" |
| 2 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +Licensed under the MIT License. |
| 4 | +""" |
| 5 | + |
| 6 | + |
| 7 | +import logging |
| 8 | +from typing import Union |
| 9 | + |
| 10 | +from .formatter import ConsoleFormatter |
| 11 | +from .ansi import ANSI |
| 12 | + |
| 13 | + |
| 14 | +def create_record(name: str, level: int, msg: Union[str, dict, list]) -> logging.LogRecord: |
| 15 | + record = logging.LogRecord( |
| 16 | + name=name, |
| 17 | + level=level, |
| 18 | + pathname="test.py", |
| 19 | + lineno=1, |
| 20 | + msg=msg, |
| 21 | + args=(), |
| 22 | + exc_info=None |
| 23 | + ) |
| 24 | + record.levelname = logging.getLevelName(level) |
| 25 | + return record |
| 26 | + |
| 27 | +def test_error_formatting(): |
| 28 | + formatter = ConsoleFormatter() |
| 29 | + record = create_record("test", logging.ERROR, "Error message") |
| 30 | + |
| 31 | + result = formatter.format(record) |
| 32 | + expected = f"{ANSI.FOREGROUND_RED}{ANSI.BOLD}[ERROR] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Error message" |
| 33 | + assert result == expected |
| 34 | + |
| 35 | +def test_warning_formatting(): |
| 36 | + formatter = ConsoleFormatter() |
| 37 | + record = create_record("test", logging.WARNING, "Warning message") |
| 38 | + |
| 39 | + result = formatter.format(record) |
| 40 | + expected = f"{ANSI.FOREGROUND_YELLOW}{ANSI.BOLD}[WARNING] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Warning message" |
| 41 | + assert result == expected |
| 42 | + |
| 43 | +def test_info_formatting(): |
| 44 | + formatter = ConsoleFormatter() |
| 45 | + record = create_record("test", logging.INFO, "Info message") |
| 46 | + |
| 47 | + result = formatter.format(record) |
| 48 | + expected = f"{ANSI.FOREGROUND_CYAN}{ANSI.BOLD}[INFO] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Info message" |
| 49 | + assert result == expected |
| 50 | + |
| 51 | +def test_debug_formatting(): |
| 52 | + formatter = ConsoleFormatter() |
| 53 | + record = create_record("test", logging.DEBUG, "Debug message") |
| 54 | + |
| 55 | + result = formatter.format(record) |
| 56 | + expected = f"{ANSI.FOREGROUND_MAGENTA}{ANSI.BOLD}[DEBUG] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Debug message" |
| 57 | + assert result == expected |
| 58 | + |
| 59 | +def test_dict_message_formatting(): |
| 60 | + formatter = ConsoleFormatter() |
| 61 | + dict_msg = {"key": "value", "nested": {"inner": "data"}} |
| 62 | + record = create_record("test", logging.INFO, dict_msg) |
| 63 | + |
| 64 | + result = formatter.format(record) |
| 65 | + result_lines = result.split("\n") |
| 66 | + |
| 67 | + prefix = f"{ANSI.FOREGROUND_CYAN}{ANSI.BOLD}[INFO] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET}" |
| 68 | + assert result_lines[0].startswith(prefix) |
| 69 | + assert '"key": "value"' in result |
| 70 | + assert '"nested": {' in result |
| 71 | + assert '"inner": "data"' in result |
| 72 | + |
| 73 | +def test_list_message_formatting(): |
| 74 | + formatter = ConsoleFormatter() |
| 75 | + list_msg = ["item1", "item2", {"key": "value"}] |
| 76 | + record = create_record("test", logging.INFO, list_msg) |
| 77 | + |
| 78 | + result = formatter.format(record) |
| 79 | + result_lines = result.split("\n") |
| 80 | + |
| 81 | + prefix = f"{ANSI.FOREGROUND_CYAN}{ANSI.BOLD}[INFO] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET}" |
| 82 | + assert result_lines[0].startswith(prefix) |
| 83 | + assert '"item1"' in result |
| 84 | + assert '"item2"' in result |
| 85 | + assert '"key": "value"' in result |
| 86 | + |
| 87 | +def test_multiline_message_formatting(): |
| 88 | + formatter = ConsoleFormatter() |
| 89 | + multiline_msg = "Line 1\nLine 2\nLine 3" |
| 90 | + record = create_record("test", logging.INFO, multiline_msg) |
| 91 | + |
| 92 | + result = formatter.format(record) |
| 93 | + expected_lines = [ |
| 94 | + f"{ANSI.FOREGROUND_CYAN}{ANSI.BOLD}[INFO] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Line 1", |
| 95 | + f"{ANSI.FOREGROUND_CYAN}{ANSI.BOLD}[INFO] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Line 2", |
| 96 | + f"{ANSI.FOREGROUND_CYAN}{ANSI.BOLD}[INFO] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Line 3" |
| 97 | + ] |
| 98 | + expected = "\n".join(expected_lines) |
| 99 | + assert result == expected |
| 100 | + |
| 101 | +def test_unknown_level_formatting(): |
| 102 | + formatter = ConsoleFormatter() |
| 103 | + record = create_record("test", 123, "Custom level message") |
| 104 | + |
| 105 | + result = formatter.format(record) |
| 106 | + expected = f"{ANSI.BOLD}[Level 123] test{ANSI.FOREGROUND_RESET}{ANSI.BOLD_RESET} Custom level message" |
| 107 | + assert result.endswith("Custom level message") |
| 108 | + assert "[LEVEL 123]" in result |
| 109 | + assert "test" in result |
0 commit comments