def test_method_in_format_to_style_conversion(format_str, expected): """ Test converting the method in a format() call to an actual style """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) assert format_str.format(c=colorful) == expected
def test_change_color_palette(): """ Test changing the color palette for an existing colorful object """ NEW_COLOR_PALETTE = {'black': (0, 0, 0)} colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS, colorpalette={'defaultColor': (255, 255, 255)}) # updating existing color palette colorful.update_palette(NEW_COLOR_PALETTE) # old and new colors should exist assert str(colorful.black) == '\033[30m' assert str(colorful.defaultColor) == '\033[37m' # set color palette and overwrite the old one colorful.use_palette(NEW_COLOR_PALETTE) assert str(colorful.black) == '\033[30m' with pytest.raises(core.ColorfulError) as exc: colorful.defaultColor( 'The defaultColor was overwritten by the new palette') expected = ('the color "defaultColor" is unknown. ' 'Use a color in your color palette (by default: X11 rgb.txt)') assert str(exc.value) == expected
def test_length_of_styled_string(method_name, expected): """ Test the length of a styled string """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) method = getattr(colorful, method_name) assert len(method(expected)) == len(expected)
def test_method_str_to_style_conversion(method_name, expected): """ Test converting the method to an actual style """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) method = getattr(colorful, method_name) assert str(method) == expected
def test_method_call_to_style_conversion_disabled_colors(method_name): """ Test converting the method call to an actual style with disabled colors """ colorful = core.Colorful(colormode=terminal.NO_COLORS) method = getattr(colorful, method_name) assert str(method('No, I am your father')) == 'No, I am your father'
def test_invalid_color_mode(): """ Test setting an invalid color mode """ colorful = core.Colorful(colormode=42) with pytest.raises(core.ColorfulError) as exc: colorful.white('Invalid color mode') assert str(exc.value) == 'invalid color mode "42"'
def test_combining_styles(): """ Test combining styles """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) style = colorful.bold & colorful.red & colorful.on_black assert str(style) == '\033[1m\033[31m\033[40m'
def test_nested_styled_string_with_format(): """ Test nested styled string with format() """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) s = colorful.red('Hello {0} world'.format(colorful.blue('awesome'))) assert str( s) == '\033[31mHello \033[34mawesome\033[39m\033[31m world\033[39m'
def test_piping_str_into_style(): """ Test piping a string into a style """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) string = colorful.bold_red | 'No, I am your father' assert str(string) == '\033[1m\033[31mNo, I am your father\033[22m\033[39m'
def test_creating_unstyled_colorfulstring(): """ Test creating an unstyled ColorfulString """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) s = colorful.str('Hello ') s += colorful.black('World') assert s.orig_string == 'Hello World' assert s.styled_string == str(s) == 'Hello \033[30mWorld\033[39m'
def test_use_unknown_style(): """ Test exception for unknown style """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) with pytest.raises(core.ColorfulError) as exc: colorful.use_style('unknown') assert str(exc.value) == 'the style "unknown" is undefined'
def test_set_color_mode_methods(method_name, colorname, expected): """ Test changing the color mode for an existing colorful object """ colorful = core.Colorful(colormode=terminal.NO_COLORS) # change color mode getattr(colorful, method_name)() assert str(getattr(colorful, colorname)) == expected
def test_colorful_obj_setup_with_no_colors(): """ Test setup switching an existing colorful object to NO_COLORS (disabled) """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) colorful.setup(colormode=terminal.NO_COLORS) assert colorful.colormode == terminal.NO_COLORS assert str(colorful.red('color is illusion')) == 'color is illusion'
def test_colorfulstring_format_protocol_no_placeholder_when_disabled(): """Test that the ColorfulString format doesn't add a placeholder when colorful is disabled""" # given colorful = core.Colorful(colormode=terminal.NO_COLORS) # when s = "foo: {}".format(colorful.red("bar")) # then assert str(s) == "foo: bar"
def test_joining_colorfulstrings(): """ Test joining ColorfulStrings """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) parts = [colorful.red('Hello'), colorful.black('World')] # FIXME(TF): is there any other way?? string = ' '.join(str(part) for part in parts) assert string == '\033[31mHello\033[39m \033[30mWorld\033[39m'
def test_piping_constitency(): """ Test piping a string into a style does the same as the method call """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) string_piped = colorful.bold & colorful.red | 'No, I am your father' string_called = (colorful.bold & colorful.red)('No, I am your father') string_called_single = colorful.bold_red('No, I am your father') assert str(string_piped) == str(string_called) == str(string_called_single)
def test_styling_object_which_implements_str_proto(): """ Test styling an object which implements the str protocol """ class Dummy(object): def __str__(self): return 'I am a dummy object' colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) assert str(colorful.black( Dummy())) == '\033[30mI am a dummy object\033[39m'
def test_invalid_color_name(): """ Test invalid color name """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) expected = ('the color "invalidColor" is unknown. ' 'Use a color in your color palette (by default: X11 rgb.txt)') with pytest.raises(core.ColorfulError) as exc: colorful.bold_invalidColor('Invalid color name') assert str(exc.value) == expected
def test_colorful_print_wrong_argument(): """ Test calling the colorful.print method with wrong arguments """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) with pytest.raises(TypeError) as exc: colorful.print('Never printed because of argument error', foo=42) assert str( exc.value) == 'Colorful.print() got unexpected keyword arguments: foo'
def test_colorful_format(): """ Test the colorful.format method """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) expected = '\033[3m\033[31mNo, I am your father\033[23m\033[39m' assert colorful.format( '{c.italic_red}{0}, I am your {who}{c.no_italic}{c.close_fg_color}', 'No', who='father') == expected
def test_unicode_support(): """ Test unicode support """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) s = u'🐧🎉🐧' styled_s = colorful.black(s) # test basic unicode support assert UNICODE(styled_s) == u'\033[30m🐧🎉🐧\033[39m'
def test_colorful_obj_color_auto_detection(env, expected): """ Test that the colorful object is able to auto detect the supported colors """ os_env_backup = os.environ.copy() os.environ = env colorful = core.Colorful( colormode=None) # None to explicitly auto detect the colors assert colorful.colormode == expected # restore environ backup os.environ = os_env_backup
def test_colorful_obj_setup_with_extending_palette(): """ Test setup of an existing colorful object and extend the color palette """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) colorful.setup(colormode=terminal.TRUE_COLORS, colorpalette={'testColor': (0, 0, 0)}, extend_colors=True) assert colorful.colormode == terminal.TRUE_COLORS assert str(colorful.testColor) == '\033[38;2;0;0;0m' assert str(colorful.black) == '\033[38;2;0;0;0m'
def test_reading_color_palette(tmpdir): """ Test reading color palette from file """ # create palette file palette_file = tmpdir.mkdir('palettes').join('my_rgb.txt') palette_file.write(""" 0 0 0 myBlack 255 255 255 myWhite""") colorful = core.Colorful(colorpalette=str(palette_file)) assert str(colorful.myBlack) == '\033[30m' assert str(colorful.myWhite) == '\033[37m'
def test_use_styles(): """ Test using a predefined style """ colorful = core.Colorful(colormode=terminal.TRUE_COLORS) colorful.use_style('solarized') assert str(colorful.red) == '\033[38;2;220;50;47m' with pytest.raises(core.ColorfulError) as exc: colorful.lightCoral( 'The color lightCoral only exists in the X11 rgb.txt palette') assert str(exc.value).startswith('the color "lightCoral" is unknown.')
def test_colorful_direct_print(capsys): """ Test the colorful.print method """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) expected = u'\033[3m\033[31mNo, I am your father\033[23m\033[39m\n' colorful.print( '{c.italic_red}No, I am your father{c.no_italic}{c.close_fg_color}', flush=True) out, err = capsys.readouterr() assert repr(out) == repr(expected) assert err == ''
def test_colorfulstring_format_protocol(): """ Test format protocol for colorful string """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) s = colorful.black('father') string = 'No, I am your {0}'.format(s) assert string == 'No, I am your \033[30mfather\033[39m\033[26m' string = colorful.red('No, I am your {0}') formatted = string.format(s) assert str( formatted ) == '\033[31mNo, I am your \033[30mfather\033[39m\033[26m\033[39m'
def test_colorfulstyles_support_equals_protocol(style_a_name, style_b_name, expected_equal): """Test that the ColorfulStyle objects support the equals protocol""" # given colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) style_a = getattr(colorful, style_a_name) style_b = getattr(colorful, style_b_name) # when actual_equal = style_a == style_b actual_hash_equal = hash(style_a) == hash(style_b) # then assert actual_equal == expected_equal assert actual_hash_equal == expected_equal
def test_colorful_obj_setup(): """ Test setup of an existing colorful object """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) colorful.setup(colormode=terminal.TRUE_COLORS, colorpalette={'testColor': (0, 0, 0)}, extend_colors=False) assert colorful.colormode == terminal.TRUE_COLORS assert str(colorful.testColor) == '\033[38;2;0;0;0m' with pytest.raises(core.ColorfulError) as exc: colorful.black('black does not exist anymore') expected = ('the color "black" is unknown. ' 'Use a color in your color palette (by default: X11 rgb.txt)') assert str(exc.value) == expected
def test_str_behavior_for_colorfulstring(): """ Test that the ColorfulString object behaves like a str """ colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS) s = colorful.black('Hello') # test adding to str assert str(s + ' World') == '\033[30mHello\033[39m World' # test being added to str assert 'World ' + s == 'World \033[30mHello\033[39m' # test being augment added to str string = 'World ' string += s assert string == 'World \033[30mHello\033[39m' # test augment add a str s2 = colorful.red('Hello ') s2 += 'World' assert str(s2) == '\033[31mHello \033[39mWorld' # test ColorfulString to ColorfulString assert str(s + s) == '\033[30mHello\033[39m\033[30mHello\033[39m' # test ColorfulString augmented added to other ColorfulString s2 = colorful.red('World ') s2 += s assert str(s2) == '\033[31mWorld \033[39m\033[30mHello\033[39m' # test multipling ColorfulString assert str( s * 3) == '\033[30mHello\033[39m\033[30mHello\033[39m\033[30mHello\033[39m' # other str methods operate on the styled string assert s.replace('Hello', 'Adieu') == '\033[30mAdieu\033[39m'