|
4 | 4 |
|
5 | 5 |
|
6 | 6 | @pytest.fixture |
7 | | -def doc() -> Module: |
| 7 | +def module() -> Module: |
8 | 8 | return Module.from_name("pathlib") |
9 | 9 |
|
10 | 10 |
|
11 | 11 | @pytest.fixture |
12 | | -def open(doc: Module) -> Function: |
13 | | - return doc.get("Path.open") |
| 12 | +def function(module: Module) -> Function: |
| 13 | + return module.get("Path.open") |
14 | 14 |
|
15 | 15 |
|
16 | 16 | @pytest.fixture |
17 | | -def funcstr() -> PdocStr: |
| 17 | +def pdocstr() -> PdocStr: |
18 | 18 | return PdocStr("\n".join([" def dummy():", " pass"])) |
19 | 19 |
|
20 | 20 |
|
21 | | -def test_module(doc: Module): |
22 | | - assert isinstance(doc, Module) |
| 21 | +@pytest.fixture(params=["indent", "dedent", "nodoc", "lower", "upper"]) |
| 22 | +def pdocstr_attr(request): |
| 23 | + return request.param |
23 | 24 |
|
24 | 25 |
|
25 | | -def test_class(doc: Module): |
26 | | - cls = doc.get("Path") |
| 26 | +@pytest.fixture(params=["source", "code", "docstring"]) |
| 27 | +def function_prop(request): |
| 28 | + return request.param |
27 | 29 |
|
28 | | - assert cls.name == "Path" |
29 | | - assert isinstance(cls, Function) |
30 | 30 |
|
31 | | - cls = doc.get("NotAClass") |
32 | | - assert cls is None |
| 31 | +def test_module(): |
| 32 | + m = Module.from_name("pathlib") |
33 | 33 |
|
34 | | - func = doc.get("Path.notafunction") |
35 | | - assert func is None |
| 34 | + assert isinstance(m, Module) |
36 | 35 |
|
37 | 36 |
|
38 | | -def test_func(open: Function): |
39 | | - assert open.name == "open" |
40 | | - assert isinstance(open, Function) |
41 | | - assert hasattr(open, "code") |
| 37 | +def test_module_raises(): |
| 38 | + with pytest.raises(RuntimeError): |
| 39 | + Module.from_name("not_a_module") |
42 | 40 |
|
43 | 41 |
|
44 | | -def test_str(open: Function): |
45 | | - sourcecode = open.code |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "name, returntype", |
| 44 | + [ |
| 45 | + ("Path", Function), |
| 46 | + ("Path.open", Function), |
| 47 | + ("NotAClass", type(None)), |
| 48 | + ("Path.notafunction", type(None)), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_module_returntype(module: Module, name: str, returntype: type): |
| 52 | + obj = module.get(name) |
| 53 | + assert isinstance(obj, returntype) |
46 | 54 |
|
47 | | - assert isinstance(sourcecode, PdocStr) |
48 | | - assert hasattr(sourcecode, "dedent") |
49 | 55 |
|
50 | | - assert isinstance(open.docstring, PdocStr) |
| 56 | +def test_pdocstr_attributes(pdocstr_attr: str, pdocstr: PdocStr): |
51 | 57 |
|
| 58 | + assert hasattr(pdocstr, pdocstr_attr) |
52 | 59 |
|
53 | | -def test_module_raises(): |
54 | | - with pytest.raises(RuntimeError): |
55 | | - Module.from_name("not_a_module") |
56 | 60 |
|
| 61 | +def test_pdocstr_returntypes(pdocstr_attr, pdocstr: PdocStr): |
| 62 | + method = getattr(pdocstr, pdocstr_attr) |
57 | 63 |
|
58 | | -def test_dedent(funcstr: PdocStr): |
59 | | - s = funcstr.dedent() |
| 64 | + assert isinstance(method(), PdocStr) |
60 | 65 |
|
61 | | - assert isinstance(s, PdocStr) |
62 | | - assert s.startswith("def dummy():\n pass") |
63 | 66 |
|
| 67 | +def test_pdocstr_callable(pdocstr_attr, pdocstr: PdocStr): |
| 68 | + method = getattr(pdocstr, pdocstr_attr) |
| 69 | + |
| 70 | + assert callable(method) |
| 71 | + |
| 72 | + |
| 73 | +def test_pdocstr_nodoc(): |
| 74 | + text = [ |
| 75 | + "", |
| 76 | + '"""docstring"""', |
| 77 | + "", |
| 78 | + "def dummy():", # 3 |
| 79 | + " pass", |
| 80 | + "", |
| 81 | + ] |
| 82 | + |
| 83 | + funcstr = PdocStr("\n".join(text)) |
| 84 | + |
| 85 | + assert funcstr.nodoc() == "\n".join(text[3:5]) |
| 86 | + |
| 87 | + |
| 88 | +def test_pdocstr_shebang(): |
| 89 | + text = [ |
| 90 | + "#! python3", |
| 91 | + "", |
| 92 | + '"""docstring"""', |
| 93 | + "", |
| 94 | + "def dummy():", # 4 |
| 95 | + " pass", |
| 96 | + "", |
| 97 | + ] |
| 98 | + |
| 99 | + funcstr = PdocStr("\n".join(text)) |
| 100 | + |
| 101 | + assert funcstr.nodoc() == "\n".join(text[4:6]) |
64 | 102 |
|
65 | | -def test_autopep8(funcstr: PdocStr): |
66 | | - s = funcstr.indent() |
67 | 103 |
|
68 | | - assert isinstance(s, PdocStr) |
| 104 | +def test_pdocstr_indent(pdocstr: PdocStr): |
| 105 | + s = pdocstr.indent() |
| 106 | + |
69 | 107 | assert s.startswith("def dummy():\n pass") |
| 108 | + |
| 109 | + |
| 110 | +def test_pdocstr_dedent(pdocstr: PdocStr): |
| 111 | + s = pdocstr.dedent() |
| 112 | + |
| 113 | + assert s.startswith("def dummy():\n pass") |
| 114 | + |
| 115 | + |
| 116 | +def test_function_attributes(function_prop, function): |
| 117 | + assert hasattr(function, function_prop) |
| 118 | + |
| 119 | + |
| 120 | +def test_function_returntypes(function_prop, function): |
| 121 | + prop = getattr(function, function_prop) |
| 122 | + |
| 123 | + assert isinstance(prop, PdocStr) |
| 124 | + |
| 125 | + |
| 126 | +def test_function_property(function_prop, function): |
| 127 | + prop = getattr(function, function_prop) |
| 128 | + |
| 129 | + assert not callable(prop) |
| 130 | + |
| 131 | + |
| 132 | +def test_function_code(function): |
| 133 | + doc = function.docstring |
| 134 | + |
| 135 | + assert doc, "testing function should have a docstring" |
| 136 | + assert doc not in function.code |
0 commit comments