[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [nyx/master] Split up and expand _handle_key() tests



commit 0549b59f6e30a031600c16533b21a99674f6f222
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Jul 22 09:30:40 2016 -0700

    Split up and expand _handle_key() tests
    
    Good tests but we should split them to be for individula actions when
    reasonable. Also improving them a bit. Picking the (40, 80) dimensions was a
    poor choice since it makes testing cursor movement difficult.
    
    I suspect Sambuddha thought that was the window dimention. It isn't - it's the
    cursor position so actually (0, x) is easier to work with.
---
 nyx/curses.py     |  3 ++-
 test/subwindow.py | 63 ++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 41 insertions(+), 25 deletions(-)

diff --git a/nyx/curses.py b/nyx/curses.py
index e87239c..0cf9527 100644
--- a/nyx/curses.py
+++ b/nyx/curses.py
@@ -303,7 +303,8 @@ def str_input(x, y, initial_text = '', backlog = None, tab_completion = None):
 
 def _handle_key(textbox, key):
   """
-  Outputs the entered key onto the textbox.
+  Handles esc, home, end, right arrow, and resizing. We don't need ot handle
+  the left arrow key because curses has reasonable behavior for that one.
 
   :param Textbox textbox: current textbox context
   :param int key: key pressed
diff --git a/test/subwindow.py b/test/subwindow.py
index 2a03007..8eef938 100644
--- a/test/subwindow.py
+++ b/test/subwindow.py
@@ -11,7 +11,7 @@ import nyx.curses
 import nyx.panel.interpreter
 import test
 
-from mock import call, Mock, patch
+from mock import patch, call, Mock
 
 from test import require_curses
 
@@ -72,6 +72,12 @@ NO_OP_HANDLER = lambda key, textbox: None
 DIMENSIONS = (40, 80)
 
 
+def _textbox(x = 0):
+  textbox = Mock()
+  textbox.win.getyx.return_value = (0, x)
+  return textbox
+
+
 class TestCurses(unittest.TestCase):
   @require_curses
   def test_addstr(self):
@@ -126,36 +132,45 @@ class TestCurses(unittest.TestCase):
 
     self.assertEqual(EXPECTED_SCROLLBAR_BOTTOM, test.render(_draw).content.strip())
 
-  def test_handle_key(self):
-    textbox = Mock()
-    textbox.win.getyx.return_value = DIMENSIONS
-    self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(textbox, 27))
+  def test_handle_key_with_text(self):
+    self.assertEqual(ord('a'), nyx.curses._handle_key(_textbox(), ord('a')))
 
-    textbox = Mock()
-    textbox.win.getyx.return_value = DIMENSIONS
-    textbox.win.move = Mock()
-    expected_call = call(DIMENSIONS[0], 0)
+  def test_handle_key_with_esc(self):
+    self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(_textbox(), 27))
+
+  def test_handle_key_with_home(self):
+    textbox = _textbox()
     nyx.curses._handle_key(textbox, curses.KEY_HOME)
-    self.assertTrue(textbox.win.move.called)
-    self.assertEquals(expected_call, textbox.win.move.call_args)
+    self.assertEquals(call(0, 0), textbox.win.move.call_args)
 
-    textbox = Mock()
-    textbox.win.getyx.return_value = DIMENSIONS
+  def test_handle_key_with_end(self):
+    textbox = _textbox()
+    textbox.gather.return_value = 'Sample Text'
+    nyx.curses._handle_key(textbox, curses.KEY_END)
+    self.assertEquals(call(0, 10), textbox.win.move.call_args)
+
+  def test_handle_key_with_right_arrow(self):
+    textbox = _textbox()
     textbox.gather.return_value = 'Sample Text'
-    textbox.win.move = Mock()
-    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
-    self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(textbox, 410))
+    # move is called twice, to revert the gather() and move the cursor
 
-    textbox = Mock()
-    textbox.win.getyx.return_value = DIMENSIONS
-    key_pressed = ord('a')
-    self.assertEqual(key_pressed, nyx.curses._handle_key(textbox, key_pressed))
+    self.assertEquals(2, textbox.win.move.call_count)
+    self.assertEquals(call(0, 1), textbox.win.move.call_args)
+
+  def test_handle_key_with_right_arrow_at_end(self):
+    textbox = _textbox(10)
+    textbox.gather.return_value = 'Sample Text'
+    nyx.curses._handle_key(textbox, curses.KEY_RIGHT)
+
+    # move is only called to revert the gather()
+
+    self.assertEquals(1, textbox.win.move.call_count)
+    self.assertEquals(call(0, 10), textbox.win.move.call_args)
+
+  def test_handle_key_when_resized(self):
+    self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(_textbox(), 410))
 
   def test_handle_history_key(self):
     backlog = ['GETINFO version']



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits