Skip to content

Commit

Permalink
Add Pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
daizutabi committed Jun 20, 2020
1 parent 849df0e commit e66aefd
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]

## [1.0.8] - 2020-06-20
### Added
- `short` filter to remove the prefix from an object name. ([#16](https://github.com/daizutabi/mkapi/pull/16)). Thanks to [Ahrak](https://github.com/Ahrak).

Expand Down
1 change: 1 addition & 0 deletions docs/usage/inherit.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Taking a look at this example, you may notice that:
* In the `Base`, there is no description for `type`.
* In the `Item`, parameters inherited from the superclass are not written.
* In the `Item.set_name()`, Parameters section itself doesn't exist.
* In the `Base.set()`, `{class}` variable is used to replace it by class name.

## Inheritance from Superclasses

Expand Down
8 changes: 4 additions & 4 deletions docs/usage/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def to_str(self, x: int) -> str:
len(docstring.sections) # type:ignore
# -
section = docstring.sections[0] # type:ignore
section.name, section.markdown
section.name
# -
print(section.markdown)
# -
section = docstring.sections[1] # type:ignore
section.name, section.markdown

# The `members` attribute gives children, for example, bound methods of a class.

Expand Down Expand Up @@ -108,7 +108,7 @@ def to_str(self, x: int) -> str:

from markdown import Markdown # isort:skip

converter = Markdown()
converter = Markdown(extensions=['admonition'])
html = converter.convert(markdown)
print(html)

Expand Down
4 changes: 4 additions & 0 deletions examples/inherit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def set_name(self, name: str):
"""
self.name = name

def get(self):
"""Returns {class} instace."""
return self


@dataclass
class Item(Base):
Expand Down
2 changes: 1 addition & 1 deletion mkapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.7"
__version__ = "1.0.8"

from mkapi.core.module import get_module
from mkapi.core.node import get_node
Expand Down
4 changes: 1 addition & 3 deletions mkapi/core/attribute.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""This module provides attributes inspection from source code."""
"""This module provides functions that inspect attributes from source code."""
import ast
import importlib
import inspect
Expand Down Expand Up @@ -90,8 +90,6 @@ def get_description(lines: List[str], lineno: int) -> str:
return "\n".join(docs).strip()
elif in_doc:
docs.append(line)
if docs:
return "\n".join(docs).strip()
return ""


Expand Down
2 changes: 1 addition & 1 deletion mkapi/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@dataclass(repr=False)
class Module(Tree):
"""Module class represents an module.
"""Module class represents a module.
Attributes:
parent: Parent Module instance.
Expand Down
2 changes: 0 additions & 2 deletions mkapi/core/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ def sort(node: Node):


def transform(node: Node, filters: Optional[List[str]] = None):
if node.docstring is None:
return
if node.object.kind in ["class", "dataclass"]:
transform_class(node, filters)
elif node.object.kind in ["module", "package"]:
Expand Down
2 changes: 1 addition & 1 deletion mkapi/core/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def admonition(name: str, markdown: str) -> str:
type = "warning"
else:
type = name.lower()
lines = [" " + line for line in markdown.split("\n")]
lines = [" " + line if line else "" for line in markdown.split("\n")]
lines.insert(0, f'!!! {type} "{name}"')
return "\n".join(lines)
2 changes: 1 addition & 1 deletion mkapi/core/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def render_docstring(self, docstring: Docstring, filters: List[str] = None) -> s
Args:
docstring: Docstring instance.
"""
if docstring is None:
if not docstring:
return ""
template = self.templates["docstring"]
for section in docstring.sections:
Expand Down
2 changes: 1 addition & 1 deletion mkapi/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __repr__(self):
return f"{class_name}({id!r}, num_sections={sections}, num_members={numbers})"

def __getitem__(self, index: Union[int, str, List[str]]):
"""Returns a member Tree instance.
"""Returns a member {class} instance.
If `index` is str, a member Tree instance whose name is equal to `index`
is returned.
Expand Down
15 changes: 13 additions & 2 deletions tests/core/test_core_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,16 @@ def func(self):

def test_multiple_assignments():
attrs = get_attributes(E)
assert attrs['a'] == (int, 'a')
assert attrs['b'] == (str, 'b')
assert attrs["a"] == (int, "a")
assert attrs["b"] == (str, "b")


def test_name_error():
abc = "abc"

class Name:
def __init__(self):
self.x: abc = 1 #: abc.

attrs = get_attributes(Name)
assert attrs["x"] == ("abc", "abc.")
9 changes: 8 additions & 1 deletion tests/core/test_core_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from mkapi.core.base import Docstring, Item, Section
from mkapi.core.base import Base, Docstring, Item, Section


def test_update_item():
Expand Down Expand Up @@ -38,3 +38,10 @@ def test_docstring_copy():
d.set_section(a, copy=True)
assert "Arguments" in d
assert d["Arguments"] is not a


def test_copy():
x = Base("x", 'markdown')
y = x.copy()
assert y.name == 'x'
assert y.markdown == 'markdown'
18 changes: 17 additions & 1 deletion tests/core/test_core_inherit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
from itertools import product

import pytest

from mkapi.core.base import Base, Inline
from mkapi.core.inherit import is_complete
from mkapi.core.inherit import get_section, is_complete
from mkapi.core.node import Node


def test_is_complete():
assert is_complete(Node(Base))
assert not is_complete(Node(Inline))


@pytest.mark.parametrize(
"name, mode", product(["Parameters", "Attributes"], ["Docstring", "Signature"])
)
def test_get_section(name, mode):
def func():
pass

section = get_section(Node(func), name, mode)
section.name == name
assert not section
12 changes: 6 additions & 6 deletions tests/core/test_core_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def test_transform_property(node):

def test_get_type(node):
assert P.get_type(node).name == ""
assert P.get_type(node['f']).name == "str"
assert P.get_type(node['g']).name == "int"
assert P.get_type(node['a']).name == "(int, str)"
assert P.get_type(node['b']).name == "str"
node['g'].docstring.sections[1]
assert P.get_type(node["f"]).name == "str"
assert P.get_type(node["g"]).name == "int"
assert P.get_type(node["a"]).name == "(int, str)"
assert P.get_type(node["b"]).name == "str"
node["g"].docstring.sections[1]


def test_transform_class(node):
Expand Down Expand Up @@ -97,6 +97,6 @@ def test_link_from_toc():
P.transform(node)
assert len(node.docstring.sections) == 5
assert "Methods" in node.docstring
section = node.docstring['Methods']
section = node.docstring["Methods"]
html = section.items[0].html
assert '<a href="#examples.google_style.ExampleClass.message">' in html
11 changes: 10 additions & 1 deletion tests/core/test_core_preprocess.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mkapi.core.preprocess import convert
from mkapi.core.preprocess import admonition, convert

source = """ABC
Expand Down Expand Up @@ -32,3 +32,12 @@

def test_convert():
assert convert(source) == output


def test_admonition():
markdown = admonition("Warnings", "abc\n\ndef")
assert markdown == '!!! warning "Warnings"\n abc\n\n def'
markdown = admonition("Note", "abc\n\ndef")
assert markdown == '!!! note "Note"\n abc\n\n def'
markdown = admonition("Tips", "abc\n\ndef")
assert markdown == '!!! tips "Tips"\n abc\n\n def'
22 changes: 22 additions & 0 deletions tests/core/test_core_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from mkapi.core.code import get_code
from mkapi.core.module import get_module
from mkapi.core.renderer import renderer


def test_module_empty_filters():
module = get_module("mkapi.core.base")
m = renderer.render_module(module).split("\n")
assert m[0] == "# ![mkapi](mkapi.core.base|plain|link|sourcelink)"
assert m[2] == "## ![mkapi](mkapi.core.base.Base||link|sourcelink)"
assert m[3] == "## ![mkapi](mkapi.core.base.Inline||link|sourcelink)"
assert m[4] == "## ![mkapi](mkapi.core.base.Type||link|sourcelink)"
assert m[5] == "## ![mkapi](mkapi.core.base.Item||link|sourcelink)"


def test_code_empty_filters():
code = get_code("mkapi.core.base")
m = renderer.render_code(code)
assert '<span class="mkapi-object-prefix">mkapi.core.</span>' in m
assert '<span class="mkapi-object-name">base</span>' in m
assert '<span id="mkapi.core.base"></span>' in m
assert '<a class="mkapi-docs-link" href="../../mkapi.core.base">DOCS</a>' in m
4 changes: 2 additions & 2 deletions tests/plugins/test_plugins_mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ def test_plugins_mkdocs_build():
def run(command):
assert subprocess.run(command.split()).returncode == 0

if os.path.exists('docs/api'):
shutil.rmtree('docs/api')
if os.path.exists("docs/api"):
shutil.rmtree("docs/api")
run("mkdocs build")
2 changes: 0 additions & 2 deletions tests/test_main.py

This file was deleted.

0 comments on commit e66aefd

Please sign in to comment.