Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Styling non-strings #40

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,42 @@ def test_nested_styled_string_with_format():
assert str(s) == '\033[31mHello \033[34mawesome\033[39m\033[31m world\033[39m'


def test_styling_object_which_implements_str_proto():
@pytest.mark.parametrize('colormode', [terminal.NO_COLORS, terminal.ANSI_8_COLORS])
def test_styling_object_which_implements_str_proto(colormode):
"""
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)
colorful = core.Colorful(colormode=colormode)
assert str(colorful.black(Dummy())) == '\033[30mI am a dummy object\033[39m'


@pytest.mark.parametrize('colormode', [terminal.NO_COLORS, terminal.ANSI_8_COLORS])
def test_styling_and_adding_numbers(colormode):
"""
Test addition of styled objects that come from numbers.

A lot of the test_str_behavior things break, but numbers, addition,
and NO_COLORS is a relatively likely case and gives unexpected results
without throwing an exception.
"""
colorful = core.Colorful(colormode=colormode)
n_left = 6543
n_right = 1234

added = colorful.black(n_left) + colorful.black(n_right)
# because __format__ and __str__ use different code paths, we might still
# get a str out of this without exception even if the str() test case erred.
s_added = "{}".format(added)

assert str(n_left) in s_added
assert str(n_right) in s_added
assert len(s_added) >= len(str(n_left)) + len(str(n_right))


def test_str_behavior_for_colorfulstring():
"""
Test that the ColorfulString object behaves like a str
Expand Down