Skip to content

Commit 8c1a6a0

Browse files
committed
Test unchecked lines.
These are mostly error conditions, which are now explicitly checked.
1 parent 70ab0ee commit 8c1a6a0

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/test_base_descriptor.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ class Example:
3939
a body giving a longer description of what's going on.
4040
"""
4141

42+
# The code that finds docstrings needs to ignore anything
43+
# that's not a "simple name". The code below should not
44+
# cause an error, but will cause a ``continue`` statement
45+
# to skip actions, testing another code path when the
46+
# class is analysed.
47+
dict_attribute = {}
48+
dict_attribute["foo"] = "bar"
49+
"""Here is a spurious docstring that should be ignored."""
50+
51+
base_descriptor = BaseDescriptor()
52+
"""This descriptor should raise NotImplementedError."""
53+
4254

4355
def test_docstrings_are_retrieved():
4456
"""Check that the docstring can be picked up from the class definition."""
@@ -47,6 +59,30 @@ def test_docstrings_are_retrieved():
4759
assert docs["my_property"] == "Docs for my_property."
4860

4961

62+
def test_non_classes_raise_errors():
63+
"""Check we validate the input object.
64+
65+
If `get_class_attribute_docstrings` is called on something other than
66+
a class, we should raise an error.
67+
"""
68+
69+
def dummy():
70+
pass
71+
72+
with pytest.raises(TypeError):
73+
get_class_attribute_docstrings(dummy)
74+
75+
76+
def test_uncheckable_class():
77+
"""Check we don't crash if we can't check a class.
78+
79+
If `inspect.getsource` fails, we should return an empty dict.
80+
"""
81+
MyClass = type("MyClass", dict={"intattr": 10})
82+
doc = get_class_attribute_docstrings(MyClass)
83+
assert doc == {}
84+
85+
5086
def test_docstrings_are_cached():
5187
"""Check that the docstrings aren't being regenerated every time."""
5288
docs1 = get_class_attribute_docstrings(Example)
@@ -92,3 +128,6 @@ def test_basedescriptor_get():
92128
e = Example()
93129
assert e.my_property == "An example value."
94130
assert isinstance(Example.my_property, MockProperty)
131+
with pytest.raises(NotImplementedError):
132+
# BaseDescriptor requires `instance_get` to be overridden.
133+
e.base_descriptor

0 commit comments

Comments
 (0)