• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 the pw_rpc.console_tools.functions module."""
15
16import unittest
17
18from pw_rpc.console_tools import functions
19
20
21def func(one, two: int, *a: bool, three=3, four: 'int' = 4, **kw) -> None:  # pylint: disable=unused-argument
22    """This is the docstring.
23
24    More stuff.
25    """
26
27
28_EXPECTED_HELP = """\
29func(one, two: int, *a: bool, three = 3, four: int = 4, **kw) -> None:
30
31    This is the docstring.
32
33    More stuff."""
34
35
36class TestFunctions(unittest.TestCase):
37    def test_format_no_args_function_help(self) -> None:
38        def simple_function():
39            pass
40
41        self.assertEqual(functions.format_function_help(simple_function),
42                         'simple_function():\n\n    (no docstring)')
43
44    def test_format_complex_function_help(self) -> None:
45        self.assertEqual(functions.format_function_help(func), _EXPECTED_HELP)
46
47    def test_help_as_repr_with_docstring_help(self) -> None:
48        wrapped = functions.help_as_repr(func)
49        self.assertEqual(repr(wrapped), _EXPECTED_HELP)
50
51    def test_help_as_repr_decorator(self) -> None:
52        @functions.help_as_repr
53        def no_docs():
54            pass
55
56        self.assertEqual(repr(no_docs), 'no_docs():\n\n    (no docstring)')
57
58    def test_help_as_repr_call_no_args(self) -> None:
59        self.assertEqual(functions.help_as_repr(lambda: 9876)(), 9876)
60
61    def test_help_as_repr_call_with_arg(self) -> None:
62        value = object()
63        self.assertIs(functions.help_as_repr(lambda arg: arg)(value), value)
64
65
66if __name__ == '__main__':
67    unittest.main()
68