[tor-commits] [nyx/master] Make test dimensions a constant

atagar at torproject.org atagar at torproject.org
Sun Jul 31 23:32:40 UTC 2016


commit 0a87754f0a597be30a8114091f0bd2dcdc24048b
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Jul 21 09:38:55 2016 -0700

    Make test dimensions a constant
    
    It's a static tuple value. This should be a constant.
---
 nyx/curses.py     | 16 +++++++---------
 test/subwindow.py | 37 +++++++++++++++++--------------------
 2 files changed, 24 insertions(+), 29 deletions(-)

diff --git a/nyx/curses.py b/nyx/curses.py
index 004a480..e87239c 100644
--- a/nyx/curses.py
+++ b/nyx/curses.py
@@ -336,16 +336,14 @@ def _handle_key(textbox, key):
 
 def _handle_history_key(next_handler, backlog, textbox, key):
   """
-  Handles history validation. When the up/down arrow keys are pressed,
-  the relative previous/next commands are shown.
+  Allows user to select previous inputs when pressing up/down.
 
   :param func next_handler: handler to invoke after this
-  :param list backlog: backlog of all previous commands entered
+  :param list backlog: backlog of all previous commands
   :param Textbox textbox: current textbox context
   :param int key: key pressed
 
-  :returns: **None** if up/down arrow key is pressed or calls function
-    to write key to the textbox
+  :returns: **None** if up/down is pressed, otherwise invokes next handler
   """
 
   global HISTORY_DICT
@@ -382,16 +380,15 @@ def _handle_history_key(next_handler, backlog, textbox, key):
 
 def _handle_tab_completion(next_handler, tab_completion, textbox, key):
   """
-  Handles tab completion. If the tab key is pressed, the current textbox
-  contents are checked for probable commands.
+  Allows user to tab complete commands if sufficient context is provided to
+  narrow to a single option.
 
   :param func next_handler: handler to invoke after this
   :param func tab_completion: function to suggest inputs to tab complete with
   :param Textbox textbox: current textbox context
   :param int key: key pressed
 
-  :returns: **None** when tab key is pressed or calls function to handle
-    history validation
+  :returns: **None** when tab is pressed, otherwise invokes next handler
   """
 
   if key == 9:
@@ -403,6 +400,7 @@ def _handle_tab_completion(next_handler, tab_completion, textbox, key):
       new_input = matches[0]
     elif len(matches) > 1:
       common_prefix = os.path.commonprefix(matches)
+
       if common_prefix != current_contents:
         new_input = common_prefix
 
diff --git a/test/subwindow.py b/test/subwindow.py
index 559c451..2a03007 100644
--- a/test/subwindow.py
+++ b/test/subwindow.py
@@ -69,6 +69,7 @@ EXPECTED_SCROLLBAR_BOTTOM = """
 """.strip()
 
 NO_OP_HANDLER = lambda key, textbox: None
+DIMENSIONS = (40, 80)
 
 
 class TestCurses(unittest.TestCase):
@@ -126,56 +127,53 @@ class TestCurses(unittest.TestCase):
     self.assertEqual(EXPECTED_SCROLLBAR_BOTTOM, test.render(_draw).content.strip())
 
   def test_handle_key(self):
-    dimensions = (40, 80)
-
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
     self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(textbox, 27))
 
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
     textbox.win.move = Mock()
-    expected_call = call(dimensions[0], 0)
+    expected_call = call(DIMENSIONS[0], 0)
     nyx.curses._handle_key(textbox, curses.KEY_HOME)
     self.assertTrue(textbox.win.move.called)
     self.assertEquals(expected_call, textbox.win.move.call_args)
 
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
     textbox.gather.return_value = 'Sample Text'
     textbox.win.move = Mock()
-    expected_call = call(*dimensions)
+    expected_call = call(*DIMENSIONS)
     nyx.curses._handle_key(textbox, curses.KEY_RIGHT)
     self.assertTrue(textbox.win.move.called)
     self.assertEquals(expected_call, textbox.win.move.call_args)
 
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
     self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(textbox, 410))
 
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
     key_pressed = ord('a')
     self.assertEqual(key_pressed, nyx.curses._handle_key(textbox, key_pressed))
 
   def test_handle_history_key(self):
     backlog = ['GETINFO version']
-    dimensions = (40, 80)
 
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
     self.assertIsNone(nyx.curses._handle_history_key(NO_OP_HANDLER, [], textbox, curses.KEY_UP))
 
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
-    textbox.win.getmaxyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
+    textbox.win.getmaxyx.return_value = DIMENSIONS
     textbox.win.addstr = Mock()
     textbox.win.move = Mock()
     nyx.curses._handle_history_key(NO_OP_HANDLER, backlog, textbox, curses.KEY_UP)
     self.assertTrue(textbox.win.clear.called)
-    expected_addstr_call = call(dimensions[0], 0, backlog[0])
+    expected_addstr_call = call(DIMENSIONS[0], 0, backlog[0])
     self.assertEqual(expected_addstr_call, textbox.win.addstr.call_args)
-    expected_move_call = call(dimensions[0], len(backlog[0]))
+    expected_move_call = call(DIMENSIONS[0], len(backlog[0]))
     self.assertEqual(expected_move_call, textbox.win.move.call_args)
 
     textbox = Mock()
@@ -185,19 +183,18 @@ class TestCurses(unittest.TestCase):
 
   @patch('nyx.curses._handle_history_key')
   def test_handle_tab_completion(self, mock_handle_history_key):
-    dimensions = (40, 80)
     tab_completion_content = 'GETINFO version'
 
     textbox = Mock()
-    textbox.win.getyx.return_value = dimensions
-    textbox.win.getmaxyx.return_value = dimensions
+    textbox.win.getyx.return_value = DIMENSIONS
+    textbox.win.getmaxyx.return_value = DIMENSIONS
     textbox.win.addstr = Mock()
     textbox.win.move = Mock()
     tab_completion = Mock()
     tab_completion.return_value = [tab_completion_content]
     nyx.curses._handle_tab_completion(NO_OP_HANDLER, tab_completion, textbox, 9)
     self.assertTrue(textbox.win.clear.called)
-    expected_addstr_call = call(dimensions[0], 0, tab_completion_content)
+    expected_addstr_call = call(DIMENSIONS[0], 0, tab_completion_content)
     self.assertEqual(expected_addstr_call, textbox.win.addstr.call_args)
-    expected_move_call = call(dimensions[0], len(tab_completion_content))
+    expected_move_call = call(DIMENSIONS[0], len(tab_completion_content))
     self.assertTrue(expected_move_call, textbox.win.move.call_args)





More information about the tor-commits mailing list