commit f8171aa62dbf940fd1e3a24279a1c5e3fd8a3fb5 Author: Sambuddha Basu sambuddhabasu1@gmail.com Date: Sun Jul 10 20:57:30 2016 -0700
Added tests --- nyx/panel/interpreter.py | 10 +++---- test/panel/interpreter.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-)
diff --git a/nyx/panel/interpreter.py b/nyx/panel/interpreter.py index 88affb2..9839e90 100644 --- a/nyx/panel/interpreter.py +++ b/nyx/panel/interpreter.py @@ -23,7 +23,6 @@ import stem.interpreter.commands
USAGE_INFO = 'to use this panel press enter' PROMPT = '>>> ' -PROMPT_LINE = [[(PROMPT, GREEN, BOLD), (USAGE_INFO, CYAN, BOLD)]] ANSI_RE = re.compile('\x1b[([0-9;]*)m') ATTRS = {'0': NORMAL, '1': BOLD, '30': BLACK, '31': RED, '32': GREEN, '33': YELLOW, '34': BLUE, '35': MAGENTA, '36': CYAN} BACKLOG_LIMIT = 100 @@ -82,6 +81,7 @@ class InterpreterPanel(panel.Panel): ) self.autocompleter = stem.interpreter.autocomplete.Autocompleter(self.controller) self.interpreter = stem.interpreter.commands.ControlInterpretor(self.controller) + self.prompt_line = [[(PROMPT, GREEN, BOLD), (USAGE_INFO, CYAN, BOLD)]]
def key_handlers(self): def _scroll(key): @@ -98,7 +98,7 @@ class InterpreterPanel(panel.Panel): self.redraw(True) _scroll(nyx.curses.KeyInput(curses.KEY_END)) page_height = self.get_preferred_size()[0] - 1 - user_input = nyx.curses.str_input(len(PROMPT) + self._x_offset, self.top + len(PROMPT_LINE[-page_height:]), '', list(reversed(self._backlog)), self.autocompleter.matches) + user_input = nyx.curses.str_input(len(PROMPT) + self._x_offset, self.top + len(self.prompt_line[-page_height:]), '', list(reversed(self._backlog)), self.autocompleter.matches) user_input, is_done = user_input.strip(), False
if not user_input: @@ -126,11 +126,11 @@ class InterpreterPanel(panel.Panel): response = '\x1b[31;1m' + new_stderr.getvalue() sys.stderr = old_stderr if response: - PROMPT_LINE.insert(len(PROMPT_LINE) - 1, format_input(user_input)) + self.prompt_line.insert(len(self.prompt_line) - 1, format_input(user_input)) attrs = [] for line in response.split('\n'): line, attrs = ansi_to_output(line, attrs) - PROMPT_LINE.insert(len(PROMPT_LINE) - 1, line) + self.prompt_line.insert(len(self.prompt_line) - 1, line) except stem.SocketClosed: is_done = True
@@ -151,7 +151,7 @@ class InterpreterPanel(panel.Panel): subwindow.scrollbar(1, scroll, self._last_content_height - 1)
y = 1 - scroll - for entry in PROMPT_LINE: + for entry in self.prompt_line: cursor = self._x_offset
for line in entry: diff --git a/test/panel/interpreter.py b/test/panel/interpreter.py index 054aeff..a3ddd5e 100644 --- a/test/panel/interpreter.py +++ b/test/panel/interpreter.py @@ -4,7 +4,27 @@ Unit tests for nyx.panel.interpreter.
import unittest
+import nyx.curses import nyx.panel.interpreter +import test + +EXPECTED_PANEL = """ +Control Interpreter: +>>> to use this panel press enter +""".strip() + +EXPECTED_PANEL_INPUT_MODE = """ +Control Interpreter (enter "/help" for usage or a blank line to stop): +>>> to use this panel press enter +""".strip() + +EXPECTED_MULTILINE_PANEL = """ +Control Interpreter: +>>> GETINFO version +250-version=0.2.4.27 (git-412e3f7dc9c6c01a) +""".strip() + +EXPECTED_SCROLLBAR_PANEL = ' |>>> to use this panel press enter'
class TestInterpreter(unittest.TestCase): @@ -16,3 +36,57 @@ class TestInterpreter(unittest.TestCase): self.assertEqual('Green', output_line[0][1]) self.assertEqual('Bold', output_line[0][2]) self.assertEqual(['Green', 'Bold'], attrs) + + def test_format_input(self): + user_input = 'getinfo' + output = nyx.panel.interpreter.format_input(user_input) + self.assertEqual(2, len(output)) + self.assertEqual(('>>> ', 'Green', 'Bold'), output[0]) + self.assertEqual(('getinfo ', 'Green', 'Bold'), output[1]) + + user_input = 'getinfo version' + output = nyx.panel.interpreter.format_input(user_input) + self.assertEqual(3, len(output)) + self.assertEqual(('>>> ', 'Green', 'Bold'), output[0]) + self.assertEqual(('getinfo ', 'Green', 'Bold'), output[1]) + self.assertEqual(('version', 'Cyan', 'Bold'), output[2]) + + user_input = '/help' + output = nyx.panel.interpreter.format_input(user_input) + self.assertEqual(2, len(output)) + self.assertEqual(('>>> ', 'Green', 'Bold'), output[0]) + self.assertEqual(('/help', 'Magenta', 'Bold'), output[1]) + + def test_panel_name(self): + panel = nyx.panel.interpreter.InterpreterPanel() + self.assertEqual(panel.get_name(), 'interpreter') + + def test_rendering_panel(self): + panel = nyx.panel.interpreter.InterpreterPanel() + self.assertEqual(EXPECTED_PANEL, test.render(panel.draw).content) + + panel._is_input_mode = True + self.assertEqual(EXPECTED_PANEL_INPUT_MODE, test.render(panel.draw).content) + + def test_rendering_multiline_panel(self): + panel = nyx.panel.interpreter.InterpreterPanel() + panel.prompt_line = [[('>>> ', 'Green', 'Bold'), ('GETINFO', 'Green', 'Bold'), (' version', 'Cyan')]] + panel.prompt_line.append([('250-version=0.2.4.27 (git-412e3f7dc9c6c01a)', 'Blue')]) + self.assertEqual(EXPECTED_MULTILINE_PANEL, test.render(panel.draw).content) + + def test_scrollbar(self): + panel = nyx.panel.interpreter.InterpreterPanel() + self.assertIsInstance(panel._scroller, nyx.curses.Scroller) + + height = panel.get_preferred_size()[0] + panel._last_content_height = height + output_lines = test.render(panel.draw).content.split('\n') + self.assertEqual(height, len(output_lines)) + self.assertEqual(EXPECTED_SCROLLBAR_PANEL, output_lines[1]) + + def test_key_handlers(self): + panel = nyx.panel.interpreter.InterpreterPanel() + output = panel.key_handlers() + self.assertEqual(2, len(output)) + self.assertEqual('enter', output[0].key) + self.assertEqual('arrows', output[1].key)
tor-commits@lists.torproject.org