File: test_doc.py

package info (click to toggle)
oxigraph 0.5.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,944 kB
  • sloc: python: 1,961; cpp: 158; sh: 145; makefile: 42
file content (47 lines) | stat: -rw-r--r-- 1,564 bytes parent folder | download
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
import inspect
from doctest import DocTest, DocTestFinder, DocTestSuite
from typing import Any, Dict, List, Optional
from unittest import TestLoader, TestSuite

import pyoxigraph


class ExtendedDocTestFinder(DocTestFinder):
    """
    More aggressive doctest lookup
    """

    def _find(
        self,
        tests: List[DocTest],
        obj: Any,
        name: Any,
        module: Any,
        source_lines: Any,
        globs: Any,
        seen: Dict[int, Any],
    ) -> None:
        # If we've already processed this object, then ignore it.
        if id(obj) in seen:
            return
        seen[id(obj)] = 1

        # Find a test for this object, and add it to the list of tests.
        test = self._get_test(obj, name, module, globs, source_lines)  # type: ignore[attr-defined]
        if test is not None:
            tests.append(test)

        # Look for tests in a module's contained objects.
        if inspect.ismodule(obj) or inspect.isclass(obj):
            for valname, val in obj.__dict__.items():
                if valname == "__doc__":
                    continue
                # Special handling for staticmethod/classmethod.
                if isinstance(val, (staticmethod, classmethod)):
                    val = val.__func__
                self._find(tests, val, f"{name}.{valname}", module, source_lines, globs, seen)


def load_tests(_loader: TestLoader, tests: TestSuite, _pattern: Optional[str] = None) -> TestSuite:
    tests.addTests(DocTestSuite(pyoxigraph, test_finder=ExtendedDocTestFinder()))
    return tests