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

[tor-commits] [nyx/master] Group menu tests by class



commit eae46c6ce9c30155777f59be51a2a53c02673ea5
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Aug 23 13:06:38 2016 -0700

    Group menu tests by class
    
    On reflection there's no reason we can only have one TestFoo class in a module.
    Good way of grouping tests by individual classes - I should really improve this
    all throughout nyx's and stem's tests...
    
    Also adding coverage for select().
---
 test/menu.py | 86 +++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 65 insertions(+), 21 deletions(-)

diff --git a/test/menu.py b/test/menu.py
index 4b03a40..683b220 100644
--- a/test/menu.py
+++ b/test/menu.py
@@ -6,11 +6,27 @@ import unittest
 
 from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup
 
+
+class Container(object):
+  value = False
+
+  def __nonzero__(self):
+    return self.value
+
+
+def action(*args):
+  IS_CALLED.value = True
+
+
 NO_OP = lambda: None
+IS_CALLED = Container()
+
 
+class TestMenuItem(unittest.TestCase):
+  def setUp(self):
+    IS_CALLED.value = False
 
-class TestMenu(unittest.TestCase):
-  def test_menu_item_parameters(self):
+  def test_parameters(self):
     menu_item = MenuItem('Test Item', NO_OP)
 
     self.assertEqual('', menu_item.prefix)
@@ -22,7 +38,37 @@ class TestMenu(unittest.TestCase):
     self.assertEqual(None, menu_item.parent)
     self.assertEqual(menu_item, menu_item.submenu)
 
-  def test_submenu_parameters(self):
+  def test_selection(self):
+    menu_item = MenuItem('Test Item', action)
+
+    self.assertFalse(IS_CALLED)
+    menu_item.select()
+    self.assertTrue(IS_CALLED)
+
+  def test_menu_item_hierarchy(self):
+    root_submenu = Submenu('Root Submenu')
+    middle_submenu = Submenu('Middle Submenu')
+
+    root_submenu.add(MenuItem('Middle Item 1', NO_OP))
+    root_submenu.add(MenuItem('Middle Item 2', NO_OP))
+    root_submenu.add(middle_submenu)
+
+    bottom_item = MenuItem('Bottom Item', NO_OP)
+    middle_submenu.add(bottom_item)
+
+    self.assertEqual(middle_submenu, bottom_item.parent)
+    self.assertEqual(middle_submenu, bottom_item.submenu)
+    self.assertEqual(bottom_item, bottom_item.next)
+    self.assertEqual(bottom_item, bottom_item.prev)
+
+    self.assertEqual(root_submenu, middle_submenu.parent)
+    self.assertEqual(middle_submenu, middle_submenu.submenu)
+    self.assertEqual('Middle Item 1', middle_submenu.next.label)
+    self.assertEqual('Middle Item 2', middle_submenu.prev.label)
+
+
+class TestSubmenu(unittest.TestCase):
+  def test_parameters(self):
     menu_item = Submenu('Test Item')
 
     self.assertEqual('', menu_item.prefix)
@@ -34,7 +80,12 @@ class TestMenu(unittest.TestCase):
     self.assertEqual(None, menu_item.parent)
     self.assertEqual(menu_item, menu_item.submenu)
 
-  def test_radio_menu_item_parameters(self):
+
+class TestRadioMenuItem(unittest.TestCase):
+  def setUp(self):
+    IS_CALLED.value = False
+
+  def test_parameters(self):
     group = RadioGroup(NO_OP, 'selected_item')
     menu_item = RadioMenuItem('Test Item', group, 'selected_item')
 
@@ -47,23 +98,16 @@ class TestMenu(unittest.TestCase):
     self.assertEqual(None, menu_item.parent)
     self.assertEqual(menu_item, menu_item.submenu)
 
-  def test_menu_item_hierarchy(self):
-    root_submenu = Submenu('Root Submenu')
-    middle_submenu = Submenu('Middle Submenu')
-
-    root_submenu.add(MenuItem('Middle Item 1', NO_OP))
-    root_submenu.add(MenuItem('Middle Item 2', NO_OP))
-    root_submenu.add(middle_submenu)
+  def test_selection(self):
+    group = RadioGroup(action, 'other_item')
+    menu_item = RadioMenuItem('Test Item', group, 'selected_item')
 
-    bottom_item = MenuItem('Bottom Item', NO_OP)
-    middle_submenu.add(bottom_item)
+    menu_item.select()
+    self.assertTrue(IS_CALLED)
 
-    self.assertEqual(middle_submenu, bottom_item.parent)
-    self.assertEqual(middle_submenu, bottom_item.submenu)
-    self.assertEqual(bottom_item, bottom_item.next)
-    self.assertEqual(bottom_item, bottom_item.prev)
+  def test_when_already_selected(self):
+    group = RadioGroup(action, 'selected_item')
+    menu_item = RadioMenuItem('Test Item', group, 'selected_item')
 
-    self.assertEqual(root_submenu, middle_submenu.parent)
-    self.assertEqual(middle_submenu, middle_submenu.submenu)
-    self.assertEqual('Middle Item 1', middle_submenu.next.label)
-    self.assertEqual('Middle Item 2', middle_submenu.prev.label)
+    menu_item.select()
+    self.assertFalse(IS_CALLED)



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