Fixing tests

This commit is contained in:
Michael Lazar
2017-09-20 13:35:20 -04:00
parent 978cdb918a
commit 552c178fe6
4 changed files with 76 additions and 17 deletions

View File

@@ -99,7 +99,7 @@ class Page(object):
def previous_theme(self): def previous_theme(self):
theme = self.term.theme_list.previous(self.term.theme) theme = self.term.theme_list.previous(self.term.theme)
while not self.term.test_theme(theme): while not self.term.check_theme(theme):
theme = self.term.theme_list.previous(theme) theme = self.term.theme_list.previous(theme)
self.term.set_theme(theme) self.term.set_theme(theme)
@@ -111,7 +111,7 @@ class Page(object):
def next_theme(self): def next_theme(self):
theme = self.term.theme_list.next(self.term.theme) theme = self.term.theme_list.next(self.term.theme)
while not self.term.test_theme(theme): while not self.term.check_theme(theme):
theme = self.term.theme_list.next(theme) theme = self.term.theme_list.next(theme)
self.term.set_theme(theme) self.term.set_theme(theme)

View File

@@ -398,7 +398,7 @@ class Terminal(object):
_logger.warning(stderr) _logger.warning(stderr)
self.show_notification( self.show_notification(
'Program exited with status={0}\n{1}'.format( 'Program exited with status={0}\n{1}'.format(
code, stderr.strip()), style='error') code, stderr.strip()), style='Error')
else: else:
# Non-blocking, open a background process # Non-blocking, open a background process
@@ -827,13 +827,15 @@ class Terminal(object):
""" """
Shortcut for fetching the color + attribute code for an element. Shortcut for fetching the color + attribute code for an element.
""" """
# The theme must be initialized before calling this
assert self.theme is not None
return self.theme.get(element) return self.theme.get(element)
@staticmethod @staticmethod
def test_theme(theme): def check_theme(theme):
""" """
Check if the given theme is supported by the terminal Check if the given theme is compatible with the terminal
""" """
terminal_colors = curses.COLORS if curses.has_colors() else 0 terminal_colors = curses.COLORS if curses.has_colors() else 0

View File

@@ -167,6 +167,8 @@ def stdscr():
curses.color_pair.return_value = 23 curses.color_pair.return_value = 23
curses.has_colors.return_value = True curses.has_colors.return_value = True
curses.ACS_VLINE = 0 curses.ACS_VLINE = 0
curses.COLORS = 256
curses.COLOR_PAIRS = 256
yield out yield out
@@ -199,6 +201,7 @@ def reddit(vcr, request):
@pytest.fixture() @pytest.fixture()
def terminal(stdscr, config): def terminal(stdscr, config):
term = Terminal(stdscr, config=config) term = Terminal(stdscr, config=config)
term.set_theme()
# Disable the python 3.4 addch patch so that the mock stdscr calls are # Disable the python 3.4 addch patch so that the mock stdscr calls are
# always made the same way # always made the same way
term.addch = lambda window, *args: window.addch(*args) term.addch = lambda window, *args: window.addch(*args)

View File

@@ -575,24 +575,78 @@ def test_add_space(terminal, stdscr):
def test_attr(terminal): def test_attr(terminal):
assert terminal.attr('cursor') == 0 assert terminal.attr('CursorBlock') == 0
assert terminal.attr('cursor.selected') == curses.A_REVERSE assert terminal.attr('@CursorBlock') == curses.A_REVERSE
assert terminal.attr('neutral_vote') == curses.A_BOLD assert terminal.attr('NeutralVote') == curses.A_BOLD
with terminal.theme.set_modifier('selected'): with terminal.theme.turn_on_selected():
assert terminal.attr('cursor') == curses.A_REVERSE assert terminal.attr('CursorBlock') == curses.A_REVERSE
assert terminal.attr('neutral_vote') == curses.A_BOLD assert terminal.attr('NeutralVote') == curses.A_BOLD
def test_check_theme(terminal):
monochrome = Theme(use_color=False)
default = Theme()
color256 = Theme.from_name('molokai')
curses.has_colors.return_value = False
assert terminal.check_theme(monochrome)
assert not terminal.check_theme(default)
assert not terminal.check_theme(color256)
curses.has_colors.return_value = True
curses.COLORS = 0
assert terminal.check_theme(monochrome)
assert not terminal.check_theme(default)
assert not terminal.check_theme(color256)
curses.COLORS = 8
assert terminal.check_theme(monochrome)
assert terminal.check_theme(default)
assert not terminal.check_theme(color256)
curses.COLORS = 256
assert terminal.check_theme(monochrome)
assert terminal.check_theme(default)
assert terminal.check_theme(color256)
curses.COLOR_PAIRS = 8
assert terminal.check_theme(monochrome)
assert terminal.check_theme(default)
assert not terminal.check_theme(color256)
def test_set_theme(terminal, stdscr): def test_set_theme(terminal, stdscr):
# Default with color enabled
stdscr.reset_mock() stdscr.reset_mock()
terminal.set_theme() terminal.set_theme()
assert not terminal.theme.monochrome assert terminal.theme.use_color
assert terminal.theme.display_string == 'default (built-in)'
stdscr.bkgd.assert_called_once_with(' ', 0) stdscr.bkgd.assert_called_once_with(' ', 0)
stdscr.reset_mock() # When the user passes in the --monochrome flag
theme = Theme(monochrome=True) terminal.theme = None
terminal.set_theme(theme=theme) terminal.set_theme(Theme(use_color=False))
assert terminal.theme.monochrome assert not terminal.theme.use_color
stdscr.bkgd.assert_called_once_with(' ', 0) assert terminal.theme.display_string == 'monochrome (built-in)'
# When the terminal doesn't support colors
curses.COLORS = 0
terminal.theme = None
terminal.set_theme()
assert terminal.theme.display_string == 'monochrome (built-in)'
# When the terminal doesn't support 256 colors so it falls back to the
# built-in default theme
curses.COLORS = 8
terminal.theme = None
terminal.set_theme(Theme.from_name('molokai'))
assert terminal.theme.display_string == 'default (built-in)'
# When the terminal does support the 256 color theme
curses.COLORS = 256
terminal.theme = None
terminal.set_theme(Theme.from_name('molokai'))
assert terminal.theme.display_string == 'molokai (preset)'