|
| 1 | +# -*- encoding:utf-8 -*- |
| 2 | +from __future__ import division, absolute_import, print_function |
| 3 | + |
| 4 | +from nose.tools import assert_equal |
| 5 | +from numpydoc.xref import make_xref_param_type |
| 6 | + |
| 7 | +xref_aliases = { |
| 8 | + # python |
| 9 | + 'sequence': ':term:`python:sequence`', |
| 10 | + 'iterable': ':term:`python:iterable`', |
| 11 | + 'string': 'str', |
| 12 | + # numpy |
| 13 | + 'array': '~numpy.array', |
| 14 | + 'dtype': 'numpy.dtype', |
| 15 | + 'ndarray': '~numpy.ndarray', |
| 16 | + 'matrix': 'numpy.matrix', |
| 17 | + 'array-like': ':term:`numpy:array_like`', |
| 18 | + 'array_like': ':term:`numpy:array_like`', |
| 19 | +} |
| 20 | + |
| 21 | +# Comes mainly from numpy |
| 22 | +data = """ |
| 23 | +(...) array_like, float, optional |
| 24 | +(...) :term:`numpy:array_like`, :xref_param_type:`float`, optional |
| 25 | +
|
| 26 | +(2,) ndarray |
| 27 | +(2,) :xref_param_type:`~numpy.ndarray` |
| 28 | +
|
| 29 | +(...,M,N) array_like |
| 30 | +(...,M,N) :term:`numpy:array_like` |
| 31 | +
|
| 32 | +(float, float), optional |
| 33 | +(:xref_param_type:`float`, :xref_param_type:`float`), optional |
| 34 | +
|
| 35 | +1-D array or sequence |
| 36 | +1-D :xref_param_type:`~numpy.array` or :term:`python:sequence` |
| 37 | +
|
| 38 | +array of str or unicode-like |
| 39 | +:xref_param_type:`~numpy.array` of :xref_param_type:`str` or unicode-like |
| 40 | +
|
| 41 | +array_like of float |
| 42 | +:term:`numpy:array_like` of :xref_param_type:`float` |
| 43 | +
|
| 44 | +bool or callable |
| 45 | +:xref_param_type:`bool` or :xref_param_type:`callable` |
| 46 | +
|
| 47 | +int in [0, 255] |
| 48 | +:xref_param_type:`int` in [0, 255] |
| 49 | +
|
| 50 | +int or None, optional |
| 51 | +:xref_param_type:`int` or :xref_param_type:`None`, optional |
| 52 | +
|
| 53 | +list of str or array_like |
| 54 | +:xref_param_type:`list` of :xref_param_type:`str` or :term:`numpy:array_like` |
| 55 | +
|
| 56 | +sequence of array_like |
| 57 | +:term:`python:sequence` of :term:`numpy:array_like` |
| 58 | +
|
| 59 | +str or pathlib.Path |
| 60 | +:xref_param_type:`str` or :xref_param_type:`pathlib.Path` |
| 61 | +
|
| 62 | +{'', string}, optional |
| 63 | +{'', :xref_param_type:`str`}, optional |
| 64 | +
|
| 65 | +{'C', 'F', 'A', or 'K'}, optional |
| 66 | +{'C', 'F', 'A', or 'K'}, optional |
| 67 | +
|
| 68 | +{'linear', 'lower', 'higher', 'midpoint', 'nearest'} |
| 69 | +{'linear', 'lower', 'higher', 'midpoint', 'nearest'} |
| 70 | +
|
| 71 | +{False, True, 'greedy', 'optimal'} |
| 72 | +{:xref_param_type:`False`, :xref_param_type:`True`, 'greedy', 'optimal'} |
| 73 | +
|
| 74 | +{{'begin', 1}, {'end', 0}}, {string, int} |
| 75 | +{{'begin', 1}, {'end', 0}}, {string, :xref_param_type:`int`} |
| 76 | +
|
| 77 | +callable f'(x,*args) |
| 78 | +:xref_param_type:`callable` f'(x,*args) |
| 79 | +
|
| 80 | +callable ``fhess(x, *args)``, optional |
| 81 | +:xref_param_type:`callable` ``fhess(x, *args)``, optional |
| 82 | +
|
| 83 | +spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``) |
| 84 | +:xref_param_type:`spmatrix` (format: ``csr``, ``bsr``, ``dia`` or coo``) |
| 85 | +
|
| 86 | +list(int) |
| 87 | +:xref_param_type:`list`(:xref_param_type:`int`) |
| 88 | +
|
| 89 | +list[int] |
| 90 | +:xref_param_type:`list`[:xref_param_type:`int`] |
| 91 | +
|
| 92 | +dict(str, int) |
| 93 | +:xref_param_type:`dict`(:xref_param_type:`str`, :xref_param_type:`int`) |
| 94 | +
|
| 95 | +dict[str, int] |
| 96 | +:xref_param_type:`dict`[:xref_param_type:`str`, :xref_param_type:`int`] |
| 97 | +
|
| 98 | +tuple(float, float) |
| 99 | +:xref_param_type:`tuple`(:xref_param_type:`float`, :xref_param_type:`float`) |
| 100 | +
|
| 101 | +dict[tuple(str, str), int] |
| 102 | +:xref_param_type:`dict`[:xref_param_type:`tuple`(:xref_param_type:`str`, :xref_param_type:`str`), :xref_param_type:`int`] |
| 103 | +""" # noqa: E501 |
| 104 | + |
| 105 | + |
| 106 | +def test_make_xref_param_type(): |
| 107 | + for s in data.strip().split('\n\n'): |
| 108 | + param_type, expected_result = s.split('\n') |
| 109 | + result = make_xref_param_type(param_type, xref_aliases) |
| 110 | + assert_equal(result, expected_result) |
0 commit comments