Coverage for core\test_leoKeys.py: 100%
58 statements
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
1# -*- coding: utf-8 -*-
2#@+leo-ver=5-thin
3#@+node:ekr.20210903155556.1: * @file ../unittests/core/test_leoKeys.py
4#@@first
5"""Tests of leoKeys.py"""
7import string
8from leo.core import leoGlobals as g
9from leo.core.leoTest2 import LeoUnitTest
11#@+others
12#@+node:ekr.20210903155556.2: ** class TestKeys(LeoUnitTest)
13class TestKeys(LeoUnitTest):
14 """Test cases for leoKeys.py"""
15 #@+others
16 #@+node:ekr.20210909194336.50: *3* TestKeys.test_g_KeyStroke
17 def test_g_KeyStroke(self):
18 table = [
19 # Gang of four, unmodified)
20 ('bksp', 'BackSpace'),
21 ('backspace', 'BackSpace'),
22 ('backtab', 'Tab'),
23 ('linefeed', '\n'),
24 ('\r', '\n'),
25 ('return', '\n'),
26 ('tab', 'Tab'),
27 # Gang of four, with shift mod.
28 ('Shift-bksp', 'Shift+BackSpace'),
29 ('Shift-backspace', 'Shift+BackSpace'),
30 ('Shift-backtab', 'Shift+Tab'),
31 ('Shift-linefeed', 'Shift+Return'),
32 ('Shift-\r', 'Shift+Return'),
33 ('Shift-return', 'Shift+Return'),
34 ('Shift-tab', 'Shift+Tab'),
35 # Gang of four, with Alt mod.
36 ('Alt-bksp', 'Alt+BackSpace'),
37 ('Alt-backspace', 'Alt+BackSpace'),
38 ('Alt-backtab', 'Alt+Tab'),
39 ('Alt-linefeed', 'Alt+Return'),
40 ('Alt-\r', 'Alt+Return'),
41 ('Alt-return', 'Alt+Return'),
42 ('Alt-tab', 'Alt+Tab'),
43 #
44 # #912: tilde.
45 ('~', '~'),
46 ('Shift-~', '~'),
47 #
48 # Alpha
49 ('1', '1'),
50 ('a', 'a'),
51 ('A', 'A'),
52 ('Alt-a', 'Alt+a'),
53 ('Alt-A', 'Alt+a'),
54 ('Alt-Shift-a', 'Alt+Shift+a'),
55 # We can no longer ignore the shift.
56 # ('Alt-Shift++','Alt+plus'), # Ignore the shift.
57 ('Shift-a', 'A'),
58 ('Shift-A', 'A'),
59 ('RtArrow', 'Right'),
60 ('Shift-RtArrow', 'Shift+Right'),
61 ('PageUp', 'Prior'),
62 ('Prior', 'Prior'),
63 ('Shift-PageUp', 'Shift+Prior'),
64 ('PageDn', 'Next'),
65 ('Next', 'Next'),
66 ('Shift-Next', 'Shift+Next'),
67 ('Alt-=', 'Alt+='),
68 ('Alt-+', 'Alt++'),
69 ('Alt--', 'Alt+-'),
70 ('Ctrl-RtArrow', 'Ctrl+Right'),
71 ('Control-Right', 'Ctrl+Right'),
72 ]
73 for setting, result in table:
74 stroke = g.KeyStroke(binding=setting)
75 val = stroke.s
76 assert val == result, 'For %r, expected %r, Got %r' % (setting, result, val)
77 #@+node:ekr.20210909194336.51: *3* TestKeys.test_g_KeyStroke_printable_characters_
78 def test_g_KeyStroke_printable_characters_(self):
79 # Unshifted.
80 for ch in string.printable:
81 stroke = g.KeyStroke(binding=ch)
82 assert stroke.s in string.printable, (repr(ch), repr(stroke.s))
83 if ch == '\r':
84 assert stroke.s == '\n', (repr(ch), repr(stroke.s))
85 else:
86 assert stroke.s == ch, (repr(ch), repr(stroke.s))
87 # Shifted.
88 for ch in string.digits + string.ascii_letters:
89 stroke = g.KeyStroke(binding='Shift-' + ch)
90 assert stroke.s in string.printable, (repr(ch), repr(stroke.s))
91 #@+node:ekr.20210909194336.52: *3* TestKeys.test_k_get_leo_completions
92 def test_k_get_leo_completions(self):
93 c = self.c
94 table = (
95 (50, 'c.'),
96 (3, 'p.ins'),
97 (17, 'g.print'),
98 )
99 ac = c.k.autoCompleter
100 ac.w = c.frame.body.wrapper
101 for expected, prefix in table:
102 aList = ac.get_leo_completions(prefix)
103 assert len(aList) >= expected, 'len(aList): %s, prefix: %s' % (len(aList), prefix)
104 #@+node:ekr.20210909194336.53: *3* TestKeys.test_k_isPlainKey
105 def test_k_isPlainKey(self):
106 k = self.c.k
107 for ch in (string.printable):
108 assert k.isPlainKey(ch), 'not plain: %s' % (repr(ch))
109 if 0:
110 # The NullGui class knows nothing about these characters,
111 # so these tests now fail.
112 # Happily, there is a continuous unit test in k.checkKeyEvent.
113 special = (
114 'Begin', 'Break', 'Caps_Lock', 'Clear', 'Down', 'End', 'Escape',
115 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
116 'KP_Add', 'KP_Decimal', 'KP_Divide', 'KP_Enter', 'KP_Equal',
117 'KP_Multiply, KP_Separator,KP_Space, KP_Subtract, KP_Tab',
118 'KP_F1', 'KP_F2', 'KP_F3', 'KP_F4',
119 'KP_0', 'KP_1', 'KP_2', 'KP_3', 'KP_4', 'KP_5', 'KP_6', 'KP_7', 'KP_8', 'KP_9',
120 'Home', 'Left', 'Next', 'Num_Lock',
121 'PageDn', 'PageUp', 'Pause', 'Prior', 'Right', 'Up',
122 'Sys_Req',
123 )
124 for ch in special:
125 assert not k.isPlainKey(ch), 'is plain: %s' % (ch)
126 #@+node:ekr.20210909194336.54: *3* TestKeys.test_k_print_bindings
127 def test_k_show_bindings(self):
128 c = self.c
129 c.k.showBindings()
130 #@+node:ekr.20210909194336.55: *3* TestKeys.test_k_registerCommand
131 callback_was_called = False
133 def test_k_registerCommand(self):
134 c, k = self.c, self.c.k
136 def callback(event=None, c=c):
137 self.callback_was_called = True
139 commandName = 'test-registerCommand'
140 k.registerCommand(commandName, callback)
141 k.simulateCommand(commandName)
142 assert self.callback_was_called, commandName
143 #@+node:ekr.20210901140645.8: *3* TestKeys.test_k_settings_ivars_match_settings
144 def test_k_settings_ivars_match_settings(self):
145 c = self.c
146 k = c.k
147 getBool = c.config.getBool
148 getColor = c.config.getColor
149 bg = getColor('body_text_background_color') or 'white'
150 fg = getColor('body_text_foreground_color') or 'black'
151 table = (
152 ('command_mode_bg_color', getColor('command_mode_bg_color') or bg),
153 ('command_mode_fg_color', getColor('command_mode_fg_color') or fg),
154 ('enable_alt_ctrl_bindings', getBool('enable_alt_ctrl_bindings')),
155 ('enable_autocompleter', getBool('enable_autocompleter_initially')),
156 ('enable_calltips', getBool('enable_calltips_initially')),
157 ('ignore_unbound_non_ascii_keys', getBool('ignore_unbound_non_ascii_keys')),
158 ('insert_mode_bg_color', getColor('insert_mode_bg_color') or bg),
159 ('insert_mode_fg_color', getColor('insert_mode_fg_color') or fg),
160 ('minibuffer_background_color', getColor('minibuffer_background_color') or 'lightblue'),
161 ('minibuffer_error_color', getColor('minibuffer_error_color') or 'red'),
162 ('minibuffer_warning_color', getColor('minibuffer_warning_color') or 'lightgrey'),
163 ('overwrite_mode_bg_color', getColor('overwrite_mode_bg_color') or bg),
164 ('overwrite_mode_fg_color', getColor('overwrite_mode_fg_color') or fg),
165 # ('swap_mac_keys', getBool('swap_mac_keys')),
166 ('unselected_body_bg_color', getColor('unselected_body_bg_color') or bg),
167 ('unselected_body_fg_color', getColor('unselected_body_fg_color') or bg),
168 ('warn_about_redefined_shortcuts', getBool('warn_about_redefined_shortcuts')),
169 )
170 for ivar, setting in table:
171 self.assertTrue(hasattr(k, ivar), msg=ivar)
172 val = getattr(k, ivar)
173 self.assertEqual(val, setting, msg=ivar)
174 #@-others
175#@-others
176#@-leo