def test_key_left_exits_returns_none(self): nm = NumberedMenu(build_contents(), get_mock_input(), get_mock_output(), name=nm_name, config={}) def scenario(): nm.deactivate() assert not nm.in_foreground with patch.object(nm, 'idle_loop', side_effect=scenario) as p: nm.activate()
def on_start(self): super(NumberedInputTestApp, self).on_start() self.n_menu = NumberedMenu(self.main_menu_contents, self.i, self.o, self.menu_name, prepend_numbers=True, input_delay=1) self.n_menu.activate()
def test_no_prepend_numbers(self): """Tests whether the NumberedMenu stops prepending numbers when it gets the corresponding argument""" nm = NumberedMenu(build_contents(3), get_mock_input(), get_mock_output(), prepend_numbers=False, name=nm_name, config={}) def scenario(): nm.deactivate() with patch.object(nm, 'idle_loop', side_effect=scenario) as p: nm.activate() assert nm.o.display_data.called assert nm.o.display_data.call_count == 1 #One in to_foreground assert nm.o.display_data.call_args[0] == ('A0', 'A1', 'A2', nm.exit_entry[0])
def test_shows_data_on_screen(self): """Tests whether the NumberedMenu outputs data on screen when it's ran""" nm = NumberedMenu(build_contents(3), get_mock_input(), get_mock_output(), name=nm_name, config={}) def scenario(): nm.deactivate() with patch.object(nm, 'idle_loop', side_effect=scenario) as p: nm.activate() #The scenario should only be called once assert nm.idle_loop.called assert nm.idle_loop.call_count == 1 assert nm.o.display_data.called assert nm.o.display_data.call_count == 1 #One in to_foreground assert nm.o.display_data.call_args[0] == ('0 A0', '1 A1', '2 A2', '3 {}'.format(nm.exit_entry[0]))
class ContactApp(ZeroApp): def __init__(self, i, o): super(ContactApp, self).__init__(i, o) self.menu_name = "Contacts" self.address_book = AddressBook() self.menu = None def on_start(self): self.address_book.load_from_file() self.menu = NumberedMenu(self.create_menu_content(), self.i, self.o, prepend_numbers=False) self.menu.activate() def create_menu_content(self): all_contacts = self.address_book.contacts return [[c.short_name(), lambda x=c: self.create_contact_page(x)] for c in all_contacts] def create_contact_page(self, contact): # type: (Contact) -> None contact_attrs = [getattr(contact, a) for a in contact.get_filled_attributes()] Listbox(i=self.i, o=self.o, contents=contact_attrs).activate()
class NumberedInputTestApp(ZeroApp): def __init__(self, i, o): super(NumberedInputTestApp, self).__init__(i, o) self.n_menu = None self.menu_name = "Numbered Input Menu" hellos = ["hello", "hello again", "ditto", "same"] self.main_menu_contents = [[ hellos[i % 4], lambda x=i: self.print_hello(x, hellos[x % 4]) ] for i in range(16)] @staticmethod def print_hello(index, hello): logger.info("{} {}".format(index, hello)) def on_start(self): self.n_menu = NumberedMenu(self.main_menu_contents, self.i, self.o, self.menu_name, prepend_numbers=True, input_delay=1) self.n_menu.activate()
def test_exit_label_leakage(self): """tests whether the exit label of one NumberedMenu leaks into another""" i = get_mock_input() o = get_mock_output() m1 = NumberedMenu(build_contents(), i, o, name=nm_name + "1", config={}) m2 = NumberedMenu(build_contents(), i, o, name=nm_name + "2", config={}) m3 = NumberedMenu(build_contents(), i, o, name=nm_name + "3", config={}) m1.exit_entry = [["Refresh", lambda: None]] m2.exit_entry[0] = "Exit" assert (m1.exit_entry != m2.exit_entry) assert (m2.exit_entry != m3.exit_entry) assert (m1.exit_entry != m3.exit_entry)
def test_recursive_process_contents(self): """ If set_contents has been called on contents that were extracted from an existing NumberedMenu, entry labels should remain the same. This ensures that process_contents doesn't actually add numbers to the labels, but only does it when get_displayed_contents() is called. """ contents_1 = build_contents() nm = NumberedMenu(contents_1, get_mock_input(), get_mock_output(), name=nm_name, config={}) # There will be an "exit" entry added in the end of new contents, # so we have to compensate for that labels_1 = [entry[0] for entry in contents_1] labels_2 = [entry[0] for entry in nm.contents] for i, label_1 in enumerate(labels_1): label_2 = labels_2[i] assert label_1 == label_2
def test_keymap(self): """tests keymap""" nm = NumberedMenu(build_contents(), get_mock_input(), get_mock_output(), name=nm_name, config={}) self.assertIsNotNone(nm.keymap) for key_name, callback in nm.keymap.iteritems(): self.assertIsNotNone(callback)
def test_constructor(self): """tests constructor""" nm = NumberedMenu(build_contents(), get_mock_input(), get_mock_output(), name=nm_name, config={}) self.assertIsNotNone(nm)
def on_start(self): self.address_book.load_from_file() self.menu = NumberedMenu(self.create_menu_content(), self.i, self.o, prepend_numbers=False) self.menu.activate()