mktestdocs 0.2.1__tar.gz → 0.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mktestdocs
3
- Version: 0.2.1
4
- Provides-Extra: test
3
+ Version: 0.2.3
5
4
  License-File: LICENSE
5
+ Provides-Extra: test
6
+ Requires-Dist: pytest>=4.0.2; extra == "test"
@@ -1,6 +1,6 @@
1
1
  <img src="icon.png" width=125 height=125 align="right">
2
2
 
3
- # mktestdocs
3
+ ### mktestdocs
4
4
 
5
5
  Run pytest against markdown files/docstrings.
6
6
 
@@ -7,7 +7,7 @@ from mktestdocs.__main__ import (
7
7
  get_codeblock_members,
8
8
  )
9
9
 
10
- __version__ = "0.2.1"
10
+ __version__ = "0.2.3"
11
11
 
12
12
  __all__ = [
13
13
  "__version__",
@@ -3,7 +3,6 @@ import pathlib
3
3
  import subprocess
4
4
  import textwrap
5
5
 
6
-
7
6
  _executors = {}
8
7
 
9
8
 
@@ -90,20 +89,26 @@ def grab_code_blocks(docstring, lang="python"):
90
89
  docstring: the docstring to analyse
91
90
  lang: if not None, the language that is assigned to the codeblock
92
91
  """
92
+ docstring = format_docstring(docstring)
93
93
  docstring = textwrap.dedent(docstring)
94
94
  in_block = False
95
95
  block = ""
96
96
  codeblocks = []
97
97
  for idx, line in enumerate(docstring.split("\n")):
98
- if line.startswith("```"):
98
+ if "```" in line:
99
99
  if in_block:
100
100
  codeblocks.append(check_codeblock(block, lang=lang))
101
101
  block = ""
102
102
  in_block = not in_block
103
103
  if in_block:
104
104
  block += line + "\n"
105
- return [c for c in codeblocks if c != ""]
105
+ return [textwrap.dedent(c) for c in codeblocks if c != ""]
106
106
 
107
+ def format_docstring(docstring):
108
+ """Formats docstring to be able to successfully go through dedent."""
109
+ if docstring[:1] != "\n":
110
+ return f"\n {docstring}"
111
+ return docstring
107
112
 
108
113
  def check_docstring(obj, lang=""):
109
114
  """
@@ -1,5 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mktestdocs
3
- Version: 0.2.1
4
- Provides-Extra: test
3
+ Version: 0.2.3
5
4
  License-File: LICENSE
5
+ Provides-Extra: test
6
+ Requires-Dist: pytest>=4.0.2; extra == "test"
@@ -11,5 +11,6 @@ mktestdocs.egg-info/top_level.txt
11
11
  tests/test_actual_docstrings.py
12
12
  tests/test_class.py
13
13
  tests/test_codeblock.py
14
+ tests/test_format_docstring.py
14
15
  tests/test_markdown.py
15
16
  tests/test_mktestdocs.py
@@ -0,0 +1,74 @@
1
+ import pytest
2
+ from mktestdocs import check_docstring
3
+
4
+
5
+ def foobar_good(a, b):
6
+ """
7
+ Returns a + b.
8
+
9
+ Examples:
10
+
11
+ ```python
12
+ import random
13
+
14
+ random.random()
15
+ assert 'a' + 'b' == 'ab'
16
+ assert 1 + 2 == 3
17
+ ```
18
+ """
19
+ pass
20
+
21
+
22
+ def foobar_also_good(a, b):
23
+ """
24
+ ```python
25
+ import random
26
+
27
+ assert random.random() < 10
28
+ ```
29
+ """
30
+ pass
31
+
32
+
33
+ def foobar_bad(a, b):
34
+ """
35
+ ```python
36
+ assert foobar(1, 2) == 4
37
+ ```
38
+ """
39
+ pass
40
+
41
+
42
+ def admonition_edge_cases():
43
+ """
44
+ !!! note
45
+
46
+ All cells of a table are initialized with an empty string. Therefore, to delete the content of a cell,
47
+ you need to assign an empty string, i.e. `''`. For instance, to delete the first row after the header:
48
+
49
+ ```python
50
+ assert 1 + 2 == 3
51
+ ```"""
52
+ pass
53
+
54
+ def adminition_edge_case_bad():
55
+ """Test that we can handle the edge cases of admonitions."""
56
+ example = """!!! note
57
+
58
+ Another one.
59
+ ```python
60
+ assert 1 + 2 == 4
61
+ ```"""
62
+ pass
63
+
64
+ @pytest.mark.parametrize("func", [foobar_good, foobar_also_good, admonition_edge_cases])
65
+ def test_base_docstrings(func):
66
+ check_docstring(func)
67
+
68
+
69
+ @pytest.mark.parametrize("func", [foobar_bad, adminition_edge_case_bad])
70
+ def test_base_docstrings_bad(func, capsys):
71
+ with pytest.raises(Exception):
72
+ check_docstring(func)
73
+ capsys.readouterr()
74
+ assert func.__name__ in capsys.readouterr().out
@@ -0,0 +1,61 @@
1
+ import pytest
2
+
3
+ from mktestdocs import get_codeblock_members
4
+ from mktestdocs.__main__ import format_docstring
5
+
6
+
7
+ def test_docstring_formatted():
8
+ # given (a docstring not prepared for dedent)
9
+ docstring = "Some class with a header and a code block"
10
+ # when (we go through the format_docstring function)
11
+ formatted = format_docstring(docstring)
12
+ # then (a new line and tab are added to the docstring)
13
+ assert formatted == "\n Some class with a header and a code block"
14
+
15
+ def test_docstring_not_formatted():
16
+ # given (a docstring prepared for dedent)
17
+ docstring = "\n Some class with a header and a code block"
18
+ # when (we go through the format_docstring function)
19
+ formatted = format_docstring(docstring)
20
+ # then (the docstring doesn't change)
21
+ assert formatted == docstring
22
+
23
+
24
+
25
+ # The docstring of the first class starts like
26
+ # """Some class ...
27
+ # The docstring of the second class starts like
28
+ # """
29
+ # Some class ...
30
+ # The tests are checking that regardless of how the class docstring starts we should always be able to read the tests
31
+ class BadClass:
32
+ """Some class with a header and a code block.
33
+
34
+ ```python
35
+ assert False, "this should fail."
36
+ ```
37
+
38
+ """
39
+ def __init__(self):
40
+ pass
41
+
42
+ class BadClassNewLine:
43
+ """
44
+ Some class with a header and a code block.
45
+
46
+ ```python
47
+ assert False, "this should fail."
48
+ ```
49
+
50
+ """
51
+ def __init__(self):
52
+ pass
53
+
54
+
55
+
56
+ @pytest.mark.parametrize("cls", [BadClass, BadClassNewLine], ids=lambda d: d.__qualname__)
57
+ def test_grab_bad_methods(cls):
58
+ bad_members = get_codeblock_members(cls)
59
+ assert len(bad_members) == 1
60
+
61
+
@@ -1,60 +0,0 @@
1
- import pytest
2
- from mktestdocs import check_docstring
3
-
4
-
5
- def foobar_good(a, b):
6
- """
7
- Returns a + b.
8
-
9
- Examples:
10
-
11
- ```python
12
- import random
13
-
14
- random.random()
15
- assert 'a' + 'b' == 'ab'
16
- assert 1 + 2 == 3
17
- ```
18
- """
19
- return a + b
20
-
21
-
22
- def foobar_also_good(a, b):
23
- """
24
- Returns a + b.
25
-
26
- Examples:
27
-
28
- ```python
29
- import random
30
-
31
- assert random.random() < 10
32
- ```
33
- """
34
- return a + b
35
-
36
-
37
- def foobar_bad(a, b):
38
- """
39
- Returns a + b.
40
-
41
- Examples:
42
-
43
- ```python
44
- assert foobar(1, 2) == 4
45
- ```
46
- """
47
- return a + b
48
-
49
-
50
- @pytest.mark.parametrize("func", [foobar_good, foobar_also_good])
51
- def test_base_docstrings(func):
52
- check_docstring(func)
53
-
54
-
55
- @pytest.mark.parametrize("func", [foobar_bad])
56
- def test_base_docstrings_bad(func, capsys):
57
- with pytest.raises(Exception):
58
- check_docstring(func)
59
- capsys.readouterr()
60
- assert func.__name__ in capsys.readouterr().out
File without changes
File without changes
File without changes