blob: 55feb3d819fdc01243dd03311f0e7a3701afb879 [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
16import unittest
17from inspect import cleandoc
18from unittest.mock import Mock
19
20from prompt_toolkit.key_binding import KeyBindings
21
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070022from pw_console.help_window import HelpWindow, KEYBIND_TEMPLATE
23
24
25class TestHelpWindow(unittest.TestCase):
26 """Tests for ConsoleApp."""
27 def setUp(self):
28 self.maxDiff = None # pylint: disable=invalid-name
29
30 def test_instantiate(self) -> None:
Anthony DiGirolamo4850d842021-06-14 12:52:36 -070031 app = Mock()
32 help_window = HelpWindow(app)
Anthony DiGirolamoaf64b1e2021-06-14 12:46:49 -070033 self.assertIsNotNone(help_window)
34
35 def test_template_loads(self) -> None:
36 self.assertIn('{%', KEYBIND_TEMPLATE)
37
38 # pylint: disable=unused-variable,unused-argument
39 def test_add_keybind_help_text(self) -> None:
40 bindings = KeyBindings()
41
42 @bindings.add('f1')
43 def show_help(event):
44 """Toggle help window."""
45
46 @bindings.add('c-w')
47 @bindings.add('c-q')
48 def exit_(event):
49 """Quit the application."""
50
51 app = Mock()
52
53 help_window = HelpWindow(app)
54 help_window.add_keybind_help_text('Global', bindings)
55
56 self.assertEqual(
57 help_window.help_text_sections,
58 {
59 'Global': {
60 'Quit the application.': ['ControlQ', 'ControlW'],
61 'Toggle help window.': ['F1'],
62 }
63 },
64 )
65
66 def test_generate_help_text(self) -> None:
67 """Test keybind list template generation."""
68 global_bindings = KeyBindings()
69
70 @global_bindings.add('f1')
71 def show_help(event):
72 """Toggle help window."""
73
74 @global_bindings.add('c-w')
75 @global_bindings.add('c-q')
76 def exit_(event):
77 """Quit the application."""
78
79 focus_bindings = KeyBindings()
80
81 @focus_bindings.add('s-tab')
82 @focus_bindings.add('c-right')
83 @focus_bindings.add('c-down')
84 def app_focus_next(event):
85 """Move focus to the next widget."""
86
87 @focus_bindings.add('c-left')
88 @focus_bindings.add('c-up')
89 def app_focus_previous(event):
90 """Move focus to the previous widget."""
91
92 app = Mock()
93
94 help_window = HelpWindow(app)
95 help_window.add_keybind_help_text('Global', global_bindings)
96 help_window.add_keybind_help_text('Focus', focus_bindings)
97
98 help_text = help_window.generate_help_text()
99
100 self.assertIn(
101 cleandoc("""
102 Toggle help window. ----------------- F1
103 Quit the application. --------------- ControlQ, ControlW
104 """),
105 help_text,
106 )
107 self.assertIn(
108 cleandoc("""
109 Move focus to the next widget. ------ BackTab, ControlDown, ControlRight
110 Move focus to the previous widget. -- ControlLeft, ControlUp
111 """),
112 help_text,
113 )
114
115
116if __name__ == '__main__':
117 unittest.main()