File: test_autodoc_enhancements.py

package info (click to toggle)
python-astropy-helpers 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 756 kB
  • ctags: 698
  • sloc: python: 5,929; ansic: 88; makefile: 11
file content (56 lines) | stat: -rw-r--r-- 1,699 bytes parent folder | download | duplicates (19)
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
import sys

from textwrap import dedent

import pytest

from ..autodoc_enhancements import type_object_attrgetter


# Define test classes outside the class; otherwise there is flakiness with the
# details of how exec works on different Python versions
class Meta(type):
    @property
    def foo(cls):
        return 'foo'

if sys.version_info[0] < 3:
    exec(dedent("""
        class MyClass(object):
            __metaclass__ = Meta
            @property
            def foo(self):
                \"\"\"Docstring for MyClass.foo property.\"\"\"
                return 'myfoo'
    """))
else:
    exec(dedent("""
        class MyClass(metaclass=Meta):
            @property
            def foo(self):
                \"\"\"Docstring for MyClass.foo property.\"\"\"
                return 'myfoo'
    """))


def test_type_attrgetter():
    """
    This test essentially reproduces the docstring for
    `type_object_attrgetter`.

    Sphinx itself tests the custom attrgetter feature; see:
    https://bitbucket.org/birkenfeld/sphinx/src/40bd03003ac6fe274ccf3c80d7727509e00a69ea/tests/test_autodoc.py?at=default#cl-502
    so rather than a full end-to-end functional test it's simple enough to just
    test that this function does what it needs to do.
    """

    assert getattr(MyClass, 'foo') == 'foo'
    obj = type_object_attrgetter(MyClass, 'foo')
    assert isinstance(obj, property)
    assert obj.__doc__ == 'Docstring for MyClass.foo property.'

    with pytest.raises(AttributeError):
        type_object_attrgetter(MyClass, 'susy')

    assert type_object_attrgetter(MyClass, 'susy', 'default') == 'default'
    assert type_object_attrgetter(MyClass, '__dict__') == MyClass.__dict__