|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 | from inheritance_explorer._testing import ClassForTesting |
7 | | -from inheritance_explorer.inheritance_explorer import ClassGraphTree, _ChildNode |
| 7 | +from inheritance_explorer.inheritance_explorer import ( |
| 8 | + ClassGraphTree, |
| 9 | + _ChildNode, |
| 10 | + _validate_color, |
| 11 | +) |
8 | 12 |
|
9 | 13 |
|
10 | 14 | @pytest.fixture() |
@@ -143,3 +147,67 @@ def test_class_exclusion(): |
143 | 147 | ) |
144 | 148 | for node in cgt._node_list: |
145 | 149 | assert node.child_name != "ClassForTesting2" |
| 150 | + |
| 151 | + |
| 152 | +def test_errors_no_source_override(): |
| 153 | + # _get_source_info |
| 154 | + cgt = ClassGraphTree(ClassForTesting) |
| 155 | + with pytest.raises( |
| 156 | + RuntimeError, match="this functionality requires function tracking." |
| 157 | + ): |
| 158 | + _ = cgt._get_source_info(ClassForTesting) |
| 159 | + |
| 160 | + with pytest.raises( |
| 161 | + RuntimeError, match="this functionality requires function tracking." |
| 162 | + ): |
| 163 | + cgt._store_node_func_source(ClassForTesting, 0) |
| 164 | + |
| 165 | + cgt = ClassGraphTree(ClassForTesting, funcname="use_this_func") |
| 166 | + # monkey patch the funcname to make the callable fail |
| 167 | + cgt.funcname = "misc_attr" |
| 168 | + result = cgt._get_source_info(ClassForTesting) |
| 169 | + assert result is None |
| 170 | + |
| 171 | + |
| 172 | +def test_missing_source_node(cgt): |
| 173 | + with pytest.raises(KeyError, match="Could not find node for not_a_node"): |
| 174 | + cgt.get_source_code_by_node_name("not_a_node") |
| 175 | + |
| 176 | + |
| 177 | +def test_class_graph_special_cases(cgt): |
| 178 | + |
| 179 | + # map out the structure of SimilarityContainer, track the run method |
| 180 | + assert cgt._node_list[1].parent_id == "1" |
| 181 | + with pytest.raises( |
| 182 | + ValueError, match="unexpected value, similarity_container_class" |
| 183 | + ): |
| 184 | + _ = cgt.check_source_similarity(similarity_container_class="not_a_thing") |
| 185 | + |
| 186 | + cgt.check_source_similarity(reference=1) |
| 187 | + |
| 188 | + |
| 189 | +def test_set_graphviz_args(cgt): |
| 190 | + |
| 191 | + cgt.set_graphviz_args_kwargs(1, 2, 3, other_arg="hello", another=True) |
| 192 | + |
| 193 | + d = cgt._graphviz_args_kwargs |
| 194 | + assert all([val in d["args"] for val in (1, 2, 3)]) |
| 195 | + assert "other_arg" in d["kwargs"] |
| 196 | + assert d["kwargs"]["other_arg"] == "hello" |
| 197 | + assert "another" in d["kwargs"] |
| 198 | + assert d["kwargs"]["another"] is True |
| 199 | + |
| 200 | + |
| 201 | +@pytest.mark.parametrize( |
| 202 | + "input_clr,expected", |
| 203 | + [((0.0, 0.0, 0.0), "#000000"), (None, "#ffffff"), ("#000000", "#000000")], |
| 204 | +) |
| 205 | +def test_validate_color(input_clr, expected): |
| 206 | + default_clr = (1.0, 1.0, 1.0) |
| 207 | + |
| 208 | + assert _validate_color(input_clr, default_rgb_tuple=default_clr) == expected |
| 209 | + |
| 210 | + |
| 211 | +def test_validate_color_invalid(): |
| 212 | + with pytest.raises(TypeError, match="clr has unexpected type"): |
| 213 | + _validate_color(100, (1.0, 1.0, 1.0)) |
0 commit comments