From 8f71069bc85acb1703328971dd946230ebde2fd6 Mon Sep 17 00:00:00 2001 From: Masayuki Igawa Date: Fri, 4 Dec 2015 18:34:41 +0900 Subject: [PATCH] Add unit test for colorizer This commit adds unit-test cases for colorizer module. Change-Id: I329a808bbed3240e6fa41ac21d3e5558a0704d6a --- os_testr/tests/utils/__init__.py | 0 os_testr/tests/utils/test_colorizer.py | 76 ++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 os_testr/tests/utils/__init__.py create mode 100644 os_testr/tests/utils/test_colorizer.py diff --git a/os_testr/tests/utils/__init__.py b/os_testr/tests/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/os_testr/tests/utils/test_colorizer.py b/os_testr/tests/utils/test_colorizer.py new file mode 100644 index 0000000..fdf96b2 --- /dev/null +++ b/os_testr/tests/utils/test_colorizer.py @@ -0,0 +1,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)