def run(): """Execute the challenges.006e module.""" # Create the menus. main_title = 'Choose the desired method for the calculation of pi' main_menuitems = [ textmenu.TextMenuItem('1', lambda: calc_pi(bbp), "Bailey-Borwein-Plouffe formula"), textmenu.TextMenuItem('2', lambda: calc_pi(bellard), "Bellard's formula"), textmenu.TextMenuItem('3', lambda: calc_pi(chudnovsky), "Chudnovsky algorithm"), textmenu.TextMenuItem('q', 'quit', 'Quit') ] menus = { 'main': textmenu.TextMenu(main_title, main_menuitems), } textmenu.TextMenuEngine(menus, 'main').run()
def test_get_action(self): """Test method :meth:`plugins.textmenu.TextMenu.get_action` **Tested:** - The returned action is correct when the passed text menu item ID exists. - A ValueError is raised when the passed text menu item ID does not exist. """ items = [ textmenu.TextMenuItem('1', 'action', 'Menu Item 1'), textmenu.TextMenuItem('q', 'quit', 'Quit'), ] tm = textmenu.TextMenu('Main Menu', items) self.assertEqual(tm.get_action('1'), 'action') self.assertEqual(tm.get_action('q'), 'quit') with self.assertRaises(ValueError): tm.get_action('foo')
def test_map_id_to_action(self): """Test method :meth:`plugins.textmenu.TextMenu.map_id_to_action` **Tested:** - The returned dictionary correctly maps text menu item IDs to actions. """ items = [ textmenu.TextMenuItem('1', 'action', 'Menu Item 1'), textmenu.TextMenuItem('q', 'quit', 'Quit'), ] tm = textmenu.TextMenu('Main Menu', items) result = tm.map_id_to_action() expected = { '1': 'action', 'q': 'quit', } self.assertEqual(result, expected)
def test___str__(self): """Test method :meth:`plugins.textmenu.TextMenu.__str__` **Tested:** - The returned string is correct. """ items = [ textmenu.TextMenuItem('1', 'action', 'Menu Item 1'), textmenu.TextMenuItem('q', 'quit', 'Quit'), ] tm = textmenu.TextMenu('Main Menu', items) result = tm.__str__() expected = '\n'.join([ "=== Main Menu ===", "1. Menu Item 1", "q. Quit", ]) self.assertEqual(result, expected)
def test___str__(self): """Test method :meth:`plugins.textmenu.TextMenuItem.__str__` **Tested:** - The returned string is correct. """ tmi = textmenu.TextMenuItem('1', 'action', 'Menu Item 1') result = tmi.__str__() expected = "1. Menu Item 1" self.assertEqual(result, expected)
def test___init__(self): """Test method :meth:`plugins.textmenu.TextMenuItem.__init__` **Tested:** - The attributes of a text menu item are correct after initialization. """ tmi = textmenu.TextMenuItem('1', 'action', 'Menu Item 1') self.assertEqual(tmi.id, '1') self.assertEqual(tmi.action, 'action') self.assertEqual(tmi.descr, 'Menu Item 1')
def test_is_valid_id(self): """Test method :meth:`plugins.textmenu.TextMenu.is_valid_id` **Tested:** - The returned boolean is True if the passed user is valid. - The returned boolean is False if the passed user is invalid. """ items = [ textmenu.TextMenuItem('1', 'action', 'Menu Item 1'), ] tm = textmenu.TextMenu('Main Menu', items) self.assertTrue(tm.is_valid_id('1')) self.assertFalse(tm.is_valid_id('foo'))
def run(): """Execute the challenges.002e module.""" cubes_menuitems = [ tm.TextMenuItem('1', calc_cube_surface_area, 'Surface Area'), tm.TextMenuItem('2', calc_cube_volume, 'Volume'), tm.TextMenuItem('b', 'back', 'Back'), tm.TextMenuItem('q', 'quit', 'Quit'), ] spheres_menuitems = [ tm.TextMenuItem('1', calc_sphere_surface_area, 'Surface Area'), tm.TextMenuItem('2', calc_sphere_volume, 'Volume'), tm.TextMenuItem('b', 'back', 'Back'), tm.TextMenuItem('q', 'quit', 'Quit'), ] geometrical_menuitems = [ tm.TextMenuItem('1', 'cubes_menu', 'Cubes'), tm.TextMenuItem('2', 'spheres_menu', 'Spheres'), tm.TextMenuItem('b', 'back', 'Back'), tm.TextMenuItem('q', 'quit', 'Quit'), ] main_menuitems = [ tm.TextMenuItem('1', 'geometrical_menu', 'Geometrical'), tm.TextMenuItem('q', 'quit', 'Quit'), ] menus = { 'main_menu': tm.TextMenu('MAIN', main_menuitems), 'geometrical_menu': tm.TextMenu('GEOMETRICAL', geometrical_menuitems), 'spheres_menu': tm.TextMenu('SPHERES', spheres_menuitems), 'cubes_menu': tm.TextMenu('CUBES', cubes_menuitems) } calculator = tm.TextMenuEngine(menus, 'main_menu') calculator.run()