Skip to content

Commit

Permalink
Test multiline docstrings with line breaks
Browse files Browse the repository at this point in the history
Signed-off-by: Roberto Di Remigio <roberto.diremigio@gmail.com>
  • Loading branch information
robertodr committed Mar 27, 2019
1 parent 9c93a24 commit 287541e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 12 deletions.
10 changes: 6 additions & 4 deletions parselglossy/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def documentation_generator(

comment = (
".. raw:: html\n\n"
" <style> .red {color:#aa0060; font-weight:bold; font-size:18px} </style>\n\n"
" <style> .red {color:#aa0060; font-weight:bold; font-size:18px} </style>\n\n" # noqa: E501
".. role:: red\n\n"
".. This documentation was autogenerated using parselglossy."
" Editing by hand is not recommended.\n"
Expand All @@ -61,7 +61,7 @@ def documentation_generator(
header_fmt = (
"{comment:s}\n{markup:s}\n{header:s}\n{markup:s}\n\n"
"- Keywords without a default value are **required**.\n"
"- Default values are either explicit or computed from the value of other keywords in the input.\n"
"- Default values are either explicit or computed from the value of other keywords in the input.\n" # noqa: E501
"- Sections where all keywords have a default value can be omitted.\n"
"- Predicates, if present, are the functions run to validate user input.\n"
)
Expand All @@ -83,7 +83,9 @@ def document_keyword(keyword: JSONDict) -> str:
**Type** ``{2:s}``
"""

doc = kw_fmt.format(keyword["name"], keyword["docstring"], keyword["type"])
doc = kw_fmt.format(
keyword["name"], keyword["docstring"].replace("\n", " "), keyword["type"]
)

if "default" in keyword.keys():
doc += """
Expand Down Expand Up @@ -134,7 +136,7 @@ def rec_documentation_generator(template, *, level: int = 0) -> str:
:{0:s}: {1:s}
"""
for s in sections:
doc = fmt.format(s["name"], s["docstring"])
doc = fmt.format(s["name"], s["docstring"].replace("\n", " "))
doc += rec_documentation_generator(s, level=level + 1)
docs.extend(indent(doc, level))

Expand Down
11 changes: 9 additions & 2 deletions tests/api/docs_template.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
keywords:
- docstring: Title of the calculation.
- docstring: |
Title of the calculation. I had to write an extremely long documentation
string for this one, as it is extremely important to let you know that this
keyword will define the title of the calculation and, not having a default,
you will be required to set it!
name: title
type: str
- docstring: Some integer
Expand Down Expand Up @@ -39,7 +43,10 @@ sections:
type: bool
docstring: A cool bool
default: True
- docstring: nothing really
- docstring: |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec luctus elit
ut posuere dictum. Proin ipsum libero, maximus vitae placerat a, bibendum
viverra ante.
name: fooffa
keywords:
- name: dwigt
Expand Down
1 change: 0 additions & 1 deletion tests/cli/docs_template.yml

This file was deleted.

60 changes: 60 additions & 0 deletions tests/cli/docs_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
keywords:
- docstring: |
Title of the calculation. I had to write an extremely long documentation
string for this one, as it is extremely important to let you know that this
keyword will define the title of the calculation and, not having a default,
you will be required to set it!
name: title
type: str
- docstring: Some integer
name: an_integer
type: int
default: 15
- docstring: A list of floats
name: float_list
type: List[float]
default: [1.0, 2.0, 3.0]
predicates:
- "len(value) < 10"
- "max(value) < user['foo']['fooffa]['another_float']"
sections:
- docstring: brilliant
name: foo
keywords:
- default: Wham! Bang! Pow! Let's Rock Out!
docstring: Title of the calculation.
name: tragic
type: str
- name: a_float
type: float
docstring: A floating point number
predicates:
- "value < 35.0"
- "value in user['float_list']"
sections:
- docstring: A ba-bar section
name: Bar
keywords:
- default: Bobson Dugnutt
docstring: Title of the calculation.
name: amazing
type: str
- name: coolio
type: bool
docstring: A cool bool
default: True
- docstring: |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec luctus elit
ut posuere dictum. Proin ipsum libero, maximus vitae placerat a, bibendum
viverra ante.
name: fooffa
keywords:
- name: dwigt
docstring: An unusual name
type: str
predicates:
- "len(value) < 80"
- name: another_float
type: float
docstring: Another floating point number
default: "user['foo']['a_float'] * 2"
4 changes: 2 additions & 2 deletions tests/ref/dwigt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Dwigt Rortugal's guide to input parameters
- Predicates, if present, are the functions run to validate user input.

:red:`Keywords`
:title: Title of the calculation.
:title: Title of the calculation. I had to write an extremely long documentation string for this one, as it is extremely important to let you know that this keyword will define the title of the calculation and, not having a default, you will be required to set it!

**Type** ``str``

Expand Down Expand Up @@ -70,7 +70,7 @@ Dwigt Rortugal's guide to input parameters

**Default** ``True``

:fooffa: nothing really
:fooffa: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec luctus elit ut posuere dictum. Proin ipsum libero, maximus vitae placerat a, bibendum viverra ante.

:red:`Keywords`
:dwigt: An unusual name
Expand Down
4 changes: 2 additions & 2 deletions tests/ref/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Input parameters
- Predicates, if present, are the functions run to validate user input.

:red:`Keywords`
:title: Title of the calculation.
:title: Title of the calculation. I had to write an extremely long documentation string for this one, as it is extremely important to let you know that this keyword will define the title of the calculation and, not having a default, you will be required to set it!

**Type** ``str``

Expand Down Expand Up @@ -70,7 +70,7 @@ Input parameters

**Default** ``True``

:fooffa: nothing really
:fooffa: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec luctus elit ut posuere dictum. Proin ipsum libero, maximus vitae placerat a, bibendum viverra ante.

:red:`Keywords`
:dwigt: An unusual name
Expand Down
42 changes: 42 additions & 0 deletions tests/ref/simple.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.. raw:: html

<style> .red {color:#aa0060; font-weight:bold; font-size:18px} </style>

.. role:: red

.. This documentation was autogenerated using parselglossy. Editing by hand is not recommended.
================
Input parameters
================

- Keywords without a default value are **required**.
- Default values are either explicit or computed from the value of other keywords in the input.
- Sections where all keywords have a default value can be omitted.
- Predicates, if present, are the functions run to validate user input.

:red:`Keywords`
:title: Title of the calculation.

**Type** ``str``

:red:`Sections`
:foo: brilliant

:red:`Keywords`
:tragic: Title of the calculation.

**Type** ``str``

**Default** ``Wham! Bang! Pow! Let's Rock Out!``

:red:`Sections`
:Bar: A ba-bar section

:red:`Keywords`
:amazing: Title of the calculation.

**Type** ``str``

**Default** ``Bobson Dugnutt``

2 changes: 1 addition & 1 deletion tests/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def template():


def test_documentation(template):
doc_ref = Path(__file__).parent / Path("ref/input.rst")
doc_ref = Path(__file__).parent / Path("ref/simple.rst")
with doc_ref.open("r") as ref:
stuff = ref.read().rstrip("\n")

Expand Down

0 comments on commit 287541e

Please sign in to comment.