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