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_jinja_env = Environment( 27 loader=PackageLoader('pw_console'), 28 undefined=make_logging_undefined(logger=logging.getLogger('pw_console')), 29 trim_blocks=True, 30 lstrip_blocks=True, 31) 32 33 34def _create_app_mock(): 35 template = _jinja_env.get_template('keybind_list.jinja') 36 mock_app = MagicMock() 37 mock_app.get_template = MagicMock(return_value=template) 38 return mock_app 39 40 41class TestHelpWindow(unittest.TestCase): 42 """Tests for HelpWindow text and keybind lists.""" 43 def setUp(self): 44 self.maxDiff = None # pylint: disable=invalid-name 45 46 def test_instantiate(self) -> None: 47 app = _create_app_mock() 48 help_window = HelpWindow(app) 49 self.assertIsNotNone(help_window) 50 51 # pylint: disable=unused-variable,unused-argument 52 def test_add_keybind_help_text(self) -> None: 53 key_bindings = KeyBindings() 54 55 @key_bindings.add('f1') 56 def show_help(event): 57 """Toggle help window.""" 58 59 @key_bindings.add('c-w') 60 @key_bindings.add('c-q') 61 def exit_(event): 62 """Quit the application.""" 63 64 app = _create_app_mock() 65 66 help_window = HelpWindow(app) 67 help_window.add_keybind_help_text('Global', key_bindings) 68 69 self.assertEqual( 70 help_window.help_text_sections, 71 { 72 'Global': { 73 'Quit the application.': ['Ctrl-Q', 'Ctrl-W'], 74 'Toggle help window.': ['F1'], 75 } 76 }, 77 ) 78 79 def test_generate_help_text(self) -> None: 80 """Test keybind list template generation.""" 81 global_bindings = KeyBindings() 82 83 @global_bindings.add('f1') 84 def show_help(event): 85 """Toggle help window.""" 86 87 @global_bindings.add('c-w') 88 @global_bindings.add('c-q') 89 def exit_(event): 90 """Quit the application.""" 91 92 focus_bindings = KeyBindings() 93 94 @focus_bindings.add('s-tab') 95 @focus_bindings.add('c-right') 96 @focus_bindings.add('c-down') 97 def app_focus_next(event): 98 """Move focus to the next widget.""" 99 100 @focus_bindings.add('c-left') 101 @focus_bindings.add('c-up') 102 def app_focus_previous(event): 103 """Move focus to the previous widget.""" 104 105 app = _create_app_mock() 106 107 help_window = HelpWindow( 108 app, 109 preamble='Pigweed CLI v0.1', 110 additional_help_text=inspect.cleandoc(""" 111 Welcome to the Pigweed Console! 112 Please enjoy this extra help text. 113 """), 114 ) 115 help_window.add_keybind_help_text('Global', global_bindings) 116 help_window.add_keybind_help_text('Focus', focus_bindings) 117 help_window.generate_help_text() 118 119 self.assertIn( 120 inspect.cleandoc(""" 121 Welcome to the Pigweed Console! 122 Please enjoy this extra help text. 123 """), 124 help_window.help_text, 125 ) 126 self.assertIn( 127 inspect.cleandoc(""" 128 ==== Global Keys ==== 129 """), 130 help_window.help_text, 131 ) 132 self.assertIn( 133 inspect.cleandoc(""" 134 Toggle help window. ----------------- F1 135 Quit the application. --------------- Ctrl-Q 136 Ctrl-W 137 """), 138 help_window.help_text, 139 ) 140 self.assertIn( 141 inspect.cleandoc(""" 142 ==== Focus Keys ==== 143 """), 144 help_window.help_text, 145 ) 146 self.assertIn( 147 inspect.cleandoc(""" 148 Move focus to the next widget. ------ Ctrl-Down 149 Ctrl-Right 150 Shift-Tab 151 Move focus to the previous widget. -- Ctrl-Left 152 Ctrl-Up 153 """), 154 help_window.help_text, 155 ) 156 157 158if __name__ == '__main__': 159 unittest.main() 160