File: test_colorizer.py

package info (click to toggle)
python-os-testr 3.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: python: 1,153; sh: 29; makefile: 22
file content (76 lines) | stat: -rw-r--r-- 2,534 bytes parent folder | download | duplicates (5)
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
# Copyright 2016 Hewlett Packard Enterprise Development LP
# All Rights Reserved.
#
#    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. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import six
import sys

from ddt import data
from ddt import ddt
from ddt import unpack

from os_testr.tests import base
from os_testr.utils import colorizer


@ddt
class TestNullColorizer(base.TestCase):

    @data(None, "foo", sys.stdout, )
    def test_supported_always_true(self, stream):
        self.assertTrue(colorizer.NullColorizer.supported(stream))

    @data(("foo", "red"), ("foo", "bar"))
    @unpack
    def test_write_string_ignore_color(self, text, color):
        output = six.StringIO()
        c = colorizer.NullColorizer(output)
        c.write(text, color)
        self.assertEqual(text, output.getvalue())

    @data((None, "red"), (None, None))
    @unpack
    def test_write_none_exception(self, text, color):
        c = colorizer.NullColorizer(sys.stdout)
        self.assertRaises(TypeError, c.write, text, color)


@ddt
class TestAnsiColorizer(base.TestCase):

    def test_supported_false(self):
        # NOTE(masayukig): This returns False because our unittest env isn't
        #  interactive
        self.assertFalse(colorizer.AnsiColorizer.supported(sys.stdout))

    @data(None, "foo")
    def test_supported_error(self, stream):
        self.assertRaises(AttributeError,
                          colorizer.AnsiColorizer.supported, stream)

    @data(("foo", "red", "31"), ("foo", "blue", "34"))
    @unpack
    def test_write_string_valid_color(self, text, color, color_code):
        output = six.StringIO()
        c = colorizer.AnsiColorizer(output)
        c.write(text, color)
        self.assertIn(text, output.getvalue())
        self.assertIn(color_code, output.getvalue())

    @data(("foo", None), ("foo", "invalid_color"))
    @unpack
    def test_write_string_invalid_color(self, text, color):
        output = six.StringIO()
        c = colorizer.AnsiColorizer(output)
        self.assertRaises(KeyError, c.write, text, color)