Skip to content

Commit

Permalink
Add a simple test for docstrings in the module
Browse files Browse the repository at this point in the history
  • Loading branch information
mwydmuch committed Jun 19, 2023
1 parent 003d55e commit e8d5817
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
57 changes: 30 additions & 27 deletions scripts/create_python_docstrings_from_docs.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python3

import os
import sys
import re


FILES_TO_PARSE = [
{"filepath": "doc/DoomGame.md", "namespace": "DoomGamePython"},
{"filepath": "doc/Utilities.md"},
Expand All @@ -12,23 +11,23 @@
RAW_STRING_ESCAPE_SEQ = "DOCSTRING"
FUNCTION_HEADER_REGEX = r"^### *<a *name=\"([a-zA-Z]+)\"><\/a> *`([a-zA-Z]+)` *$"
LINES_TO_IGNORE_REGEXES = [
r"---", # Lines
r"^\|.+\|$", # Tables
r"^See also:.*$", # See also
r"- \[.*\]\(.*\)", # List of links starting with -
r"\* \[.*\]\(.*\)", # List of links starting with *
r"^Added in .*$", # Added in annotations
r"^Changed in .*$", # Changed in annotations
r"^Deprecated since .*$", # Deprecated since annotations
r"^Removed in .*$", # Removed in annotations
r"^Config key: .*$", # Config annotations
r"---", # Lines
r"^\|.+\|$", # Tables
r"^See also:.*$", # See also
r"- \[.*\]\(.*\)", # List of links starting with -
r"\* \[.*\]\(.*\)", # List of links starting with *
r"^Added in .*$", # Added in annotations
r"^Changed in .*$", # Changed in annotations
r"^Deprecated since .*$", # Deprecated since annotations
r"^Removed in .*$", # Removed in annotations
r"^Config key: .*$", # Config annotations
]


if __name__ == "__main__":
with open(OUTPUT_FILE, "w") as output_file:
output_file.write(
f"""
"""
/*
This file is autogenerated by scripts/create_python_docstrings_from_docs.py
Do not edit it manually. Edit the Markdown files instead and regenerate it.
Expand All @@ -40,13 +39,14 @@
namespace vizdoom {{
namespace docstrings {{
""")

"""
)

for file in FILES_TO_PARSE:
if "namespace" in file:
output_file.write(f"namespace {file['namespace']} {{\n\n")

with open(file["filepath"], "r") as f:
with open(file["filepath"]) as f:
lines = f.readlines()

started = False
Expand All @@ -57,28 +57,31 @@
if match:
if started:
next_docstring = next_docstring.strip()
next_docstring += f"){RAW_STRING_ESCAPE_SEQ}\";\n\n"
next_docstring += f'){RAW_STRING_ESCAPE_SEQ}";\n\n'
output_file.write(next_docstring)

next_docstring = ""
function_name = match.group(1)
output_file.write(f" const char *{function_name} = R\"{RAW_STRING_ESCAPE_SEQ}(")
output_file.write(
f' const char *{function_name} = R"{RAW_STRING_ESCAPE_SEQ}('
)
started = True

elif started:
if not any (re.match(r, line) for r in LINES_TO_IGNORE_REGEXES):
# Filter out some lines
if not any(re.match(r, line) for r in LINES_TO_IGNORE_REGEXES):
next_docstring += line

if started:
output_file.write(f"){RAW_STRING_ESCAPE_SEQ}\";\n\n")

if "namespace" in file:
output_file.write(f"}} // namespace {file['namespace']}\n\n")
output_file.write(f'){RAW_STRING_ESCAPE_SEQ}";\n\n')

if "namespace" in file:
output_file.write(f"}} // namespace {file['namespace']}\n\n")

output_file.write(
"""
"""
} // namespace docstrings
} // namespace vizdoom
#endif
""")
"""
)
18 changes: 18 additions & 0 deletions tests/test_docstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3

import vizdoom as vzd


def test_docstrings(object):
object_methods = [
method_name
for method_name in dir(object)
if callable(getattr(object, method_name))
]

for method in object_methods:
assert method.__doc__ is not None, f"Method {method} has no docstring"


if __name__ == "__main__":
test_docstrings(vzd.DoomGame)

0 comments on commit e8d5817

Please sign in to comment.