aboutsummaryrefslogtreecommitdiffstats
path: root/pylog/tests/test_color_formatter.py
blob: 56bf9c2db7867e76f3f14e7e7d34558669d4f461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (c) 2020 Deutsche Telekom.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

import unittest
import sys
from logging import LogRecord

if sys.version_info[0] < 3:
    from mock import patch
if sys.version_info[0] >= 3:
    from unittest.mock import patch
import pytest

from onaplogging.colorFormatter import (
    ATTRIBUTES,
    BaseColorFormatter,
    colored,
    COLORS,
    HIGHLIGHTS,
    FMT_STR,
    RESET,
)
from onaplogging.utils.system import is_above_python_3_2


class TestColorFormatter(unittest.TestCase):

    TEST_TEXT = "test"

    def test_colored_os_name_nt(self):

        with patch("onaplogging.colorFormatter.os.name", "nt"):

            text = colored(self.TEST_TEXT)
            assert text == self.TEST_TEXT

            text = colored(self.TEST_TEXT, color="black")
            assert text == self.TEST_TEXT

            text = colored(self.TEST_TEXT, on_color="black")
            assert text == self.TEST_TEXT

            text = colored(self.TEST_TEXT, attrs="bold")
            assert text == self.TEST_TEXT

    def test_colored_os_name_ce(self):

        with patch("onaplogging.colorFormatter.os.name", "ce"):

            text = colored(self.TEST_TEXT)
            assert text == self.TEST_TEXT

            text = colored(self.TEST_TEXT, color="black")
            assert text == self.TEST_TEXT

            text = colored(self.TEST_TEXT, on_color="black")
            assert text == self.TEST_TEXT

            text = colored(self.TEST_TEXT, attrs="bold")
            assert text == self.TEST_TEXT

    def test_colored_os_name_posix(self):

        with patch("onaplogging.colorFormatter.os.name", "posix"):
            text = colored(self.TEST_TEXT)
            assert text == self.TEST_TEXT + RESET

            text = colored(self.TEST_TEXT, color="black")
            assert text == FMT_STR % (COLORS["black"], self.TEST_TEXT) + RESET

            text = colored(self.TEST_TEXT, color="invalid")
            assert text == FMT_STR % (0, self.TEST_TEXT) + RESET

            text = colored(self.TEST_TEXT, on_color="red")
            assert text == FMT_STR % (HIGHLIGHTS["red"], self.TEST_TEXT) + RESET

            text = colored(self.TEST_TEXT, on_color="invalid")
            assert text == FMT_STR % (0, self.TEST_TEXT) + RESET

            text = colored(self.TEST_TEXT, attrs="bold")
            assert text == FMT_STR % (ATTRIBUTES["bold"], self.TEST_TEXT) + RESET

            text = colored(self.TEST_TEXT, attrs=["bold", "blink"])
            assert (
                text
                == FMT_STR % (ATTRIBUTES["blink"], FMT_STR % (ATTRIBUTES["bold"], self.TEST_TEXT))
                + RESET
            )

            text = colored(self.TEST_TEXT, attrs="invalid")
            assert text == FMT_STR % (0, self.TEST_TEXT) + RESET

    def test_base_color_formatter(self):

        if is_above_python_3_2():
            with pytest.raises(ValueError):
                BaseColorFormatter(style="!")

        TEST_MESSAGE = "TestMessage"
        record = LogRecord(
            name="TestName",
            level=0,
            pathname="TestPathName",
            lineno=1,
            msg=TEST_MESSAGE,
            args=None,
            exc_info=None,
        )

        base_formatter = BaseColorFormatter()
        assert base_formatter.format(record) == TEST_MESSAGE + RESET

        base_formatter = BaseColorFormatter(fmt="TEST %(message)s")
        assert base_formatter.format(record) == "TEST " + TEST_MESSAGE + RESET

        colorfmt = {record.levelname: {"color": "black", "highlight": "red", "attribute": "bold"}}
        base_formatter = BaseColorFormatter(colorfmt=colorfmt)
        assert (
            base_formatter.format(record)
            == FMT_STR
            % (
                ATTRIBUTES["bold"],
                FMT_STR % (HIGHLIGHTS["red"], FMT_STR % (COLORS["black"], "TestMessage")),
            )
            + RESET
        )