1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
# Copyright (C) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for fire docstrings module."""
from fire import docstrings
from fire import testutils
# pylint: disable=invalid-name
DocstringInfo = docstrings.DocstringInfo
ArgInfo = docstrings.ArgInfo
KwargInfo = docstrings.KwargInfo
# pylint: enable=invalid-name
class DocstringsTest(testutils.BaseTestCase):
def test_one_line_simple(self):
docstring = """A simple one line docstring."""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='A simple one line docstring.',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_one_line_simple_whitespace(self):
docstring = """
A simple one line docstring.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='A simple one line docstring.',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_one_line_too_long(self):
# pylint: disable=line-too-long
docstring = """A one line docstring that is both a little too verbose and a little too long so it keeps going well beyond a reasonable length for a one-liner.
"""
# pylint: enable=line-too-long
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='A one line docstring that is both a little too verbose and '
'a little too long so it keeps going well beyond a reasonable length '
'for a one-liner.',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_one_line_runs_over(self):
# pylint: disable=line-too-long
docstring = """A one line docstring that is both a little too verbose and a little too long
so it runs onto a second line.
"""
# pylint: enable=line-too-long
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='A one line docstring that is both a little too verbose and '
'a little too long so it runs onto a second line.',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_one_line_runs_over_whitespace(self):
docstring = """
A one line docstring that is both a little too verbose and a little too long
so it runs onto a second line.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='A one line docstring that is both a little too verbose and '
'a little too long so it runs onto a second line.',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_google_format_args_only(self):
docstring = """One line description.
Args:
arg1: arg1_description
arg2: arg2_description
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='One line description.',
args=[
ArgInfo(name='arg1', description='arg1_description'),
ArgInfo(name='arg2', description='arg2_description'),
]
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_google_format_arg_named_args(self):
docstring = """
Args:
args: arg_description
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
args=[
ArgInfo(name='args', description='arg_description'),
]
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_google_format_typed_args_and_returns(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans multiple lines, as
is allowed.
Args:
param1 (int): The first parameter.
param2 (str): The second parameter.
Returns:
bool: The return value. True for success, False otherwise.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'multiple lines, as\nis allowed.',
args=[
ArgInfo(name='param1', type='int',
description='The first parameter.'),
ArgInfo(name='param2', type='str',
description='The second parameter.'),
],
returns='bool: The return value. True for success, False otherwise.'
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_google_format_multiline_arg_description(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans multiple lines, as
is allowed.
Args:
param1 (int): The first parameter.
param2 (str): The second parameter. This has a lot of text, enough to
cover two lines.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'multiple lines, as\nis allowed.',
args=[
ArgInfo(name='param1', type='int',
description='The first parameter.'),
ArgInfo(name='param2', type='str',
description='The second parameter. This has a lot of text, '
'enough to cover two lines.'),
],
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_rst_format_typed_args_and_returns(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans across multiple
lines.
:param arg1: Description of arg1.
:type arg1: str.
:param arg2: Description of arg2.
:type arg2: bool.
:returns: int -- description of the return value.
:raises: AttributeError, KeyError
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'across multiple\nlines.',
args=[
ArgInfo(name='arg1', type='str',
description='Description of arg1.'),
ArgInfo(name='arg2', type='bool',
description='Description of arg2.'),
],
returns='int -- description of the return value.',
raises='AttributeError, KeyError',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_numpy_format_typed_args_and_returns(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans across multiple
lines.
Parameters
----------
param1 : int
The first parameter.
param2 : str
The second parameter.
Returns
-------
bool
True if successful, False otherwise.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'across multiple\nlines.',
args=[
ArgInfo(name='param1', type='int',
description='The first parameter.'),
ArgInfo(name='param2', type='str',
description='The second parameter.'),
],
# TODO(dbieber): Support return type.
returns='bool True if successful, False otherwise.',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_numpy_format_multiline_arg_description(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans across multiple
lines.
Parameters
----------
param1 : int
The first parameter.
param2 : str
The second parameter. This has a lot of text, enough to cover two
lines.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'across multiple\nlines.',
args=[
ArgInfo(name='param1', type='int',
description='The first parameter.'),
ArgInfo(name='param2', type='str',
description='The second parameter. This has a lot of text, '
'enough to cover two lines.'),
],
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_multisection_docstring(self):
docstring = """Docstring summary.
This is the first section of a docstring description.
This is the second section of a docstring description. This docstring
description has just two sections.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is the first section of a docstring description.'
'\n\n'
'This is the second section of a docstring description. This docstring'
'\n'
'description has just two sections.',
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_google_section_with_blank_first_line(self):
docstring = """Inspired by requests HTTPAdapter docstring.
:param x: Simple param.
Usage:
>>> import requests
"""
docstring_info = docstrings.parse(docstring)
self.assertEqual('Inspired by requests HTTPAdapter docstring.',
docstring_info.summary)
def test_ill_formed_docstring(self):
docstring = """Docstring summary.
args: raises ::
:
pathological docstrings should not fail, and ideally should behave
reasonably.
"""
docstrings.parse(docstring)
def test_strip_blank_lines(self):
lines = [' ', ' foo ', ' ']
expected_output = [' foo ']
self.assertEqual(expected_output, docstrings._strip_blank_lines(lines)) # pylint: disable=protected-access
def test_numpy_colon_in_description(self):
docstring = """
Greets name.
Arguments
---------
name : str
name, default : World
arg2 : int
arg2, default:None
arg3 : bool
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Greets name.',
description=None,
args=[
ArgInfo(name='name', type='str',
description='name, default : World'),
ArgInfo(name='arg2', type='int',
description='arg2, default:None'),
ArgInfo(name='arg3', type='bool', description=None),
]
)
self.assertEqual(expected_docstring_info, docstring_info)
def test_rst_format_typed_args_and_kwargs(self):
docstring = """Docstring summary.
:param arg1: Description of arg1.
:type arg1: str.
:key arg2: Description of arg2.
:type arg2: bool.
:key arg3: Description of arg3.
:type arg3: str.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
args=[
ArgInfo(name='arg1', type='str',
description='Description of arg1.'),
KwargInfo(name='arg2', type='bool',
description='Description of arg2.'),
KwargInfo(name='arg3', type='str',
description='Description of arg3.'),
],
)
self.assertEqual(expected_docstring_info, docstring_info)
if __name__ == '__main__':
testutils.main()
|