Skip to content

Commit

Permalink
more tests. catch duplicated entries in DocGuideSections
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Feb 2, 2024
1 parent 0ea680f commit 07c0f3a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 36 deletions.
32 changes: 27 additions & 5 deletions mathics/doc/common_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,18 @@ def __init__(self, part, title, doc=None):
print(" DEBUG Creating Chapter", title)

def __str__(self) -> str:
sections = "\n".join(section.title for section in self.sections)
return f"= {self.part.title}: {self.title} =\n\n{sections}"
"""
A DocChapter is represented as the index of its sections
and subsections.
"""
sections_descr = ""
for section in self.all_sections:
sec_class = "@>" if isinstance(section, DocGuideSection) else "@ "
sections_descr += f" {sec_class} " + section.title + "\n"
for subsection in section.subsections:
sections_descr += " * " + subsection.title + "\n"

return f" = {self.part.title}: {self.title} =\n\n{sections_descr}"

@property
def all_sections(self):
Expand Down Expand Up @@ -521,7 +531,7 @@ def __init__(self, doc, title, is_reference=False):
print("DEBUG Creating Part", title)

def __str__(self) -> str:
return f"{self.title}\n\n" + "\n".join(
return f" Part {self.title}\n\n" + "\n\n".join(
str(chapter) for chapter in sorted_chapters(self.chapters)
)

Expand Down Expand Up @@ -575,7 +585,7 @@ def __lt__(self, other) -> bool:
return self.title < other.title

def __str__(self) -> str:
return f"== {self.title} ==\n{self.doc}"
return f" == {self.title} ==\n{self.doc}"


class DocTests:
Expand Down Expand Up @@ -635,6 +645,7 @@ def __init__(self):
self.parts = []
self.appendix = []
self.parts_by_slug = {}
self.title = "Title"

def _set_classes(self):
"""
Expand All @@ -649,6 +660,12 @@ def _set_classes(self):
self.section_class = DocSection
self.subsection_class = DocSubsection

def __str__(self):
result = self.title + "\n" + len(self.title) * "~" + "\n"
return (
result + "\n\n".join([str(part) for part in self.parts]) + "\n" + 60 * "-"
)

def get_part(self, part_slug):
return self.parts_by_slug.get(part_slug)

Expand Down Expand Up @@ -935,6 +952,7 @@ def __init__(self, want_sorting=False):
self.doc_data_file = settings.get_doctest_latex_data_path(
should_be_readable=True
)
self.title = "Mathics Main Documentation"

def add_section(
self,
Expand Down Expand Up @@ -1190,6 +1208,10 @@ def load_documentation_sources(self):
# This is information from `mathics.builtin`.
# This is Part 2 of the documentation.

# Notice that in order to generate the documentation
# from the builtin classes, it is needed to call first to
# import_and_load_builtins()

for title, modules, builtins_by_module, start in [
(
"Reference of Built-in Symbols",
Expand Down Expand Up @@ -1312,7 +1334,7 @@ def _set_classes(self):
self.docText_class = DocText

def __str__(self) -> str:
return "\n".join(str(item) for item in self.items)
return "\n\n".join(str(item) for item in self.items)

def text(self) -> str:
# used for introspection
Expand Down
1 change: 1 addition & 0 deletions mathics/doc/latex/doc2latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def write_latex(
doc_data, quiet=False, filter_parts=None, filter_chapters=None, filter_sections=None
):
documentation = LaTeXMathicsDocumentation()
assert_non_duplicated(documentation)
if not quiet:
print(f"Writing LaTeX document to {DOC_LATEX_FILE}")
with open_ensure_dir(DOC_LATEX_FILE, "wb") as doc:
Expand Down
12 changes: 7 additions & 5 deletions mathics/doc/latex_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
SUBSECTION_RE,
TESTCASE_OUT_RE,
DocChapter,
DocGuideSection,
DocPart,
DocSection,
DocSubsection,
Expand Down Expand Up @@ -638,6 +639,7 @@ class LaTeXMathicsDocumentation(MathicsMainDocumentation):
def __init__(self, want_sorting=False):
super().__init__(want_sorting)
self.load_documentation_sources()
assert_non_duplicated(self)

def _set_classes(self):
"""
Expand Down Expand Up @@ -677,15 +679,12 @@ def latex(
filter_chapters=filter_chapters,
filter_sections=filter_sections,
)
print(" latex content:", text)
if part.is_appendix and not appendix:
appendix = True
text = "\n\\appendix\n" + text
parts.append(text)
result = "\n\n".join(parts)
print(" -> result", result)
result = post_process_latex(result)
print(" postprocess->", result)
return result


Expand All @@ -712,7 +711,10 @@ def latex(self, doc_data: dict, quiet=False, filter_sections=None) -> str:
"\\chaptersections\n",
"\n\n".join(
section.latex(doc_data, quiet)
for section in sorted(self.all_sections)
# Here we should use self.all_sections, but for some reason
# guidesections are not properly loaded, duplicating
# the load of subsections.
for section in sorted(self.sections)
if not filter_sections or section.title in filter_sections
),
"\n\\chapterend\n",
Expand Down Expand Up @@ -814,7 +816,7 @@ def latex(self, doc_data: dict, quiet=False) -> str:
return section_string


class LaTeXDocGuideSection(DocSection):
class LaTeXDocGuideSection(DocGuideSection):
"""An object for a Documented Guide Section.
A Guide Section is part of a Chapter. "Colors" or "Special Functions"
are examples of Guide Sections, and each contains a number of Sections.
Expand Down
20 changes: 20 additions & 0 deletions test/doc/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path as osp

from mathics.core.evaluation import Message, Print
from mathics.core.load_builtin import import_and_load_builtins
from mathics.doc.common_doc import (
DocChapter,
DocPart,
Expand Down Expand Up @@ -195,4 +196,23 @@ def test_load_documentation():


def test_load_mathics_documentation():
import_and_load_builtins()
documentation = MathicsMainDocumentation()
documentation.load_documentation_sources()

# Check that there are not repeated elements.
visited_parts = set([])
for part in documentation.parts:
assert part.title not in visited_parts
visited_chapters = set([])
for chapter in part.chapters:
assert chapter.title not in visited_chapters
visited_chapters.add(chapter.title)
visited_sections = set([])
for section in chapter.all_sections:
assert section.title not in visited_sections
visited_sections.add(section.title)
visited_subsections = set([])
for subsection in section.subsections:
assert subsection.title not in visited_subsections
visited_subsections.add(subsection.title)
79 changes: 53 additions & 26 deletions test/doc/test_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path as osp

from mathics.core.evaluation import Message, Print
from mathics.core.load_builtin import import_and_load_builtins
from mathics.doc.latex_doc import (
LaTeXDocChapter,
LaTeXDocPart,
Expand All @@ -17,35 +18,36 @@
)
from mathics.settings import DOC_DIR

# Load the documentation once.
import_and_load_builtins()
LATEX_DOCUMENTATION = LaTeXMathicsDocumentation()

TEST_DOC_DATA_DICT = {
(
"Manual",
"Further Tutorial Examples",
"Curve Sketching",
0,
): {
"query": "f[x_] := 4 x / (x ^ 2 + 3 x + 5)",
"results": [
{
"out": [],
"result": "o",
}
],
},
}


def test_load_latex_documentation():
ssss = LaTeXDocTest(
index=0,
testcase="f[x_] := 4 x / (x ^ 2 + 3 x + 5)",
key_prefix=(
"Manual",
"Further Tutorial Examples",
"Curve Sketching",
),
)
"""
Test the structure of the LaTeX Documentation
"""

documentation = LATEX_DOCUMENTATION
doc_data = TEST_DOC_DATA_DICT

documentation = LaTeXMathicsDocumentation()
doc_data = {
(
"Manual",
"Further Tutorial Examples",
"Curve Sketching",
0,
): {
"query": "f[x_] := 4 x / (x ^ 2 + 3 x + 5)",
"results": [
{
"out": [],
"result": "o",
}
],
},
}
part = documentation.get_part("manual")
assert isinstance(part, LaTeXDocPart)

Expand Down Expand Up @@ -92,3 +94,28 @@ def test_load_latex_documentation():
assert (
third_chapter.latex(doc_data)[:38]
).strip() == "\\chapter{Further Tutorial Examples}"


def test_chapter():
documentation = LATEX_DOCUMENTATION
part = documentation.parts[1]
chapter = part.chapters_by_slug["testing-expressions"]
section = chapter.sections_by_slug["numerical-properties"]
latex_section_head = section.latex({})[:63].strip()
assert (
latex_section_head
== "\section*{Numerical Properties}{\index{Numerical Properties}}"
)
print(60 * "@")
latex_chapter = chapter.latex({}, quiet=False)

count = 0
next_pos = 0
while True:
print(next_pos)
next_pos = latex_chapter.find(latex_section_head, next_pos + 64)
if next_pos == -1:
break
count += 1

assert count == 1, "The section is rendered twice"

0 comments on commit 07c0f3a

Please sign in to comment.