[tor-commits] [nyx/master] Unit tests for MenuCursor
atagar at torproject.org
atagar at torproject.org
Sat Aug 27 18:23:50 UTC 2016
commit 009f80a11f8d5fb2cf567bd7cd13834691abeb90
Author: Damian Johnson <atagar at torproject.org>
Date: Fri Aug 26 10:09:50 2016 -0700
Unit tests for MenuCursor
Adding tests for the MenuCursor class. It's a simple and easily testable one so
simple to add good coverage.
---
test/menu.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 97 insertions(+), 1 deletion(-)
diff --git a/test/menu.py b/test/menu.py
index a0772b2..162e08c 100644
--- a/test/menu.py
+++ b/test/menu.py
@@ -2,9 +2,12 @@
Unit tests for nyx.menu.
"""
+import curses
import unittest
-from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup
+import nyx.curses
+
+from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup, MenuCursor
class Container(object):
@@ -21,9 +24,35 @@ def action(*args):
IS_CALLED.value = args if args else True
+def menu_cursor(*key_inputs):
+ cursor = MenuCursor(INITIAL_SELECTION)
+
+ for key in key_inputs:
+ cursor.handle_key(nyx.curses.KeyInput(key))
+
+ return cursor
+
+
NO_OP = lambda: None
IS_CALLED = Container()
+TEST_MENU = Submenu('Root Submenu', [
+ Submenu('Submenu 1', [
+ MenuItem('Item 1', action, 'selected 1'),
+ MenuItem('Item 2', action, 'selected 2'),
+ Submenu('Inner Submenu', [
+ MenuItem('Item 3', action, 'selected 3'),
+ ]),
+ Submenu('Empty Submenu', []),
+ ]),
+ Submenu('Submenu 2', [
+ MenuItem('Item 4', action, 'selected 1'),
+ MenuItem('Item 5', action, 'selected 2'),
+ ])
+])
+
+INITIAL_SELECTION = TEST_MENU.children[0].children[0]
+
class TestMenuItem(unittest.TestCase):
def setUp(self):
@@ -147,3 +176,70 @@ class TestRadioMenuItem(unittest.TestCase):
menu_item.select()
self.assertFalse(IS_CALLED)
+
+
+class TestMenuCursor(unittest.TestCase):
+ def setUp(self):
+ IS_CALLED.value = False
+
+ def test_selection_of_item(self):
+ cursor = menu_cursor(curses.KEY_ENTER)
+ self.assertEqual(('selected 1',), IS_CALLED.value)
+ self.assertTrue(cursor.is_done)
+
+ def test_selection_of_submenu(self):
+ cursor = menu_cursor(curses.KEY_DOWN, curses.KEY_DOWN, curses.KEY_ENTER)
+ self.assertEqual('Item 3', cursor.selection.label)
+ self.assertFalse(IS_CALLED.value)
+ self.assertFalse(cursor.is_done)
+
+ def test_up(self):
+ cursor = menu_cursor()
+
+ for expected in ('Item 1', 'Empty Submenu', 'Inner Submenu', 'Item 2', 'Item 1'):
+ self.assertEqual(expected, cursor.selection.label)
+ cursor.handle_key(nyx.curses.KeyInput(curses.KEY_UP))
+
+ def test_down(self):
+ cursor = menu_cursor()
+
+ for expected in ('Item 1', 'Item 2', 'Inner Submenu', 'Empty Submenu', 'Item 1'):
+ self.assertEqual(expected, cursor.selection.label)
+ cursor.handle_key(nyx.curses.KeyInput(curses.KEY_DOWN))
+
+ def test_left(self):
+ cursor = menu_cursor()
+
+ for expected in ('Item 1', 'Item 4', 'Item 1'):
+ self.assertEqual(expected, cursor.selection.label)
+ cursor.handle_key(nyx.curses.KeyInput(curses.KEY_LEFT))
+
+ def test_left_when_inner_submenu(self):
+ cursor = menu_cursor(curses.KEY_DOWN, curses.KEY_DOWN, curses.KEY_RIGHT)
+
+ for expected in ('Item 3', 'Inner Submenu', 'Item 4'):
+ self.assertEqual(expected, cursor.selection.label)
+ cursor.handle_key(nyx.curses.KeyInput(curses.KEY_LEFT))
+
+ def test_right(self):
+ cursor = menu_cursor()
+
+ for expected in ('Item 1', 'Item 4', 'Item 1'):
+ self.assertEqual(expected, cursor.selection.label)
+ cursor.handle_key(nyx.curses.KeyInput(curses.KEY_RIGHT))
+
+ def test_right_when_inner_submenu(self):
+ cursor = menu_cursor(curses.KEY_DOWN, curses.KEY_DOWN)
+
+ for expected in ('Inner Submenu', 'Item 3', 'Item 4', 'Item 1'):
+ self.assertEqual(expected, cursor.selection.label)
+ cursor.handle_key(nyx.curses.KeyInput(curses.KEY_RIGHT))
+
+ def test_esc(self):
+ cursor = menu_cursor(27)
+ self.assertTrue(cursor.is_done)
+
+ # pressing 'm' closes the menu too
+
+ cursor = menu_cursor(ord('m'))
+ self.assertTrue(cursor.is_done)
More information about the tor-commits
mailing list