1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Tests for pw_console.console_app""" 15 16import inspect 17import logging 18import unittest 19from unittest.mock import MagicMock 20 21from jinja2 import Environment, PackageLoader, make_logging_undefined 22from prompt_toolkit.key_binding import KeyBindings 23 24from pw_console.help_window import HelpWindow 25 26_PW_CONSOLE_MODULE = 'pw_console' 27 28 29_jinja_env = Environment( 30 loader=PackageLoader(_PW_CONSOLE_MODULE), 31 undefined=make_logging_undefined(logger=logging.getLogger('pw_console')), 32 trim_blocks=True, 33 lstrip_blocks=True, 34) 35 36 37def _create_app_mock(): 38 template = _jinja_env.get_template('keybind_list.jinja') 39 mock_app = MagicMock() 40 mock_app.get_template = MagicMock(return_value=template) 41 return mock_app 42 43 44class TestHelpWindow(unittest.TestCase): 45 """Tests for HelpWindow text and keybind lists.""" 46 47 def setUp(self): 48 self.maxDiff = None # pylint: disable=invalid-name 49 50 def test_instantiate(self) -> None: 51 app = _create_app_mock() 52 help_window = HelpWindow(app) 53 self.assertIsNotNone(help_window) 54 55 # pylint: disable=unused-variable,unused-argument 56 def test_add_keybind_help_text(self) -> None: 57 key_bindings = KeyBindings() 58 59 @key_bindings.add('f1') 60 def show_help(event): 61 """Toggle help window.""" 62 63 @key_bindings.add('c-w') 64 @key_bindings.add('c-q') 65 def exit_(event): 66 """Quit the application.""" 67 68 app = _create_app_mock() 69 70 help_window = HelpWindow(app) 71 help_window.add_keybind_help_text('Global', key_bindings) 72 73 self.assertEqual( 74 help_window.help_text_sections, 75 { 76 'Global': { 77 'Quit the application.': ['Ctrl-Q', 'Ctrl-W'], 78 'Toggle help window.': ['F1'], 79 } 80 }, 81 ) 82 83 def test_generate_keybind_help_text(self) -> None: 84 """Test keybind list template generation.""" 85 global_bindings = KeyBindings() 86 87 @global_bindings.add('f1') 88 def show_help(event): 89 """Toggle help window.""" 90 91 @global_bindings.add('c-w') 92 @global_bindings.add('c-q') 93 def exit_(event): 94 """Quit the application.""" 95 96 focus_bindings = KeyBindings() 97 98 @focus_bindings.add('s-tab') 99 @focus_bindings.add('c-right') 100 @focus_bindings.add('c-down') 101 def app_focus_next(event): 102 """Move focus to the next widget.""" 103 104 @focus_bindings.add('c-left') 105 @focus_bindings.add('c-up') 106 def app_focus_previous(event): 107 """Move focus to the previous widget.""" 108 109 app = _create_app_mock() 110 111 help_window = HelpWindow( 112 app, 113 preamble='Pigweed CLI v0.1', 114 additional_help_text=inspect.cleandoc( 115 """ 116 Welcome to the Pigweed Console! 117 Please enjoy this extra help text. 118 """ 119 ), 120 ) 121 help_window.add_keybind_help_text('Global', global_bindings) 122 help_window.add_keybind_help_text('Focus', focus_bindings) 123 help_window.generate_keybind_help_text() 124 125 self.assertIn( 126 inspect.cleandoc( 127 """ 128 Welcome to the Pigweed Console! 129 Please enjoy this extra help text. 130 """ 131 ), 132 help_window.help_text, 133 ) 134 self.assertIn( 135 inspect.cleandoc( 136 """ 137 ==== Global Keys ==== 138 """ 139 ), 140 help_window.help_text, 141 ) 142 self.assertIn( 143 inspect.cleandoc( 144 """ 145 Toggle help window. ----------------- F1 146 Quit the application. --------------- Ctrl-Q 147 Ctrl-W 148 """ 149 ), 150 help_window.help_text, 151 ) 152 self.assertIn( 153 inspect.cleandoc( 154 """ 155 ==== Focus Keys ==== 156 """ 157 ), 158 help_window.help_text, 159 ) 160 self.assertIn( 161 inspect.cleandoc( 162 """ 163 Move focus to the next widget. ------ Ctrl-Down 164 Ctrl-Right 165 Shift-Tab 166 Move focus to the previous widget. -- Ctrl-Left 167 Ctrl-Up 168 """ 169 ), 170 help_window.help_text, 171 ) 172 173 174if __name__ == '__main__': 175 unittest.main() 176