Skip to content

Commit

Permalink
Code block index relative to each docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
freider committed Oct 22, 2024
1 parent 9dd77a0 commit 9c49506
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/pytest_markdown_docs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ def extract_options_from_mdx_comment(comment: str) -> typing.Set[str]:
def find_object_tests_recursive(
module_name: str, object: typing.Any
) -> typing.Generator[
typing.Tuple[typing.Any, typing.Tuple[str, typing.List[str], int]], None, None
typing.Tuple[int, typing.Any, typing.Tuple[str, typing.List[str], int]], None, None
]:
docstr = inspect.getdoc(object)

if docstr:
for code_block in extract_code_blocks(docstr):
yield object, code_block
for i, code_block in enumerate(extract_code_blocks(docstr)):
yield i, object, code_block

for member_name, member in inspect.getmembers(object):
if member_name.startswith("_"):
Expand All @@ -253,17 +253,19 @@ def collect(self):
# but unsupported before 8.1...
module = import_path(self.path, root=self.config.rootpath)

for i, (obj, (test_code, fixture_names, start_line)) in enumerate(
find_object_tests_recursive(module.__name__, module)
):
for code_block_idx, obj, (
test_code,
fixture_names,
start_line,
) in find_object_tests_recursive(module.__name__, module):
obj_name = (
getattr(obj, "__qualname__", None)
or getattr(obj, "__name__", None)
or "<Unnamed obj>"
)
yield MarkdownInlinePythonItem.from_parent(
self,
name=f"{obj_name}[CodeBlock#{i+1}][rel.line:{start_line}]",
name=f"{obj_name}[CodeBlock#{code_block_idx+1}][rel.line:{start_line}]",
code=test_code,
fixture_names=fixture_names,
start_line=start_line,
Expand Down

0 comments on commit 9c49506

Please sign in to comment.