Coverage for tests/test_parsers/test_attributes.py: 100.00%

80 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-09 17:28 +0100

1"""Tests for [the `parsers.attributes` module][pytkdocs.parsers.attributes].""" 

2 

3from pytkdocs.parsers.attributes import get_class_attributes, get_instance_attributes, get_module_attributes 

4from tests.fixtures.parsing import attributes as attr_module 

5 

6 

7class TestParsing: 

8 """Test the parser in general.""" 

9 

10 attributes = get_module_attributes(attr_module) 

11 

12 def test_parse_tuple_target(self) -> None: 

13 """Assert can parse `a, b, c = 0, 0, 0`.""" 

14 assert "OK" in self.attributes 

15 assert "WARNING" in self.attributes 

16 assert "CRITICAL" in self.attributes 

17 assert "UNKNOWN" in self.attributes 

18 

19 

20class TestModuleAttributes: 

21 """Test the parser for module attributes.""" 

22 

23 attributes = get_module_attributes(attr_module) 

24 

25 def test_pick_up_attribute_without_docstring(self) -> None: 

26 """Don't pick attributes without docstrings.""" 

27 assert "NO_DOC_NO_TYPE" in self.attributes 

28 assert "NO_DOC_NO_VALUE" in self.attributes 

29 assert "NO_DOC" in self.attributes 

30 

31 def test_pick_up_attribute_without_type(self) -> None: 

32 """Pick up attribute without a type.""" 

33 assert "NO_TYPE" in self.attributes 

34 assert self.attributes["NO_TYPE"]["docstring"] == "No type." 

35 

36 def test_pick_up_attribute_without_value(self) -> None: 

37 """Pick up attribute without a value.""" 

38 assert "NO_VALUE" in self.attributes 

39 assert self.attributes["NO_VALUE"]["docstring"] == "No value." 

40 

41 def test_pick_up_attribute_with_type_and_value(self) -> None: 

42 """Pick up attribute with type and value.""" 

43 assert "FULL" in self.attributes 

44 assert self.attributes["FULL"]["docstring"] == "Full." 

45 

46 def test_pick_up_attribute_with_complex_type(self) -> None: 

47 """Pick up attribute with complex type.""" 

48 assert "COMPLEX_TYPE" in self.attributes 

49 assert self.attributes["COMPLEX_TYPE"]["docstring"] == "Complex type." 

50 

51 def test_pick_up_attribute_in_if(self) -> None: 

52 """Pick attribute in `if` and `else`.""" 

53 assert "IN_IF" in self.attributes 

54 assert self.attributes["IN_IF"]["docstring"] == "In if." 

55 

56 assert "IN_ELSE" in self.attributes 

57 assert self.attributes["IN_ELSE"]["docstring"] == "In else." 

58 

59 def test_pick_up_attribute_in_try_except(self) -> None: 

60 """Pick attribute in `try`, `except`, `else` and `finally`..""" 

61 assert "IN_TRY" in self.attributes 

62 assert self.attributes["IN_TRY"]["docstring"] == "In try." 

63 

64 assert "IN_EXCEPT" in self.attributes 

65 assert self.attributes["IN_EXCEPT"]["docstring"] == "In except." 

66 

67 assert "IN_TRY_ELSE" in self.attributes 

68 assert self.attributes["IN_TRY_ELSE"]["docstring"] == "In try else." 

69 

70 assert "IN_FINALLY" in self.attributes 

71 assert self.attributes["IN_FINALLY"]["docstring"] == "In finally." 

72 

73 def test_docstring_is_correctly_dedented(self) -> None: # noqa: D102 

74 assert "\n " not in self.attributes["DEDENT"]["docstring"] 

75 

76 

77class TestClassAttributes: 

78 """Test the parser for module attributes.""" 

79 

80 attributes = get_class_attributes(attr_module.E) 

81 

82 def test_pick_up_attribute_in_class(self) -> None: 

83 """Pick up class attribute.""" 

84 assert "IN_CLASS" in self.attributes 

85 assert self.attributes["IN_CLASS"]["docstring"] == "In class." 

86 

87 def test_docstring_is_correctly_dedented(self) -> None: # noqa: D102 

88 assert "\n " not in self.attributes["DEDENT"]["docstring"] 

89 

90 

91class TestInstanceAttributes: 

92 """Test the parser for module attributes.""" 

93 

94 attributes = get_instance_attributes(attr_module.E.__init__) 

95 

96 def test_pick_up_attribute_in_init_method(self) -> None: 

97 """Pick up instance attribute.""" 

98 assert "in_init" in self.attributes 

99 assert self.attributes["in_init"]["docstring"] == "In init." 

100 

101 def test_do_not_pick_up_non_attributes(self) -> None: 

102 """Don't pick documented variables in functions.""" 

103 assert "non_attribute" not in self.attributes 

104 assert "non_attribute2" not in self.attributes 

105 assert "non_self_attribute" not in self.attributes 

106 assert "non_self_attribute2" not in self.attributes 

107 

108 def test_do_not_pick_up_subscript_attribute(self) -> None: 

109 """Don't pick documented variables in functions.""" 

110 assert "d" not in self.attributes 

111 assert "d.subscript" not in self.attributes 

112 assert "subscript" not in self.attributes 

113 

114 def test_docstring_is_correctly_dedented(self) -> None: # noqa: D102 

115 assert "\n " not in self.attributes["dedent"]["docstring"] 

116 

117 

118class TestPydanticFields: 

119 """Test the parser for module attributes.""" 

120 

121 attributes = get_class_attributes(attr_module.Model) 

122 

123 def test_pick_up_attribute_in_pydantic_model(self) -> None: 

124 """Pick up attribute in Pydantic model.""" 

125 assert "in_pydantic_model" in self.attributes 

126 assert self.attributes["in_pydantic_model"]["docstring"] == "In Pydantic model." 

127 

128 assert "model_field" in self.attributes 

129 assert self.attributes["model_field"]["docstring"] == "A model field." 

130 

131 

132class TestMarshmallowFields: 

133 """Test the parser for module attributes.""" 

134 

135 attributes = get_class_attributes(attr_module.MarshmallowSchema) 

136 

137 def test_pick_up_attribute_in_pydantic_model(self) -> None: 

138 """Pick up attribute in Marshmallow model.""" 

139 assert "in_marshmallow_model" in self.attributes 

140 assert self.attributes["in_marshmallow_model"]["docstring"] == "In Marshmallow model." 

141 

142 assert "model_field" in self.attributes 

143 assert self.attributes["model_field"]["docstring"] == "A model field."