blob: 0df86950e09cf68f2b4eab5f813c02e3d8e59450 [file] [log] [blame]
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -07001# 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 DiGirolamo855b01d2021-06-18 17:11:56 -070016import inspect
Anthony DiGirolamofc1c7162021-07-29 08:41:52 -070017import logging
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070018import unittest
Anthony DiGirolamofc1c7162021-07-29 08:41:52 -070019from unittest.mock import MagicMock
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070020
Anthony DiGirolamofc1c7162021-07-29 08:41:52 -070021from jinja2 import Environment, PackageLoader, make_logging_undefined
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070022from prompt_toolkit.key_binding import KeyBindings
23
Anthony DiGirolamofc1c7162021-07-29 08:41:52 -070024from 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
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070039
40
41class TestHelpWindow(unittest.TestCase):
Anthony DiGirolamo8a498802021-06-14 23:52:42 -070042 """Tests for HelpWindow text and keybind lists."""
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070043 def setUp(self):
44 self.maxDiff = None # pylint: disable=invalid-name
45
46 def test_instantiate(self) -> None:
Anthony DiGirolamofc1c7162021-07-29 08:41:52 -070047 app = _create_app_mock()
Anthony DiGirolamo4850d842021-06-14 12:52:36 -070048 help_window = HelpWindow(app)
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070049 self.assertIsNotNone(help_window)
50
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070051 # 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 DiGirolamofc1c7162021-07-29 08:41:52 -070064 app = _create_app_mock()
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070065
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 DiGirolamoc6874802021-07-02 14:52:45 -070073 'Quit the application.': ['Ctrl-Q', 'Ctrl-W'],
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070074 '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 DiGirolamofc1c7162021-07-29 08:41:52 -0700105 app = _create_app_mock()
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700106
Anthony DiGirolamofc1c7162021-07-29 08:41:52 -0700107 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 DiGirolamoaf64b1e2021-06-14 12:46:49 -0700115 help_window.add_keybind_help_text('Global', global_bindings)
116 help_window.add_keybind_help_text('Focus', focus_bindings)
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700117 help_window.generate_help_text()
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700118
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700119 self.assertIn(
Anthony DiGirolamo855b01d2021-06-18 17:11:56 -0700120 inspect.cleandoc("""
121 Pigweed CLI v0.1
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700122
Anthony DiGirolamoc6874802021-07-02 14:52:45 -0700123 ================================ Help ===============================
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700124
Anthony DiGirolamo855b01d2021-06-18 17:11:56 -0700125 Welcome to the Pigweed Console!
126 Please enjoy this extra help text.
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700127 """),
128 help_window.help_text,
129 )
130 self.assertIn(
Anthony DiGirolamo855b01d2021-06-18 17:11:56 -0700131 inspect.cleandoc("""
Anthony DiGirolamoc6874802021-07-02 14:52:45 -0700132 ============================ Global Keys ============================
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700133 """),
134 help_window.help_text,
135 )
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700136 self.assertIn(
Anthony DiGirolamo855b01d2021-06-18 17:11:56 -0700137 inspect.cleandoc("""
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700138 Toggle help window. ----------------- F1
Anthony DiGirolamoc6874802021-07-02 14:52:45 -0700139 Quit the application. --------------- Ctrl-Q, Ctrl-W
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700140 """),
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700141 help_window.help_text,
142 )
143 self.assertIn(
Anthony DiGirolamo855b01d2021-06-18 17:11:56 -0700144 inspect.cleandoc("""
Anthony DiGirolamoc6874802021-07-02 14:52:45 -0700145 ============================= Focus Keys ============================
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700146 """),
147 help_window.help_text,
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700148 )
149 self.assertIn(
Anthony DiGirolamo855b01d2021-06-18 17:11:56 -0700150 inspect.cleandoc("""
Anthony DiGirolamoc6874802021-07-02 14:52:45 -0700151 Move focus to the next widget. ------ BackTab, Ctrl-Down, Ctrl-Right
152 Move focus to the previous widget. -- Ctrl-Left, Ctrl-Up
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700153 """),
Anthony DiGirolamof48d0b62021-06-15 12:47:31 -0700154 help_window.help_text,
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -0700155 )
156
157
158if __name__ == '__main__':
159 unittest.main()