| 1 |
"""Tests the C Domain""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import itertools |
| 6 |
import xml.etree.ElementTree as ET |
| 7 |
import zlib |
| 8 |
from typing import TYPE_CHECKING |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from sphinx import addnodes |
| 13 |
from sphinx.addnodes import ( |
| 14 |
desc, |
| 15 |
desc_content, |
| 16 |
desc_name, |
| 17 |
desc_parameter, |
| 18 |
desc_parameterlist, |
| 19 |
desc_sig_name, |
| 20 |
desc_sig_space, |
| 21 |
desc_signature, |
| 22 |
desc_signature_line, |
| 23 |
pending_xref, |
| 24 |
) |
| 25 |
from sphinx.domains.c._ids import _id_prefix, _macro_keywords, _max_id |
| 26 |
from sphinx.domains.c._parser import DefinitionParser |
| 27 |
from sphinx.domains.c._symbol import Symbol |
| 28 |
from sphinx.ext.intersphinx._load import load_mappings, validate_intersphinx_mapping |
| 29 |
from sphinx.testing import restructuredtext |
| 30 |
from sphinx.testing.util import assert_node |
| 31 |
from sphinx.util.cfamily import DefinitionError |
| 32 |
from sphinx.writers.text import STDINDENT |
| 33 |
|
| 34 |
from tests.utils import extract_node |
| 35 |
|
| 36 |
if TYPE_CHECKING: |
| 37 |
from io import StringIO |
| 38 |
|
| 39 |
|
| 40 |
class Config: |
| 41 |
c_id_attributes = ['id_attr', 'LIGHTGBM_C_EXPORT'] |
| 42 |
c_paren_attributes = ['paren_attr'] |
| 43 |
c_extra_keywords = _macro_keywords |
| 44 |
|
| 45 |
|
| 46 |
def parse(name, string): |
| 47 |
parser = DefinitionParser(string, location=None, config=Config()) |
| 48 |
parser.allowFallbackExpressionParsing = False |
| 49 |
ast = parser.parse_declaration(name, name) |
| 50 |
parser.assert_end() |
| 51 |
return ast |
| 52 |
|
| 53 |
|
| 54 |
def _check(name, input, id_dict, output, key, as_text_output): |
| 55 |
if key is None: |
| 56 |
key = name |
| 57 |
key += ' ' |
| 58 |
if name in {'function', 'member'}: |
| 59 |
input_actual = input |
| 60 |
output_ast = output |
| 61 |
output_as_text = output |
| 62 |
else: |
| 63 |
input_actual = input.format(key='') |
| 64 |
output_ast = output.format(key='') |
| 65 |
output_as_text = output.format(key=key) |
| 66 |
if as_text_output is not None: |
| 67 |
output_as_text = as_text_output |
| 68 |
|
| 69 |
# first a simple check of the AST |
| 70 |
ast = parse(name, input_actual) |
| 71 |
res = str(ast) |
| 72 |
if res != output_ast: |
| 73 |
print() |
| 74 |
print('Input: ', input) |
| 75 |
print('Result: ', res) |
| 76 |
print('Expected: ', output_ast) |
| 77 |
raise DefinitionError |
| 78 |
root_symbol = Symbol(None, None, None, None, None) |
| 79 |
symbol = root_symbol.add_declaration(ast, docname='TestDoc', line=42) |
| 80 |
parent_node = addnodes.desc() |
| 81 |
signode = addnodes.desc_signature(input, '') |
| 82 |
parent_node += signode |
| 83 |
ast.describe_signature(signode, 'lastIsName', symbol, options={}) |
| 84 |
res_as_text = parent_node.astext() |
| 85 |
if res_as_text != output_as_text: |
| 86 |
print() |
| 87 |
print('Input: ', input) |
| 88 |
print('astext(): ', res_as_text) |
| 89 |
print('Expected: ', output_as_text) |
| 90 |
raise DefinitionError |
| 91 |
|
| 92 |
id_expected = [None] |
| 93 |
for i in range(1, _max_id + 1): |
| 94 |
if i in id_dict: |
| 95 |
id_expected.append(id_dict[i]) |
| 96 |
else: |
| 97 |
id_expected.append(id_expected[i - 1]) |
| 98 |
id_actual = [None] |
| 99 |
for i in range(1, _max_id + 1): |
| 100 |
# try: |
| 101 |
id = ast.get_id(version=i) |
| 102 |
assert id is not None |
| 103 |
id_actual.append(id[len(_id_prefix[i]) :]) |
| 104 |
# except NoOldIdError: |
| 105 |
# id_actual.append(None) |
| 106 |
|
| 107 |
res = [True] |
| 108 |
for i in range(1, _max_id + 1): |
| 109 |
res.append(id_expected[i] == id_actual[i]) |
| 110 |
|
| 111 |
if not all(res): |
| 112 |
print('input: %s' % input.rjust(20)) |
| 113 |
for i in range(1, _max_id + 1): |
| 114 |
if res[i]: |
| 115 |
continue |
| 116 |
print('Error in id version %d.' % i) |
| 117 |
print('result: %s' % id_actual[i]) |
| 118 |
print('expected: %s' % id_expected[i]) |
| 119 |
# print(root_symbol.dump(0)) |
| 120 |
raise DefinitionError |
| 121 |
|
| 122 |
|
| 123 |
def check(name, input, id_dict, output=None, key=None, as_text_output=None): |
| 124 |
if output is None: |
| 125 |
output = input |
| 126 |
# First, check without semicolon |
| 127 |
_check(name, input, id_dict, output, key, as_text_output) |
| 128 |
if name != 'macro': |
| 129 |
# Second, check with semicolon |
| 130 |
_check( |
| 131 |
name, |
| 132 |
input + ' ;', |
| 133 |
id_dict, |
| 134 |
output + ';', |
| 135 |
key, |
| 136 |
as_text_output + ';' if as_text_output is not None else None, |
| 137 |
) |
| 138 |
|
| 139 |
|
| 140 |
def test_domain_c_ast_expressions() -> None: |
| 141 |
def expr_check(expr, output=None): |
| 142 |
parser = DefinitionParser(expr, location=None, config=Config()) |
| 143 |
parser.allowFallbackExpressionParsing = False |
| 144 |
ast = parser.parse_expression() |
| 145 |
parser.assert_end() |
| 146 |
# first a simple check of the AST |
| 147 |
if output is None: |
| 148 |
output = expr |
| 149 |
res = str(ast) |
| 150 |
if res != output: |
| 151 |
print() |
| 152 |
print('Input: ', input) |
| 153 |
print('Result: ', res) |
| 154 |
print('Expected: ', output) |
| 155 |
raise DefinitionError |
| 156 |
display_string = ast.get_display_string() |
| 157 |
if res != display_string: |
| 158 |
# note: if the expression contains an anon name then this will trigger a falsely |
| 159 |
print() |
| 160 |
print('Input: ', expr) |
| 161 |
print('Result: ', res) |
| 162 |
print('Display: ', display_string) |
| 163 |
raise DefinitionError |
| 164 |
|
| 165 |
# type expressions |
| 166 |
expr_check('int*') |
| 167 |
expr_check('int *const*') |
| 168 |
expr_check('int *volatile*') |
| 169 |
expr_check('int *restrict*') |
| 170 |
expr_check('int *(*)(double)') |
| 171 |
expr_check('const int*') |
| 172 |
expr_check('__int64') |
| 173 |
expr_check('unsigned __int64') |
| 174 |
|
| 175 |
# actual expressions |
| 176 |
|
| 177 |
# primary |
| 178 |
expr_check('true') |
| 179 |
expr_check('false') |
| 180 |
ints = [ |
| 181 |
'5', |
| 182 |
'0', |
| 183 |
'075', |
| 184 |
'0x0123456789ABCDEF', |
| 185 |
'0XF', |
| 186 |
'0b1', |
| 187 |
'0B1', |
| 188 |
"0b0'1'0", |
| 189 |
"00'1'2", |
| 190 |
"0x0'1'2", |
| 191 |
"1'2'3", |
| 192 |
] |
| 193 |
unsigned_suffix = ['', 'u', 'U'] |
| 194 |
long_suffix = ['', 'l', 'L', 'll', 'LL'] |
| 195 |
for i in ints: |
| 196 |
for u in unsigned_suffix: |
| 197 |
for l in long_suffix: |
| 198 |
expr = i + u + l |
| 199 |
expr_check(expr) |
| 200 |
expr = i + l + u |
| 201 |
expr_check(expr) |
| 202 |
for suffix in ('', 'f', 'F', 'l', 'L'): |
| 203 |
for e in ( |
| 204 |
'5e42', |
| 205 |
'5e+42', |
| 206 |
'5e-42', |
| 207 |
'5.', |
| 208 |
'5.e42', |
| 209 |
'5.e+42', |
| 210 |
'5.e-42', |
| 211 |
'.5', |
| 212 |
'.5e42', |
| 213 |
'.5e+42', |
| 214 |
'.5e-42', |
| 215 |
'5.0', |
| 216 |
'5.0e42', |
| 217 |
'5.0e+42', |
| 218 |
'5.0e-42', |
| 219 |
"1'2'3e7'8'9", |
| 220 |
"1'2'3.e7'8'9", |
| 221 |
".4'5'6e7'8'9", |
| 222 |
"1'2'3.4'5'6e7'8'9", |
| 223 |
): |
| 224 |
expr = e + suffix |
| 225 |
expr_check(expr) |
| 226 |
for e in ( |
| 227 |
'ApF', |
| 228 |
'Ap+F', |
| 229 |
'Ap-F', |
| 230 |
'A.', |
| 231 |
'A.pF', |
| 232 |
'A.p+F', |
| 233 |
'A.p-F', |
| 234 |
'.A', |
| 235 |
'.ApF', |
| 236 |
'.Ap+F', |
| 237 |
'.Ap-F', |
| 238 |
'A.B', |
| 239 |
'A.BpF', |
| 240 |
'A.Bp+F', |
| 241 |
'A.Bp-F', |
| 242 |
"A'B'Cp1'2'3", |
| 243 |
"A'B'C.p1'2'3", |
| 244 |
".D'E'Fp1'2'3", |
| 245 |
"A'B'C.D'E'Fp1'2'3", |
| 246 |
): |
| 247 |
expr = '0x' + e + suffix |
| 248 |
expr_check(expr) |
| 249 |
expr_check('"abc\\"cba"') # string |
| 250 |
# character literals |
| 251 |
for p in ('', 'u8', 'u', 'U', 'L'): |
| 252 |
expr_check(p + "'a'") |
| 253 |
expr_check(p + "'\\n'") |
| 254 |
expr_check(p + "'\\012'") |
| 255 |
expr_check(p + "'\\0'") |
| 256 |
expr_check(p + "'\\x0a'") |
| 257 |
expr_check(p + "'\\x0A'") |
| 258 |
expr_check(p + "'\\u0a42'") |
| 259 |
expr_check(p + "'\\u0A42'") |
| 260 |
expr_check(p + "'\\U0001f34c'") |
| 261 |
expr_check(p + "'\\U0001F34C'") |
| 262 |
|
| 263 |
expr_check('(5)') |
| 264 |
expr_check('C') |
| 265 |
# postfix |
| 266 |
expr_check('A(2)') |
| 267 |
expr_check('A[2]') |
| 268 |
expr_check('a.b.c') |
| 269 |
expr_check('a->b->c') |
| 270 |
expr_check('i++') |
| 271 |
expr_check('i--') |
| 272 |
# unary |
| 273 |
expr_check('++5') |
| 274 |
expr_check('--5') |
| 275 |
expr_check('*5') |
| 276 |
expr_check('&5') |
| 277 |
expr_check('+5') |
| 278 |
expr_check('-5') |
| 279 |
expr_check('!5') |
| 280 |
expr_check('not 5') |
| 281 |
expr_check('~5') |
| 282 |
expr_check('compl 5') |
| 283 |
expr_check('sizeof(T)') |
| 284 |
expr_check('sizeof -42') |
| 285 |
expr_check('alignof(T)') |
| 286 |
# cast |
| 287 |
expr_check('(int)2') |
| 288 |
# binary op |
| 289 |
expr_check('5 || 42') |
| 290 |
expr_check('5 or 42') |
| 291 |
expr_check('5 && 42') |
| 292 |
expr_check('5 and 42') |
| 293 |
expr_check('5 | 42') |
| 294 |
expr_check('5 bitor 42') |
| 295 |
expr_check('5 ^ 42') |
| 296 |
expr_check('5 xor 42') |
| 297 |
expr_check('5 & 42') |
| 298 |
expr_check('5 bitand 42') |
| 299 |
# ['==', '!='] |
| 300 |
expr_check('5 == 42') |
| 301 |
expr_check('5 != 42') |
| 302 |
expr_check('5 not_eq 42') |
| 303 |
# ['<=', '>=', '<', '>'] |
| 304 |
expr_check('5 <= 42') |
| 305 |
expr_check('5 >= 42') |
| 306 |
expr_check('5 < 42') |
| 307 |
expr_check('5 > 42') |
| 308 |
# ['<<', '>>'] |
| 309 |
expr_check('5 << 42') |
| 310 |
expr_check('5 >> 42') |
| 311 |
# ['+', '-'] |
| 312 |
expr_check('5 + 42') |
| 313 |
expr_check('5 - 42') |
| 314 |
# ['*', '/', '%'] |
| 315 |
expr_check('5 * 42') |
| 316 |
expr_check('5 / 42') |
| 317 |
expr_check('5 % 42') |
| 318 |
# ['.*', '->*'] |
| 319 |
expr_check('5 .* 42') |
| 320 |
expr_check('5 ->* 42') |
| 321 |
# TODO: conditional is unimplemented |
| 322 |
# conditional |
| 323 |
# expr_check('5 ? 7 : 3') |
| 324 |
# assignment |
| 325 |
expr_check('a = 5') |
| 326 |
expr_check('a *= 5') |
| 327 |
expr_check('a /= 5') |
| 328 |
expr_check('a %= 5') |
| 329 |
expr_check('a += 5') |
| 330 |
expr_check('a -= 5') |
| 331 |
expr_check('a >>= 5') |
| 332 |
expr_check('a <<= 5') |
| 333 |
expr_check('a &= 5') |
| 334 |
expr_check('a and_eq 5') |
| 335 |
expr_check('a ^= 5') |
| 336 |
expr_check('a xor_eq 5') |
| 337 |
expr_check('a |= 5') |
| 338 |
expr_check('a or_eq 5') |
| 339 |
|
| 340 |
|
| 341 |
def test_domain_c_ast_fundamental_types() -> None: |
| 342 |
def types(): |
| 343 |
def signed(t): |
| 344 |
yield t |
| 345 |
yield 'signed ' + t |
| 346 |
yield 'unsigned ' + t |
| 347 |
|
| 348 |
# integer types |
| 349 |
# ------------- |
| 350 |
yield 'void' |
| 351 |
yield from ('_Bool', 'bool') |
| 352 |
yield from signed('char') |
| 353 |
yield from signed('short') |
| 354 |
yield from signed('short int') |
| 355 |
yield from signed('int') |
| 356 |
yield from ('signed', 'unsigned') |
| 357 |
yield from signed('long') |
| 358 |
yield from signed('long int') |
| 359 |
yield from signed('long long') |
| 360 |
yield from signed('long long int') |
| 361 |
yield from ('__int128', '__uint128') |
| 362 |
# extensions |
| 363 |
for t in ('__int8', '__int16', '__int32', '__int64', '__int128'): |
| 364 |
yield from signed(t) |
| 365 |
|
| 366 |
# floating point types |
| 367 |
# -------------------- |
| 368 |
yield from ('_Decimal32', '_Decimal64', '_Decimal128') |
| 369 |
for f in ('float', 'double', 'long double'): |
| 370 |
yield f |
| 371 |
yield from (f + ' _Complex', f + ' complex') |
| 372 |
yield from ('_Complex ' + f, 'complex ' + f) |
| 373 |
yield from ('_Imaginary ' + f, 'imaginary ' + f) |
| 374 |
# extensions |
| 375 |
# https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html#Floating-Types |
| 376 |
yield from ( |
| 377 |
'__float80', |
| 378 |
'_Float64x', |
| 379 |
'__float128', |
| 380 |
'_Float128', |
| 381 |
'__ibm128', |
| 382 |
) |
| 383 |
# https://gcc.gnu.org/onlinedocs/gcc/Half-Precision.html#Half-Precision |
| 384 |
yield '__fp16' |
| 385 |
|
| 386 |
# fixed-point types (extension) |
| 387 |
# ----------------------------- |
| 388 |
# https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html#Fixed-Point |
| 389 |
for sat in ('', '_Sat '): |
| 390 |
for t in ('_Fract', 'fract', '_Accum', 'accum'): |
| 391 |
for size in ('short ', '', 'long ', 'long long '): |
| 392 |
for tt in signed(size + t): |
| 393 |
yield sat + tt |
| 394 |
|
| 395 |
for t in types(): |
| 396 |
input = '{key}%s foo' % t |
| 397 |
output = ' '.join(input.split()) |
| 398 |
check('type', input, {1: 'foo'}, key='typedef', output=output) |
| 399 |
if ' ' in t: |
| 400 |
# try permutations of all components |
| 401 |
tcs = t.split() |
| 402 |
for p in itertools.permutations(tcs): |
| 403 |
input = '{key}%s foo' % ' '.join(p) |
| 404 |
output = ' '.join(input.split()) |
| 405 |
check('type', input, {1: 'foo'}, key='typedef', output=output) |
| 406 |
|
| 407 |
|
| 408 |
def test_domain_c_ast_type_definitions() -> None: |
| 409 |
check('type', '{key}T', {1: 'T'}) |
| 410 |
|
| 411 |
check('type', '{key}bool *b', {1: 'b'}, key='typedef') |
| 412 |
check('type', '{key}bool *const b', {1: 'b'}, key='typedef') |
| 413 |
check('type', '{key}bool *const *b', {1: 'b'}, key='typedef') |
| 414 |
check('type', '{key}bool *volatile *b', {1: 'b'}, key='typedef') |
| 415 |
check('type', '{key}bool *restrict *b', {1: 'b'}, key='typedef') |
| 416 |
check('type', '{key}bool *volatile const b', {1: 'b'}, key='typedef') |
| 417 |
check('type', '{key}bool *volatile const b', {1: 'b'}, key='typedef') |
| 418 |
check('type', '{key}bool *volatile const *b', {1: 'b'}, key='typedef') |
| 419 |
check('type', '{key}bool b[]', {1: 'b'}, key='typedef') |
| 420 |
check('type', '{key}long long int foo', {1: 'foo'}, key='typedef') |
| 421 |
# test decl specs on right |
| 422 |
check('type', '{key}bool const b', {1: 'b'}, key='typedef') |
| 423 |
|
| 424 |
# from https://github.com/breathe-doc/breathe/issues/267 |
| 425 |
# (named function parameters for function pointers |
| 426 |
check( |
| 427 |
'type', |
| 428 |
'{key}void (*gpio_callback_t)(struct device *port, uint32_t pin)', |
| 429 |
{1: 'gpio_callback_t'}, |
| 430 |
key='typedef', |
| 431 |
) |
| 432 |
|
| 433 |
|
| 434 |
def test_domain_c_ast_macro_definitions() -> None: |
| 435 |
check('macro', 'M', {1: 'M'}) |
| 436 |
check('macro', 'M()', {1: 'M'}) |
| 437 |
check('macro', 'M(arg)', {1: 'M'}) |
| 438 |
check('macro', 'M(arg1, arg2)', {1: 'M'}) |
| 439 |
check('macro', 'M(arg1, arg2, arg3)', {1: 'M'}) |
| 440 |
check('macro', 'M(...)', {1: 'M'}) |
| 441 |
check('macro', 'M(arg, ...)', {1: 'M'}) |
| 442 |
check('macro', 'M(arg1, arg2, ...)', {1: 'M'}) |
| 443 |
check('macro', 'M(arg1, arg2, arg3, ...)', {1: 'M'}) |
| 444 |
# GNU extension |
| 445 |
check('macro', 'M(arg1, arg2, arg3...)', {1: 'M'}) |
| 446 |
with pytest.raises(DefinitionError): |
| 447 |
check('macro', 'M(arg1, arg2..., arg3)', {1: 'M'}) |
| 448 |
|
| 449 |
|
| 450 |
def test_domain_c_ast_member_definitions() -> None: |
| 451 |
check('member', 'void a', {1: 'a'}) |
| 452 |
check('member', '_Bool a', {1: 'a'}) |
| 453 |
check('member', 'bool a', {1: 'a'}) |
| 454 |
check('member', 'char a', {1: 'a'}) |
| 455 |
check('member', 'int a', {1: 'a'}) |
| 456 |
check('member', 'float a', {1: 'a'}) |
| 457 |
check('member', 'double a', {1: 'a'}) |
| 458 |
|
| 459 |
check('member', 'unsigned long a', {1: 'a'}) |
| 460 |
check('member', '__int64 a', {1: 'a'}) |
| 461 |
check('member', 'unsigned __int64 a', {1: 'a'}) |
| 462 |
|
| 463 |
check('member', 'int .a', {1: 'a'}) |
| 464 |
|
| 465 |
check('member', 'int *a', {1: 'a'}) |
| 466 |
check('member', 'int **a', {1: 'a'}) |
| 467 |
check('member', 'const int a', {1: 'a'}) |
| 468 |
check('member', 'volatile int a', {1: 'a'}) |
| 469 |
check('member', 'restrict int a', {1: 'a'}) |
| 470 |
check('member', 'volatile const int a', {1: 'a'}) |
| 471 |
check('member', 'restrict const int a', {1: 'a'}) |
| 472 |
check('member', 'restrict volatile int a', {1: 'a'}) |
| 473 |
check('member', 'restrict volatile const int a', {1: 'a'}) |
| 474 |
|
| 475 |
check('member', 'T t', {1: 't'}) |
| 476 |
|
| 477 |
check('member', 'int a[]', {1: 'a'}) |
| 478 |
|
| 479 |
check('member', 'int (*p)[]', {1: 'p'}) |
| 480 |
|
| 481 |
check('member', 'int a[42]', {1: 'a'}) |
| 482 |
check('member', 'int a = 42', {1: 'a'}) |
| 483 |
check('member', 'T a = {}', {1: 'a'}) |
| 484 |
check('member', 'T a = {1}', {1: 'a'}) |
| 485 |
check('member', 'T a = {1, 2}', {1: 'a'}) |
| 486 |
check('member', 'T a = {1, 2, 3}', {1: 'a'}) |
| 487 |
|
| 488 |
# test from issue https://github.com/sphinx-doc/sphinx/issues/1539 |
| 489 |
check('member', 'CK_UTF8CHAR model[16]', {1: 'model'}) |
| 490 |
|
| 491 |
check('member', 'auto int a', {1: 'a'}) |
| 492 |
check('member', 'register int a', {1: 'a'}) |
| 493 |
check('member', 'extern int a', {1: 'a'}) |
| 494 |
check('member', 'static int a', {1: 'a'}) |
| 495 |
|
| 496 |
check('member', 'thread_local int a', {1: 'a'}) |
| 497 |
check('member', '_Thread_local int a', {1: 'a'}) |
| 498 |
check('member', 'extern thread_local int a', {1: 'a'}) |
| 499 |
check('member', 'thread_local extern int a', {1: 'a'}, 'extern thread_local int a') |
| 500 |
check('member', 'static thread_local int a', {1: 'a'}) |
| 501 |
check('member', 'thread_local static int a', {1: 'a'}, 'static thread_local int a') |
| 502 |
|
| 503 |
check('member', 'int b : 3', {1: 'b'}) |
| 504 |
|
| 505 |
|
| 506 |
def test_domain_c_ast_function_definitions() -> None: |
| 507 |
check('function', 'void f()', {1: 'f'}) |
| 508 |
check('function', 'void f(int)', {1: 'f'}) |
| 509 |
check('function', 'void f(int i)', {1: 'f'}) |
| 510 |
check('function', 'void f(int i, int j)', {1: 'f'}) |
| 511 |
check('function', 'void f(...)', {1: 'f'}) |
| 512 |
check('function', 'void f(int i, ...)', {1: 'f'}) |
| 513 |
check('function', 'void f(struct T)', {1: 'f'}) |
| 514 |
check('function', 'void f(struct T t)', {1: 'f'}) |
| 515 |
check('function', 'void f(union T)', {1: 'f'}) |
| 516 |
check('function', 'void f(union T t)', {1: 'f'}) |
| 517 |
check('function', 'void f(enum T)', {1: 'f'}) |
| 518 |
check('function', 'void f(enum T t)', {1: 'f'}) |
| 519 |
|
| 520 |
# test from issue https://github.com/sphinx-doc/sphinx/issues/1539 |
| 521 |
check('function', 'void f(A x[])', {1: 'f'}) |
| 522 |
|
| 523 |
# test from issue https://github.com/sphinx-doc/sphinx/issues/2377 |
| 524 |
check('function', 'void (*signal(int sig, void (*func)(int)))(int)', {1: 'signal'}) |
| 525 |
|
| 526 |
check('function', 'extern void f()', {1: 'f'}) |
| 527 |
check('function', 'static void f()', {1: 'f'}) |
| 528 |
check('function', 'inline void f()', {1: 'f'}) |
| 529 |
|
| 530 |
# tests derived from https://github.com/sphinx-doc/sphinx/issues/1753 |
| 531 |
# (skip to keep sanity) |
| 532 |
check('function', 'void f(float *q(double))', {1: 'f'}) |
| 533 |
check('function', 'void f(float *(*q)(double))', {1: 'f'}) |
| 534 |
check('function', 'void f(float (*q)(double))', {1: 'f'}) |
| 535 |
check('function', 'int (*f(double d))(float)', {1: 'f'}) |
| 536 |
check('function', 'int (*f(bool b))[5]', {1: 'f'}) |
| 537 |
check('function', 'void f(int *const p)', {1: 'f'}) |
| 538 |
check('function', 'void f(int *volatile const p)', {1: 'f'}) |
| 539 |
|
| 540 |
# from https://github.com/breathe-doc/breathe/issues/223 |
| 541 |
check('function', 'void f(struct E e)', {1: 'f'}) |
| 542 |
check('function', 'void f(enum E e)', {1: 'f'}) |
| 543 |
check('function', 'void f(union E e)', {1: 'f'}) |
| 544 |
|
| 545 |
# array declarators |
| 546 |
check('function', 'void f(int arr[])', {1: 'f'}) |
| 547 |
check('function', 'void f(int arr[*])', {1: 'f'}) |
| 548 |
cvrs = ['', 'const', 'volatile', 'restrict', 'restrict volatile const'] |
| 549 |
for cvr in cvrs: |
| 550 |
space = ' ' if len(cvr) != 0 else '' |
| 551 |
check('function', f'void f(int arr[{cvr}*])', {1: 'f'}) |
| 552 |
check('function', f'void f(int arr[{cvr}])', {1: 'f'}) |
| 553 |
check('function', f'void f(int arr[{cvr}{space}42])', {1: 'f'}) |
| 554 |
check('function', f'void f(int arr[static{space}{cvr} 42])', {1: 'f'}) |
| 555 |
check( |
| 556 |
'function', |
| 557 |
f'void f(int arr[{cvr}{space}static 42])', |
| 558 |
{1: 'f'}, |
| 559 |
output=f'void f(int arr[static{space}{cvr} 42])', |
| 560 |
) |
| 561 |
check( |
| 562 |
'function', |
| 563 |
'void f(int arr[const static volatile 42])', |
| 564 |
{1: 'f'}, |
| 565 |
output='void f(int arr[static volatile const 42])', |
| 566 |
) |
| 567 |
|
| 568 |
with pytest.raises(DefinitionError): |
| 569 |
parse('function', 'void f(int for)') |
| 570 |
|
| 571 |
# from https://github.com/sphinx-doc/sphinx/issues/8960 |
| 572 |
check('function', 'void f(void (*p)(int, double), int i)', {1: 'f'}) |
| 573 |
|
| 574 |
|
| 575 |
def test_domain_c_ast_nested_name() -> None: |
| 576 |
check('struct', '{key}.A', {1: 'A'}) |
| 577 |
check('struct', '{key}.A.B', {1: 'A.B'}) |
| 578 |
check('function', 'void f(.A a)', {1: 'f'}) |
| 579 |
check('function', 'void f(.A.B a)', {1: 'f'}) |
| 580 |
|
| 581 |
|
| 582 |
def test_domain_c_ast_struct_definitions() -> None: |
| 583 |
check('struct', '{key}A', {1: 'A'}) |
| 584 |
|
| 585 |
|
| 586 |
def test_domain_c_ast_union_definitions() -> None: |
| 587 |
check('union', '{key}A', {1: 'A'}) |
| 588 |
|
| 589 |
|
| 590 |
def test_domain_c_ast_enum_definitions() -> None: |
| 591 |
check('enum', '{key}A', {1: 'A'}) |
| 592 |
|
| 593 |
check('enumerator', '{key}A', {1: 'A'}) |
| 594 |
check('enumerator', '{key}A = 42', {1: 'A'}) |
| 595 |
|
| 596 |
|
| 597 |
def test_domain_c_ast_anon_definitions() -> None: |
| 598 |
check('struct', '@a', {1: '@a'}, as_text_output='struct [anonymous]') |
| 599 |
check('union', '@a', {1: '@a'}, as_text_output='union [anonymous]') |
| 600 |
check('enum', '@a', {1: '@a'}, as_text_output='enum [anonymous]') |
| 601 |
check('struct', '@1', {1: '@1'}, as_text_output='struct [anonymous]') |
| 602 |
check('struct', '@a.A', {1: '@a.A'}, as_text_output='struct [anonymous].A') |
| 603 |
|
| 604 |
|
| 605 |
def test_domain_c_ast_initializers() -> None: |
| 606 |
ids_member = {1: 'v'} |
| 607 |
ids_function = {1: 'f'} |
| 608 |
# no init |
| 609 |
check('member', 'T v', ids_member) |
| 610 |
check('function', 'void f(T v)', ids_function) |
| 611 |
# with '=', assignment-expression |
| 612 |
check('member', 'T v = 42', ids_member) |
| 613 |
check('function', 'void f(T v = 42)', ids_function) |
| 614 |
# with '=', braced-init |
| 615 |
check('member', 'T v = {}', ids_member) |
| 616 |
check('function', 'void f(T v = {})', ids_function) |
| 617 |
check('member', 'T v = {42, 42, 42}', ids_member) |
| 618 |
check('function', 'void f(T v = {42, 42, 42})', ids_function) |
| 619 |
check('member', 'T v = {42, 42, 42,}', ids_member) |
| 620 |
check('function', 'void f(T v = {42, 42, 42,})', ids_function) |
| 621 |
# TODO: designator-list |
| 622 |
|
| 623 |
|
| 624 |
def test_domain_c_ast_attributes() -> None: |
| 625 |
# style: C++ |
| 626 |
check('member', '[[]] int f', {1: 'f'}) |
| 627 |
check( |
| 628 |
'member', |
| 629 |
'[ [ ] ] int f', |
| 630 |
{1: 'f'}, |
| 631 |
# this will fail when the proper grammar is implemented |
| 632 |
output='[[ ]] int f', |
| 633 |
) |
| 634 |
check('member', '[[a]] int f', {1: 'f'}) |
| 635 |
# style: GNU |
| 636 |
check('member', '__attribute__(()) int f', {1: 'f'}) |
| 637 |
check('member', '__attribute__((a)) int f', {1: 'f'}) |
| 638 |
check('member', '__attribute__((a, b)) int f', {1: 'f'}) |
| 639 |
check('member', '__attribute__((optimize(3))) int f', {1: 'f'}) |
| 640 |
check('member', '__attribute__((format(printf, 1, 2))) int f', {1: 'f'}) |
| 641 |
# style: user-defined id |
| 642 |
check('member', 'id_attr int f', {1: 'f'}) |
| 643 |
# style: user-defined paren |
| 644 |
check('member', 'paren_attr() int f', {1: 'f'}) |
| 645 |
check('member', 'paren_attr(a) int f', {1: 'f'}) |
| 646 |
check('member', 'paren_attr("") int f', {1: 'f'}) |
| 647 |
check('member', 'paren_attr(()[{}][]{}) int f', {1: 'f'}) |
| 648 |
with pytest.raises(DefinitionError): |
| 649 |
parse('member', 'paren_attr(() int f') |
| 650 |
with pytest.raises(DefinitionError): |
| 651 |
parse('member', 'paren_attr([) int f') |
| 652 |
with pytest.raises(DefinitionError): |
| 653 |
parse('member', 'paren_attr({) int f') |
| 654 |
with pytest.raises(DefinitionError): |
| 655 |
parse('member', 'paren_attr([)]) int f') |
| 656 |
with pytest.raises(DefinitionError): |
| 657 |
parse('member', 'paren_attr((])) int f') |
| 658 |
with pytest.raises(DefinitionError): |
| 659 |
parse('member', 'paren_attr({]}) int f') |
| 660 |
|
| 661 |
# position: decl specs |
| 662 |
check( |
| 663 |
'function', |
| 664 |
'static inline __attribute__(()) void f()', |
| 665 |
{1: 'f'}, |
| 666 |
output='__attribute__(()) static inline void f()', |
| 667 |
) |
| 668 |
check('function', '[[attr1]] [[attr2]] void f()', {1: 'f'}) |
| 669 |
# position: declarator |
| 670 |
check('member', 'int *[[attr1]] [[attr2]] i', {1: 'i'}) |
| 671 |
check( |
| 672 |
'member', |
| 673 |
'int *const [[attr1]] [[attr2]] volatile i', |
| 674 |
{1: 'i'}, |
| 675 |
output='int *[[attr1]] [[attr2]] volatile const i', |
| 676 |
) |
| 677 |
check('member', 'int *[[attr1]] [[attr2]] *i', {1: 'i'}) |
| 678 |
# position: parameters |
| 679 |
check('function', 'void f() [[attr1]] [[attr2]]', {1: 'f'}) |
| 680 |
|
| 681 |
# position: enumerator |
| 682 |
check('enumerator', '{key}Foo [[attr1]] [[attr2]]', {1: 'Foo'}) |
| 683 |
check('enumerator', '{key}Foo [[attr1]] [[attr2]] = 42', {1: 'Foo'}) |
| 684 |
|
| 685 |
# issue https://github.com/breathe-doc/breathe/issues/500 |
| 686 |
check( |
| 687 |
'function', |
| 688 |
'LIGHTGBM_C_EXPORT int LGBM_BoosterFree(int handle)', |
| 689 |
{1: 'LGBM_BoosterFree'}, |
| 690 |
) |
| 691 |
|
| 692 |
|
| 693 |
def test_extra_keywords() -> None: |
| 694 |
with pytest.raises(DefinitionError, match='Expected identifier in nested name'): |
| 695 |
parse('function', 'void complex(void)') |
| 696 |
|
| 697 |
|
| 698 |
# def test_print() -> None: |
| 699 |
# # used for getting all the ids out for checking |
| 700 |
# for a in ids: |
| 701 |
# print(a) |
| 702 |
# raise DefinitionError |
| 703 |
|
| 704 |
|
| 705 |
def split_warnings(warning: StringIO) -> list[str]: |
| 706 |
ws = warning.getvalue().split('\n') |
| 707 |
assert len(ws) >= 1 |
| 708 |
assert ws[-1] == '' |
| 709 |
return ws[:-1] |
| 710 |
|
| 711 |
|
| 712 |
def filter_warnings(warning: StringIO, file: str) -> list[str]: |
| 713 |
lines = split_warnings(warning) |
| 714 |
res = [ |
| 715 |
l |
| 716 |
for l in lines |
| 717 |
if 'domain-c' in l |
| 718 |
and f'{file}.rst' in l |
| 719 |
and "WARNING: document isn't included in any toctree" not in l |
| 720 |
] |
| 721 |
print(f"Filtered warnings for file '{file}':") |
| 722 |
for w in res: |
| 723 |
print(w) |
| 724 |
return res |
| 725 |
|
| 726 |
|
| 727 |
def extract_role_links(app, filename): |
| 728 |
t = (app.outdir / filename).read_text(encoding='utf8') |
| 729 |
lis = [l for l in t.split('\n') if l.startswith('<li')] |
| 730 |
entries = [] |
| 731 |
for l in lis: |
| 732 |
li = ET.fromstring(l) # NoQA: S314 # using known data in tests |
| 733 |
a_list = list(li.iter('a')) |
| 734 |
assert len(a_list) == 1 |
| 735 |
a = a_list[0] |
| 736 |
target = a.attrib['href'].lstrip('#') |
| 737 |
title = a.attrib['title'] |
| 738 |
assert len(a) == 1 |
| 739 |
code = a[0] |
| 740 |
assert code.tag == 'code' |
| 741 |
text = ''.join(code.itertext()) |
| 742 |
entries.append((target, title, text)) |
| 743 |
return entries |
| 744 |
|
| 745 |
|
| 746 |
@pytest.mark.sphinx('html', testroot='domain-c', confoverrides={'nitpicky': True}) |
| 747 |
def test_domain_c_build(app): |
| 748 |
app.build(force_all=True) |
| 749 |
ws = filter_warnings(app.warning, 'index') |
| 750 |
assert len(ws) == 0 |
| 751 |
|
| 752 |
|
| 753 |
@pytest.mark.sphinx('html', testroot='domain-c', confoverrides={'nitpicky': True}) |
| 754 |
def test_domain_c_build_namespace(app): |
| 755 |
app.build(force_all=True) |
| 756 |
ws = filter_warnings(app.warning, 'namespace') |
| 757 |
assert len(ws) == 0 |
| 758 |
t = (app.outdir / 'namespace.html').read_text(encoding='utf8') |
| 759 |
for id_ in ('NS.NSVar', 'NULLVar', 'ZeroVar', 'NS2.NS3.NS2NS3Var', 'PopVar'): |
| 760 |
assert f'id="c.{id_}"' in t |
| 761 |
|
| 762 |
|
| 763 |
@pytest.mark.sphinx('html', testroot='domain-c', confoverrides={'nitpicky': True}) |
| 764 |
def test_domain_c_build_anon_dup_decl(app): |
| 765 |
app.build(force_all=True) |
| 766 |
ws = filter_warnings(app.warning, 'anon-dup-decl') |
| 767 |
assert len(ws) == 2 |
| 768 |
assert 'WARNING: c:identifier reference target not found: @a' in ws[0] |
| 769 |
assert 'WARNING: c:identifier reference target not found: @b' in ws[1] |
| 770 |
|
| 771 |
|
| 772 |
@pytest.mark.sphinx('html', testroot='_blank', confoverrides={'nitpicky': True}) |
| 773 |
def test_domain_c_build_semicolon(app): |
| 774 |
text = """ |
| 775 |
.. c:member:: int member; |
| 776 |
.. c:var:: int var; |
| 777 |
.. c:function:: void f(); |
| 778 |
.. .. c:macro:: NO_SEMICOLON; |
| 779 |
.. c:struct:: Struct; |
| 780 |
.. c:union:: Union; |
| 781 |
.. c:enum:: Enum; |
| 782 |
.. c:enumerator:: Enumerator; |
| 783 |
.. c:type:: Type; |
| 784 |
.. c:type:: int TypeDef; |
| 785 |
""" |
| 786 |
restructuredtext.parse(app, text) |
| 787 |
ws = split_warnings(app.warning) |
| 788 |
assert len(ws) == 0 |
| 789 |
|
| 790 |
|
| 791 |
@pytest.mark.sphinx('html', testroot='domain-c', confoverrides={'nitpicky': True}) |
| 792 |
def test_domain_c_build_function_param_target(app): |
| 793 |
# the anchor for function parameters should be the function |
| 794 |
app.build(force_all=True) |
| 795 |
ws = filter_warnings(app.warning, 'function_param_target') |
| 796 |
assert len(ws) == 0 |
| 797 |
entries = extract_role_links(app, 'function_param_target.html') |
| 798 |
assert entries == [ |
| 799 |
('c.function_param_target.f', 'i', 'i'), |
| 800 |
('c.function_param_target.f', 'f.i', 'f.i'), |
| 801 |
] |
| 802 |
|
| 803 |
|
| 804 |
@pytest.mark.sphinx('html', testroot='domain-c', confoverrides={'nitpicky': True}) |
| 805 |
def test_domain_c_build_ns_lookup(app): |
| 806 |
app.build(force_all=True) |
| 807 |
ws = filter_warnings(app.warning, 'ns_lookup') |
| 808 |
assert len(ws) == 0 |
| 809 |
|
| 810 |
|
| 811 |
@pytest.mark.sphinx('html', testroot='domain-c', confoverrides={'nitpicky': True}) |
| 812 |
def test_domain_c_build_field_role(app): |
| 813 |
app.build(force_all=True) |
| 814 |
ws = filter_warnings(app.warning, 'field-role') |
| 815 |
assert len(ws) == 0 |
| 816 |
|
| 817 |
|
| 818 |
def _get_obj(app, query_name): |
| 819 |
domain = app.env.domains.c_domain |
| 820 |
for name, _dispname, object_type, docname, anchor, _prio in domain.get_objects(): |
| 821 |
if name == query_name: |
| 822 |
return docname, anchor, object_type |
| 823 |
return query_name, 'not', 'found' |
| 824 |
|
| 825 |
|
| 826 |
@pytest.mark.sphinx( |
| 827 |
'html', testroot='domain-c-intersphinx', confoverrides={'nitpicky': True} |
| 828 |
) |
| 829 |
def test_domain_c_build_intersphinx(tmp_path, app): |
| 830 |
# a splitting of test_ids_vs_tags0 into the primary directives in a remote project, |
| 831 |
# and then the references in the test project |
| 832 |
orig_source = """\ |
| 833 |
.. c:member:: int _member |
| 834 |
.. c:var:: int _var |
| 835 |
.. c:function:: void _function() |
| 836 |
.. c:macro:: _macro |
| 837 |
.. c:struct:: _struct |
| 838 |
.. c:union:: _union |
| 839 |
.. c:enum:: _enum |
| 840 |
|
| 841 |
.. c:enumerator:: _enumerator |
| 842 |
|
| 843 |
.. c:type:: _type |
| 844 |
.. c:function:: void _functionParam(int param) |
| 845 |
""" |
| 846 |
inv_file = tmp_path / 'inventory' |
| 847 |
inv_file.write_bytes( |
| 848 |
b"""\ |
| 849 |
# Sphinx inventory version 2 |
| 850 |
# Project: C Intersphinx Test |
| 851 |
# Version: |
| 852 |
# The remainder of this file is compressed using zlib. |
| 853 |
""" |
| 854 |
+ zlib.compress(b"""\ |
| 855 |
_enum c:enum 1 index.html#c.$ - |
| 856 |
_enum._enumerator c:enumerator 1 index.html#c.$ - |
| 857 |
_enumerator c:enumerator 1 index.html#c._enum.$ - |
| 858 |
_function c:function 1 index.html#c.$ - |
| 859 |
_functionParam c:function 1 index.html#c.$ - |
| 860 |
_functionParam.param c:functionParam 1 index.html#c._functionParam - |
| 861 |
_macro c:macro 1 index.html#c.$ - |
| 862 |
_member c:member 1 index.html#c.$ - |
| 863 |
_struct c:struct 1 index.html#c.$ - |
| 864 |
_type c:type 1 index.html#c.$ - |
| 865 |
_union c:union 1 index.html#c.$ - |
| 866 |
_var c:member 1 index.html#c.$ - |
| 867 |
""") |
| 868 |
) |
| 869 |
app.config.intersphinx_mapping = { |
| 870 |
'local': ('https://localhost/intersphinx/c/', str(inv_file)), |
| 871 |
} |
| 872 |
app.config.intersphinx_cache_limit = 0 |
| 873 |
# load the inventory and check if it's done correctly |
| 874 |
validate_intersphinx_mapping(app, app.config) |
| 875 |
load_mappings(app) |
| 876 |
|
| 877 |
app.build(force_all=True) |
| 878 |
ws = filter_warnings(app.warning, 'index') |
| 879 |
assert len(ws) == 0 |
| 880 |
|
| 881 |
|
| 882 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 883 |
def test_domain_c_parse_cfunction(app): |
| 884 |
text = ( |
| 885 |
'.. c:function:: PyObject* ' |
| 886 |
'PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)' |
| 887 |
) |
| 888 |
doctree = restructuredtext.parse(app, text) |
| 889 |
assert_node( |
| 890 |
doctree[1], |
| 891 |
addnodes.desc, |
| 892 |
desctype='function', |
| 893 |
domain='c', |
| 894 |
objtype='function', |
| 895 |
no_index=False, |
| 896 |
) |
| 897 |
|
| 898 |
entry = _get_obj(app, 'PyType_GenericAlloc') |
| 899 |
assert entry == ('index', 'c.PyType_GenericAlloc', 'function') |
| 900 |
|
| 901 |
|
| 902 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 903 |
def test_domain_c_parse_cmember(app): |
| 904 |
text = '.. c:member:: PyObject* PyTypeObject.tp_bases' |
| 905 |
doctree = restructuredtext.parse(app, text) |
| 906 |
assert_node( |
| 907 |
doctree[1], |
| 908 |
addnodes.desc, |
| 909 |
desctype='member', |
| 910 |
domain='c', |
| 911 |
objtype='member', |
| 912 |
no_index=False, |
| 913 |
) |
| 914 |
|
| 915 |
entry = _get_obj(app, 'PyTypeObject.tp_bases') |
| 916 |
assert entry == ('index', 'c.PyTypeObject.tp_bases', 'member') |
| 917 |
|
| 918 |
|
| 919 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 920 |
def test_domain_c_parse_cvar(app): |
| 921 |
text = '.. c:var:: PyObject* PyClass_Type' |
| 922 |
doctree = restructuredtext.parse(app, text) |
| 923 |
assert_node( |
| 924 |
doctree[1], |
| 925 |
addnodes.desc, |
| 926 |
desctype='var', |
| 927 |
domain='c', |
| 928 |
objtype='var', |
| 929 |
no_index=False, |
| 930 |
) |
| 931 |
|
| 932 |
entry = _get_obj(app, 'PyClass_Type') |
| 933 |
assert entry == ('index', 'c.PyClass_Type', 'member') |
| 934 |
|
| 935 |
|
| 936 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 937 |
def test_domain_c_parse_no_index_entry(app): |
| 938 |
text = '.. c:function:: void f()\n.. c:function:: void g()\n :no-index-entry:\n' |
| 939 |
doctree = restructuredtext.parse(app, text) |
| 940 |
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc)) |
| 941 |
assert_node( |
| 942 |
doctree[0], |
| 943 |
addnodes.index, |
| 944 |
entries=[('single', 'f (C function)', 'c.f', '', None)], |
| 945 |
) |
| 946 |
assert_node(doctree[2], addnodes.index, entries=[]) |
| 947 |
|
| 948 |
|
| 949 |
@pytest.mark.sphinx( |
| 950 |
'html', |
| 951 |
testroot='_blank', |
| 952 |
confoverrides={ |
| 953 |
'c_maximum_signature_line_length': len('str hello(str name)'), |
| 954 |
}, |
| 955 |
) |
| 956 |
def test_cfunction_signature_with_c_maximum_signature_line_length_equal(app): |
| 957 |
text = '.. c:function:: str hello(str name)' |
| 958 |
doctree = restructuredtext.parse(app, text) |
| 959 |
assert_node( |
| 960 |
doctree, |
| 961 |
( |
| 962 |
addnodes.index, |
| 963 |
[ |
| 964 |
desc, |
| 965 |
( |
| 966 |
[ |
| 967 |
desc_signature, |
| 968 |
( |
| 969 |
[ |
| 970 |
desc_signature_line, |
| 971 |
( |
| 972 |
pending_xref, |
| 973 |
desc_sig_space, |
| 974 |
[desc_name, [desc_sig_name, 'hello']], |
| 975 |
desc_parameterlist, |
| 976 |
), |
| 977 |
], |
| 978 |
), |
| 979 |
], |
| 980 |
desc_content, |
| 981 |
), |
| 982 |
], |
| 983 |
), |
| 984 |
) |
| 985 |
assert_node( |
| 986 |
doctree[1], |
| 987 |
addnodes.desc, |
| 988 |
desctype='function', |
| 989 |
domain='c', |
| 990 |
objtype='function', |
| 991 |
no_index=False, |
| 992 |
) |
| 993 |
assert_node( |
| 994 |
extract_node(doctree, 1, 0, 0, 3), |
| 995 |
[ |
| 996 |
desc_parameterlist, |
| 997 |
desc_parameter, |
| 998 |
( |
| 999 |
[pending_xref, [desc_sig_name, 'str']], |
| 1000 |
desc_sig_space, |
| 1001 |
[desc_sig_name, 'name'], |
| 1002 |
), |
| 1003 |
], |
| 1004 |
) |
| 1005 |
assert_node( |
| 1006 |
extract_node(doctree, 1, 0, 0, 3), |
| 1007 |
desc_parameterlist, |
| 1008 |
multi_line_parameter_list=False, |
| 1009 |
) |
| 1010 |
|
| 1011 |
|
| 1012 |
@pytest.mark.sphinx( |
| 1013 |
'html', |
| 1014 |
testroot='_blank', |
| 1015 |
confoverrides={ |
| 1016 |
'c_maximum_signature_line_length': len('str hello(str name)'), |
| 1017 |
}, |
| 1018 |
) |
| 1019 |
def test_cfunction_signature_with_c_maximum_signature_line_length_force_single(app): |
| 1020 |
text = '.. c:function:: str hello(str names)\n :single-line-parameter-list:' |
| 1021 |
doctree = restructuredtext.parse(app, text) |
| 1022 |
assert_node( |
| 1023 |
doctree, |
| 1024 |
( |
| 1025 |
addnodes.index, |
| 1026 |
[ |
| 1027 |
desc, |
| 1028 |
( |
| 1029 |
[ |
| 1030 |
desc_signature, |
| 1031 |
( |
| 1032 |
[ |
| 1033 |
desc_signature_line, |
| 1034 |
( |
| 1035 |
pending_xref, |
| 1036 |
desc_sig_space, |
| 1037 |
[desc_name, [desc_sig_name, 'hello']], |
| 1038 |
desc_parameterlist, |
| 1039 |
), |
| 1040 |
], |
| 1041 |
), |
| 1042 |
], |
| 1043 |
desc_content, |
| 1044 |
), |
| 1045 |
], |
| 1046 |
), |
| 1047 |
) |
| 1048 |
assert_node( |
| 1049 |
doctree[1], |
| 1050 |
addnodes.desc, |
| 1051 |
desctype='function', |
| 1052 |
domain='c', |
| 1053 |
objtype='function', |
| 1054 |
no_index=False, |
| 1055 |
) |
| 1056 |
assert_node( |
| 1057 |
extract_node(doctree, 1, 0, 0, 3), |
| 1058 |
[ |
| 1059 |
desc_parameterlist, |
| 1060 |
desc_parameter, |
| 1061 |
( |
| 1062 |
[pending_xref, [desc_sig_name, 'str']], |
| 1063 |
desc_sig_space, |
| 1064 |
[desc_sig_name, 'names'], |
| 1065 |
), |
| 1066 |
], |
| 1067 |
) |
| 1068 |
assert_node( |
| 1069 |
extract_node(doctree, 1, 0, 0, 3), |
| 1070 |
desc_parameterlist, |
| 1071 |
multi_line_parameter_list=False, |
| 1072 |
) |
| 1073 |
|
| 1074 |
|
| 1075 |
@pytest.mark.sphinx( |
| 1076 |
'html', |
| 1077 |
testroot='_blank', |
| 1078 |
confoverrides={ |
| 1079 |
'c_maximum_signature_line_length': len('str hello(str name)'), |
| 1080 |
}, |
| 1081 |
) |
| 1082 |
def test_cfunction_signature_with_c_maximum_signature_line_length_break(app): |
| 1083 |
text = '.. c:function:: str hello(str names)' |
| 1084 |
doctree = restructuredtext.parse(app, text) |
| 1085 |
assert_node( |
| 1086 |
doctree, |
| 1087 |
( |
| 1088 |
addnodes.index, |
| 1089 |
[ |
| 1090 |
desc, |
| 1091 |
( |
| 1092 |
[ |
| 1093 |
desc_signature, |
| 1094 |
( |
| 1095 |
[ |
| 1096 |
desc_signature_line, |
| 1097 |
( |
| 1098 |
pending_xref, |
| 1099 |
desc_sig_space, |
| 1100 |
[desc_name, [desc_sig_name, 'hello']], |
| 1101 |
desc_parameterlist, |
| 1102 |
), |
| 1103 |
], |
| 1104 |
), |
| 1105 |
], |
| 1106 |
desc_content, |
| 1107 |
), |
| 1108 |
], |
| 1109 |
), |
| 1110 |
) |
| 1111 |
assert_node( |
| 1112 |
doctree[1], |
| 1113 |
addnodes.desc, |
| 1114 |
desctype='function', |
| 1115 |
domain='c', |
| 1116 |
objtype='function', |
| 1117 |
no_index=False, |
| 1118 |
) |
| 1119 |
assert_node( |
| 1120 |
extract_node(doctree, 1, 0, 0, 3), |
| 1121 |
[ |
| 1122 |
desc_parameterlist, |
| 1123 |
desc_parameter, |
| 1124 |
( |
| 1125 |
[pending_xref, [desc_sig_name, 'str']], |
| 1126 |
desc_sig_space, |
| 1127 |
[desc_sig_name, 'names'], |
| 1128 |
), |
| 1129 |
], |
| 1130 |
) |
| 1131 |
assert_node( |
| 1132 |
extract_node(doctree, 1, 0, 0, 3), |
| 1133 |
desc_parameterlist, |
| 1134 |
multi_line_parameter_list=True, |
| 1135 |
) |
| 1136 |
|
| 1137 |
|
| 1138 |
@pytest.mark.sphinx( |
| 1139 |
'html', |
| 1140 |
testroot='_blank', |
| 1141 |
confoverrides={ |
| 1142 |
'maximum_signature_line_length': len('str hello(str name)'), |
| 1143 |
}, |
| 1144 |
) |
| 1145 |
def test_cfunction_signature_with_maximum_signature_line_length_equal(app): |
| 1146 |
text = '.. c:function:: str hello(str name)' |
| 1147 |
doctree = restructuredtext.parse(app, text) |
| 1148 |
assert_node( |
| 1149 |
doctree, |
| 1150 |
( |
| 1151 |
addnodes.index, |
| 1152 |
[ |
| 1153 |
desc, |
| 1154 |
( |
| 1155 |
[ |
| 1156 |
desc_signature, |
| 1157 |
( |
| 1158 |
[ |
| 1159 |
desc_signature_line, |
| 1160 |
( |
| 1161 |
pending_xref, |
| 1162 |
desc_sig_space, |
| 1163 |
[desc_name, [desc_sig_name, 'hello']], |
| 1164 |
desc_parameterlist, |
| 1165 |
), |
| 1166 |
], |
| 1167 |
), |
| 1168 |
], |
| 1169 |
desc_content, |
| 1170 |
), |
| 1171 |
], |
| 1172 |
), |
| 1173 |
) |
| 1174 |
assert_node( |
| 1175 |
doctree[1], |
| 1176 |
addnodes.desc, |
| 1177 |
desctype='function', |
| 1178 |
domain='c', |
| 1179 |
objtype='function', |
| 1180 |
no_index=False, |
| 1181 |
) |
| 1182 |
assert_node( |
| 1183 |
extract_node(doctree, 1, 0, 0, 3), |
| 1184 |
[ |
| 1185 |
desc_parameterlist, |
| 1186 |
desc_parameter, |
| 1187 |
( |
| 1188 |
[pending_xref, [desc_sig_name, 'str']], |
| 1189 |
desc_sig_space, |
| 1190 |
[desc_sig_name, 'name'], |
| 1191 |
), |
| 1192 |
], |
| 1193 |
) |
| 1194 |
assert_node( |
| 1195 |
extract_node(doctree, 1, 0, 0, 3), |
| 1196 |
desc_parameterlist, |
| 1197 |
multi_line_parameter_list=False, |
| 1198 |
) |
| 1199 |
|
| 1200 |
|
| 1201 |
@pytest.mark.sphinx( |
| 1202 |
'html', |
| 1203 |
testroot='_blank', |
| 1204 |
confoverrides={ |
| 1205 |
'maximum_signature_line_length': len('str hello(str name)'), |
| 1206 |
}, |
| 1207 |
) |
| 1208 |
def test_cfunction_signature_with_maximum_signature_line_length_force_single(app): |
| 1209 |
text = '.. c:function:: str hello(str names)\n :single-line-parameter-list:' |
| 1210 |
doctree = restructuredtext.parse(app, text) |
| 1211 |
assert_node( |
| 1212 |
doctree, |
| 1213 |
( |
| 1214 |
addnodes.index, |
| 1215 |
[ |
| 1216 |
desc, |
| 1217 |
( |
| 1218 |
[ |
| 1219 |
desc_signature, |
| 1220 |
( |
| 1221 |
[ |
| 1222 |
desc_signature_line, |
| 1223 |
( |
| 1224 |
pending_xref, |
| 1225 |
desc_sig_space, |
| 1226 |
[desc_name, [desc_sig_name, 'hello']], |
| 1227 |
desc_parameterlist, |
| 1228 |
), |
| 1229 |
], |
| 1230 |
), |
| 1231 |
], |
| 1232 |
desc_content, |
| 1233 |
), |
| 1234 |
], |
| 1235 |
), |
| 1236 |
) |
| 1237 |
assert_node( |
| 1238 |
doctree[1], |
| 1239 |
addnodes.desc, |
| 1240 |
desctype='function', |
| 1241 |
domain='c', |
| 1242 |
objtype='function', |
| 1243 |
no_index=False, |
| 1244 |
) |
| 1245 |
assert_node( |
| 1246 |
extract_node(doctree, 1, 0, 0, 3), |
| 1247 |
[ |
| 1248 |
desc_parameterlist, |
| 1249 |
desc_parameter, |
| 1250 |
( |
| 1251 |
[pending_xref, [desc_sig_name, 'str']], |
| 1252 |
desc_sig_space, |
| 1253 |
[desc_sig_name, 'names'], |
| 1254 |
), |
| 1255 |
], |
| 1256 |
) |
| 1257 |
assert_node( |
| 1258 |
extract_node(doctree, 1, 0, 0, 3), |
| 1259 |
desc_parameterlist, |
| 1260 |
multi_line_parameter_list=False, |
| 1261 |
) |
| 1262 |
|
| 1263 |
|
| 1264 |
@pytest.mark.sphinx( |
| 1265 |
'html', |
| 1266 |
testroot='_blank', |
| 1267 |
confoverrides={ |
| 1268 |
'maximum_signature_line_length': len('str hello(str name)'), |
| 1269 |
}, |
| 1270 |
) |
| 1271 |
def test_cfunction_signature_with_maximum_signature_line_length_break(app): |
| 1272 |
text = '.. c:function:: str hello(str names)' |
| 1273 |
doctree = restructuredtext.parse(app, text) |
| 1274 |
assert_node( |
| 1275 |
doctree, |
| 1276 |
( |
| 1277 |
addnodes.index, |
| 1278 |
[ |
| 1279 |
desc, |
| 1280 |
( |
| 1281 |
[ |
| 1282 |
desc_signature, |
| 1283 |
( |
| 1284 |
[ |
| 1285 |
desc_signature_line, |
| 1286 |
( |
| 1287 |
pending_xref, |
| 1288 |
desc_sig_space, |
| 1289 |
[desc_name, [desc_sig_name, 'hello']], |
| 1290 |
desc_parameterlist, |
| 1291 |
), |
| 1292 |
], |
| 1293 |
), |
| 1294 |
], |
| 1295 |
desc_content, |
| 1296 |
), |
| 1297 |
], |
| 1298 |
), |
| 1299 |
) |
| 1300 |
assert_node( |
| 1301 |
doctree[1], |
| 1302 |
addnodes.desc, |
| 1303 |
desctype='function', |
| 1304 |
domain='c', |
| 1305 |
objtype='function', |
| 1306 |
no_index=False, |
| 1307 |
) |
| 1308 |
assert_node( |
| 1309 |
extract_node(doctree, 1, 0, 0, 3), |
| 1310 |
[ |
| 1311 |
desc_parameterlist, |
| 1312 |
desc_parameter, |
| 1313 |
( |
| 1314 |
[pending_xref, [desc_sig_name, 'str']], |
| 1315 |
desc_sig_space, |
| 1316 |
[desc_sig_name, 'names'], |
| 1317 |
), |
| 1318 |
], |
| 1319 |
) |
| 1320 |
assert_node( |
| 1321 |
extract_node(doctree, 1, 0, 0, 3), |
| 1322 |
desc_parameterlist, |
| 1323 |
multi_line_parameter_list=True, |
| 1324 |
) |
| 1325 |
|
| 1326 |
|
| 1327 |
@pytest.mark.sphinx( |
| 1328 |
'html', |
| 1329 |
testroot='_blank', |
| 1330 |
confoverrides={ |
| 1331 |
'c_maximum_signature_line_length': len('str hello(str name)'), |
| 1332 |
'maximum_signature_line_length': 1, |
| 1333 |
}, |
| 1334 |
) |
| 1335 |
def test_c_maximum_signature_line_length_overrides_global(app): |
| 1336 |
text = '.. c:function:: str hello(str name)' |
| 1337 |
doctree = restructuredtext.parse(app, text) |
| 1338 |
assert_node( |
| 1339 |
doctree, |
| 1340 |
( |
| 1341 |
addnodes.index, |
| 1342 |
[ |
| 1343 |
desc, |
| 1344 |
( |
| 1345 |
[ |
| 1346 |
desc_signature, |
| 1347 |
([ |
| 1348 |
desc_signature_line, |
| 1349 |
( |
| 1350 |
pending_xref, |
| 1351 |
desc_sig_space, |
| 1352 |
[desc_name, [desc_sig_name, 'hello']], |
| 1353 |
desc_parameterlist, |
| 1354 |
), |
| 1355 |
]), |
| 1356 |
], |
| 1357 |
desc_content, |
| 1358 |
), |
| 1359 |
], |
| 1360 |
), |
| 1361 |
) |
| 1362 |
assert_node( |
| 1363 |
doctree[1], |
| 1364 |
addnodes.desc, |
| 1365 |
desctype='function', |
| 1366 |
domain='c', |
| 1367 |
objtype='function', |
| 1368 |
no_index=False, |
| 1369 |
) |
| 1370 |
assert_node( |
| 1371 |
extract_node(doctree, 1, 0, 0, 3), |
| 1372 |
[ |
| 1373 |
desc_parameterlist, |
| 1374 |
desc_parameter, |
| 1375 |
( |
| 1376 |
[pending_xref, [desc_sig_name, 'str']], |
| 1377 |
desc_sig_space, |
| 1378 |
[desc_sig_name, 'name'], |
| 1379 |
), |
| 1380 |
], |
| 1381 |
) |
| 1382 |
assert_node( |
| 1383 |
extract_node(doctree, 1, 0, 0, 3), |
| 1384 |
desc_parameterlist, |
| 1385 |
multi_line_parameter_list=False, |
| 1386 |
) |
| 1387 |
|
| 1388 |
|
| 1389 |
@pytest.mark.sphinx('html', testroot='domain-c-c_maximum_signature_line_length') |
| 1390 |
def test_domain_c_c_maximum_signature_line_length_in_html(app): |
| 1391 |
app.build() |
| 1392 |
content = (app.outdir / 'index.html').read_text(encoding='utf-8') |
| 1393 |
expected = """\ |
| 1394 |
|
| 1395 |
<dl> |
| 1396 |
<dd>\ |
| 1397 |
<span class="n"><span class="pre">str</span></span>\ |
| 1398 |
<span class="w"> </span>\ |
| 1399 |
<span class="n"><span class="pre">name</span></span>\ |
| 1400 |
</dd> |
| 1401 |
</dl> |
| 1402 |
|
| 1403 |
<span class="sig-paren">)</span>\ |
| 1404 |
<a class="headerlink" href="#c.hello" title="Link to this definition">ΒΆ</a>\ |
| 1405 |
<br />\ |
| 1406 |
</dt> |
| 1407 |
""" |
| 1408 |
assert expected in content |
| 1409 |
|
| 1410 |
|
| 1411 |
@pytest.mark.sphinx( |
| 1412 |
'text', |
| 1413 |
testroot='domain-c-c_maximum_signature_line_length', |
| 1414 |
) |
| 1415 |
def test_domain_c_c_maximum_signature_line_length_in_text(app): |
| 1416 |
app.build() |
| 1417 |
content = (app.outdir / 'index.txt').read_text(encoding='utf8') |
| 1418 |
param_line_fmt = STDINDENT * ' ' + '{}\n' |
| 1419 |
|
| 1420 |
expected_parameter_list_hello = '(\n{})'.format(param_line_fmt.format('str name')) |
| 1421 |
|
| 1422 |
assert expected_parameter_list_hello in content |