def test_invalid_modifier(self): with self.assertRaises(ValueError) as cm: Key('shit-e') self.assertIn('Invalid modifier', str(cm.exception)) with self.assertRaises(ValueError) as cm: Key('alt-') self.assertIn('Missing key', str(cm.exception))
def test_invalid_key(self): with self.assertRaises(ValueError) as cm: Key('hello') self.assertIn('Unknown key', str(cm.exception)) with self.assertRaises(ValueError) as cm: Key('alt-hello') self.assertIn('Unknown key', str(cm.exception))
def test_mkkey(self): self.assertEqual(self.km.mkkey(Key('x')), Key('x')) self.assertEqual(self.km.mkkey(KeyChain('1', '2', '3')), KeyChain('1', '2', '3')) self.assertEqual(self.km.mkkey('x'), Key('x')) self.assertEqual(self.km.mkkey('x y z'), KeyChain('x', 'y', 'z')) self.assertEqual(self.km.mkkey('x+y+z'), KeyChain('x', 'y', 'z')) self.assertEqual(self.km.mkkey('x +'), KeyChain('x', '+')) self.assertEqual(self.km.mkkey('+ x'), KeyChain('+', 'x')) self.assertEqual(self.km.mkkey('x y +'), KeyChain('x', 'y', '+')) self.assertEqual(self.km.mkkey('+ y z'), KeyChain('+', 'y', 'z')) self.assertEqual(self.km.mkkey('+ + +'), KeyChain('+', '+', '+'))
def test_invalid_key(self): with self.assertRaises(ValueError) as cm: Key('hello') self.assertIn('Unknown key', str(cm.exception)) with self.assertRaises(ValueError) as cm: Key('alt-hello') self.assertIn('Unknown key', str(cm.exception)) with self.assertRaises(ValueError) as cm: Key('') self.assertIn('No key', str(cm.exception)) with self.assertRaises(TypeError) as cm: Key(object()) self.assertIn('Not a string', str(cm.exception))
def test_key_translation(self): widget = self.km.wrap(urwid.Text)('Test Text') self.km.bind(key='a', action=lambda widget: widget.set_text('Key pressed: a')) self.km.bind(key='b', action=Key('a')) widget.keypress((80,), 'b') self.assertEqual(widget.text, 'Key pressed: a')
def test_compare_Key_with_str(self): self.assertEqual(Key('enter'), '\n') self.assertEqual(Key('enter'), 'return') self.assertEqual(Key('pgdn'), 'page down') self.assertEqual(Key('pgup'), 'page up') self.assertEqual(Key('space'), ' ') self.assertEqual(Key('ins'), 'insert') self.assertEqual(Key('insert'), 'ins')
def test_key_translation_to_builtin_key(self): class SelectableText(urwid.Text): def selectable(self): return True def keypress(self, size, key): return key list_contents = [SelectableText(str(i)) for i in range(1, 10)] listw = self.km.wrap(urwid.ListBox, context='list')(urwid.SimpleFocusListWalker(list_contents)) self.km.bind(key='j', action=Key('down')) self.assertEqual(listw.focus_position, 0) listw.keypress((3, 3), 'j') self.assertEqual(listw.focus_position, 1)
def test_evaluated_keys_are_offered_to_parent_again(self): list_contents = [urwid.Text(str(i)) for i in range(1, 10)] action = FakeAction() widget = self.mk_widget(urwid.ListBox, urwid.SimpleFocusListWalker(list_contents), context='list', callback=action) self.keymap.bind('j', context='list', action=Key('down')) size = (3, 3) self.assert_lines(widget, size, exp_lines=('1 ', '2 ', '3 ')) widget.keypress(size, 'j') self.assertEqual(action.callnum, 0) self.assert_lines(widget, size, exp_lines=('2 ', '3 ', '4 '))
def test_unbind(self): self.km.bind(key='a', action='foo') self.assertIn(Key('a'), self.km.keys()) self.km.unbind(key='a') self.assertNotIn(Key('a'), self.km.keys()) self.km.bind(key='b', action='foo') with self.assertRaises(ValueError) as cm: self.km.unbind(key='c') self.assertIn("Key not mapped in context 'default'", str(cm.exception)) self.assertIn(str(Key('c')), str(cm.exception)) self.km.bind(key='d', action='foo') with self.assertRaises(ValueError) as cm: self.km.unbind(key='d', context='bar') self.assertIn("Unknown context: 'bar'", str(cm.exception)) self.km.bind(key='e', action='foo', context='kablam') with self.assertRaises(ValueError) as cm: self.km.unbind(key='e') self.assertIn("Key not mapped in context 'default'", str(cm.exception)) self.assertIn(str(Key('e')), str(cm.exception))
def test_evaluated_key_does_not_replace_original_key(self): # Create a list of widgets that translate 'j' to 'down' in their # keypress() methods. lst_contents = [ self.mk_widget(urwid.Text, str(i), context='item') for i in range(1, 10) ] self.keymap.bind('j', context='item', action=Key('down')) # Create ListBox with separate key context. If the ListBox gets to # handle 'j', it just checks a mark we can look for. lst_widget = self.mk_widget(urwid.ListBox, urwid.SimpleFocusListWalker(lst_contents), context='list') lst_got_j = FakeAction() self.keymap.bind('j', context='list', action=lst_got_j) # Make sure everything works regularly size = (3, 3) self.assert_lines(lst_widget, size, exp_lines=('1 ', '2 ', '3 '), exp_focus_pos=0) lst_widget.keypress(size, 'down') self.assert_lines(lst_widget, size, exp_lines=('1 ', '2 ', '3 '), exp_focus_pos=1) # Do the actual test: Pressing 'j' should pass 'j' to the focused item, # which evaluates it to 'down'. But the list widget must get 'j'. lst_widget.keypress(size, 'j') self.assert_lines(lst_widget, size, exp_lines=('1 ', '2 ', '3 '), exp_focus_pos=1) self.assertEqual(lst_got_j.callnum, 1)
def test_unbind(self): self.km.bind(key='a', action='foo') self.assertIn(Key('a'), self.km.keys()) self.km.unbind(key='a') self.assertNotIn(Key('a'), self.km.keys()) self.km.bind(key='b', action='foo') with self.assertRaises(ValueError) as cm: self.km.unbind(key='c') self.assertIn("Key not mapped in context 'default'", str(cm.exception)) self.assertIn(str(Key('c')), str(cm.exception)) self.km.bind(key='d', action='foo') with self.assertRaises(ValueError) as cm: self.km.unbind(key='d', context='bar') self.assertIn("Unknown context: 'bar'", str(cm.exception)) self.km.bind(key='e', action='foo', context='kablam') with self.assertRaises(ValueError) as cm: self.km.unbind(key='e') self.assertIn("Key not mapped in context 'default'", str(cm.exception)) self.assertIn(str(Key('e')), str(cm.exception)) self.km.bind(key='f', action='foo') self.km.bind(key='f', action='foo', context='fubar') self.km.bind(key='f', action='food', context='imhungry') self.km.unbind(key='f') self.assertIn(Key('f'), self.km.keys()) self.km.unbind(key='f', context=self.km.ALL_CONTEXTS) self.assertNotIn(Key('f'), self.km.keys()) with self.assertRaises(ValueError) as cm: self.km.unbind(key='f', context=self.km.ALL_CONTEXTS) self.assertEqual( 'Key ' + str(Key('f')) + ' not mapped in any context.', str(cm.exception))
def test_convert_multiple_modifiers(self): self.assertEqual(Key('shift meta right'), Key('shift-alt-right')) self.assertEqual(Key('meta shift right'), Key('shift-alt-right')) self.assertEqual(Key('meta ctrl x'), Key('ctrl-alt-x')) self.assertEqual(Key('ctrl meta shift u'), Key('ctrl-meta-U'))
def test_convert_shift_modifier(self): self.assertEqual(Key('shift-E'), Key('E')) self.assertEqual(Key('shift-e'), Key('E')) self.assertEqual(Key('shift-ö'), Key('Ö')) self.assertEqual(Key('shift-alt-ö'), Key('alt-Ö')) self.assertEqual(Key('ctrl-shift-alt-ö'), Key('ctrl-alt-Ö'))
def test_compare_Key_with_invalid_value(self): self.assertNotEqual(Key('space'), 'foo') self.assertNotEqual(Key('space'), range(10))
def test_compare_Key_with_Key(self): self.assertEqual(Key('alt-l'), Key('Alt-l')) self.assertEqual(Key('alt-l'), Key('meta-l')) self.assertEqual(Key('alt-l'), Key('Meta-l')) self.assertEqual(Key('alt-l'), Key('<alt-l>')) self.assertNotEqual(Key('alt-l'), Key('alt-L')) self.assertEqual(Key('ctrl-e'), Key('Ctrl-e')) self.assertEqual(Key('ctrl-e'), Key('<ctrl-e>')) self.assertEqual(Key('ctrl-e'), Key('CTRL-e')) self.assertEqual(Key('ctrl-e'), Key('ctrl-E')) self.assertEqual(Key('space'), Key(' ')) self.assertEqual(Key('escape'), Key('esc')) self.assertEqual(Key('home'), Key('pos1')) self.assertEqual(Key('delete'), Key('del')) self.assertEqual(Key('insert'), Key('ins')) self.assertEqual(Key('enter'), Key('return')) self.assertEqual(Key('enter'), Key('\n')) self.assertEqual(Key('alt-enter'), Key('meta-\n')) self.assertEqual(Key('alt-insert'), Key('meta ins')) self.assertEqual(Key('alt-del'), Key('meta delete')) self.assertEqual(Key('shift-ctrl-enter'), Key('shift-Ctrl-RETURN')) self.assertEqual(Key('alt-space'), Key('meta ')) self.assertEqual(Key('alt-pgup'), Key('meta page up'))