Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 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 | |
Anthony DiGirolamo | 855b01d | 2021-06-18 17:11:56 -0700 | [diff] [blame] | 16 | import inspect |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 17 | import logging |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 18 | import unittest |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 19 | from unittest.mock import MagicMock |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 20 | |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 21 | from jinja2 import Environment, PackageLoader, make_logging_undefined |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 22 | from prompt_toolkit.key_binding import KeyBindings |
| 23 | |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 24 | from 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 | |
| 34 | def _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 |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class TestHelpWindow(unittest.TestCase): |
Anthony DiGirolamo | 8a49880 | 2021-06-14 23:52:42 -0700 | [diff] [blame] | 42 | """Tests for HelpWindow text and keybind lists.""" |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 43 | def setUp(self): |
| 44 | self.maxDiff = None # pylint: disable=invalid-name |
| 45 | |
| 46 | def test_instantiate(self) -> None: |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 47 | app = _create_app_mock() |
Anthony DiGirolamo | 4850d84 | 2021-06-14 12:52:36 -0700 | [diff] [blame] | 48 | help_window = HelpWindow(app) |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 49 | self.assertIsNotNone(help_window) |
| 50 | |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 51 | # pylint: disable=unused-variable,unused-argument |
| 52 | def test_add_keybind_help_text(self) -> None: |
| 53 | bindings = KeyBindings() |
| 54 | |
| 55 | @bindings.add('f1') |
| 56 | def show_help(event): |
| 57 | """Toggle help window.""" |
| 58 | |
| 59 | @bindings.add('c-w') |
| 60 | @bindings.add('c-q') |
| 61 | def exit_(event): |
| 62 | """Quit the application.""" |
| 63 | |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 64 | app = _create_app_mock() |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 65 | |
| 66 | help_window = HelpWindow(app) |
| 67 | help_window.add_keybind_help_text('Global', bindings) |
| 68 | |
| 69 | self.assertEqual( |
| 70 | help_window.help_text_sections, |
| 71 | { |
| 72 | 'Global': { |
Anthony DiGirolamo | c687480 | 2021-07-02 14:52:45 -0700 | [diff] [blame] | 73 | 'Quit the application.': ['Ctrl-Q', 'Ctrl-W'], |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 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 | |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 105 | app = _create_app_mock() |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 106 | |
Anthony DiGirolamo | fc1c716 | 2021-07-29 08:41:52 -0700 | [diff] [blame^] | 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 | ) |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 115 | help_window.add_keybind_help_text('Global', global_bindings) |
| 116 | help_window.add_keybind_help_text('Focus', focus_bindings) |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 117 | help_window.generate_help_text() |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 118 | |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 119 | self.assertIn( |
Anthony DiGirolamo | 855b01d | 2021-06-18 17:11:56 -0700 | [diff] [blame] | 120 | inspect.cleandoc(""" |
| 121 | Pigweed CLI v0.1 |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 122 | |
Anthony DiGirolamo | c687480 | 2021-07-02 14:52:45 -0700 | [diff] [blame] | 123 | ================================ Help =============================== |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 124 | |
Anthony DiGirolamo | 855b01d | 2021-06-18 17:11:56 -0700 | [diff] [blame] | 125 | Welcome to the Pigweed Console! |
| 126 | Please enjoy this extra help text. |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 127 | """), |
| 128 | help_window.help_text, |
| 129 | ) |
| 130 | self.assertIn( |
Anthony DiGirolamo | 855b01d | 2021-06-18 17:11:56 -0700 | [diff] [blame] | 131 | inspect.cleandoc(""" |
Anthony DiGirolamo | c687480 | 2021-07-02 14:52:45 -0700 | [diff] [blame] | 132 | ============================ Global Keys ============================ |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 133 | """), |
| 134 | help_window.help_text, |
| 135 | ) |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 136 | self.assertIn( |
Anthony DiGirolamo | 855b01d | 2021-06-18 17:11:56 -0700 | [diff] [blame] | 137 | inspect.cleandoc(""" |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 138 | Toggle help window. ----------------- F1 |
Anthony DiGirolamo | c687480 | 2021-07-02 14:52:45 -0700 | [diff] [blame] | 139 | Quit the application. --------------- Ctrl-Q, Ctrl-W |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 140 | """), |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 141 | help_window.help_text, |
| 142 | ) |
| 143 | self.assertIn( |
Anthony DiGirolamo | 855b01d | 2021-06-18 17:11:56 -0700 | [diff] [blame] | 144 | inspect.cleandoc(""" |
Anthony DiGirolamo | c687480 | 2021-07-02 14:52:45 -0700 | [diff] [blame] | 145 | ============================= Focus Keys ============================ |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 146 | """), |
| 147 | help_window.help_text, |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 148 | ) |
| 149 | self.assertIn( |
Anthony DiGirolamo | 855b01d | 2021-06-18 17:11:56 -0700 | [diff] [blame] | 150 | inspect.cleandoc(""" |
Anthony DiGirolamo | c687480 | 2021-07-02 14:52:45 -0700 | [diff] [blame] | 151 | Move focus to the next widget. ------ BackTab, Ctrl-Down, Ctrl-Right |
| 152 | Move focus to the previous widget. -- Ctrl-Left, Ctrl-Up |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 153 | """), |
Anthony DiGirolamo | f48d0b6 | 2021-06-15 12:47:31 -0700 | [diff] [blame] | 154 | help_window.help_text, |
Anthony DiGirolamo | af64b1e | 2021-06-14 12:46:49 -0700 | [diff] [blame] | 155 | ) |
| 156 | |
| 157 | |
| 158 | if __name__ == '__main__': |
| 159 | unittest.main() |