clog/testing/test_printfmt.py
2023-07-06 20:36:50 +01:00

95 lines
2.7 KiB
Python

# flake8: noqa
import re
import pytest
from clog.utils.common import LogLevel
from clog.utils.printfmt import (
Colours,
gen_log_header,
log_as_col,
loglevel_as_str,
wrap,
)
class TestPrintFMT:
@pytest.mark.parametrize(
"seq", [
eval(f"Colours.{_}") for _ in dir(Colours) if _.isupper()
]
)
def test_colour_ansi_format(self, seq):
assert re.match('\\033\\[[0-9]+m', seq)
@pytest.mark.parametrize(
"value, expected", [
(Colours.NORMAL, '\033[0m'),
(Colours.RED, '\033[91m'),
(Colours.GREEN, '\033[92m'),
(Colours.YELLOW, '\033[93m'),
(Colours.BLUE, '\033[94m'),
]
)
def test_colour_ansi_match_codes(self, value, expected):
assert value == expected
def test_log_header_time_format_iso_8601(self):
assert re.match(
'^\\[\\d{4}(-\\d{2}){2}T\\d{2}(:\\d{2}){2}\\+\\d{4}\\].*$',
gen_log_header('')
)
def test_log_header_component_length(self):
assert len(gen_log_header('TEST').split(' ')) >= 4
def test_log_header_is_formattable(self):
assert len(re.findall(r'{(.*?)}', gen_log_header(LogLevel.DEBUG))) > 0
@pytest.mark.parametrize(
"value, expected", [
(LogLevel.NORMAL, Colours.NORMAL),
(LogLevel.PASS, Colours.GREEN),
(LogLevel.DEBUG, Colours.BLUE),
(LogLevel.WARN, Colours.YELLOW),
(LogLevel.ERROR, Colours.RED),
(-1, Colours.NORMAL),
(1000, Colours.NORMAL),
(2, '\033[94m'),
(LogLevel.WARN, '\033[93m'),
("a pesky string", Colours.NORMAL),
]
)
def test_log_as_colour(self, value, expected):
assert log_as_col(value) == expected
@pytest.mark.parametrize(
"value, expected", [
*zip([eval(f"LogLevel.{_}") for _ in dir(LogLevel) if _.isupper()],
["{0: <5}".format(_) for _ in dir(LogLevel) if _.isupper()])
]
)
def test_log_level_in_string_form(self, value, expected):
assert loglevel_as_str(value) == expected
@pytest.mark.parametrize(
"value, expected", [
("This won't be too long. So no wrap", False),
("This will be long enough" * 10, True),
(gen_log_header(LogLevel.DEBUG).format(
"Not long enough in file", CALLER=0), False),
(gen_log_header(LogLevel.DEBUG).format(
"Long enough to break many times" * 20, CALLER=0), True)
]
)
def test_wrap_has_newlines(self, value, expected):
assert (wrap(value).find('\n') != -1) == expected