Skip to content

Commit

Permalink
Improved tests (#244)
Browse files Browse the repository at this point in the history
  - Added a `null` test project that does nothing
  - Improved the `module` to test further things
  - Redefined the `MardkowPage.is_rendered()` method
    as "not (source is included in target)"
  • Loading branch information
Laurent Franceschetti committed Sep 28, 2024
1 parent 6265d90 commit b76efef
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 9 deletions.
21 changes: 16 additions & 5 deletions test/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def get_frontmatter(text:str) -> tuple[str, dict]:
markdown = parts[2]
except IndexError:
markdown = ''
return (markdown, frontmatter, metadata)
return (markdown.strip(), frontmatter, metadata)
else:
return (text, '', {})

Expand All @@ -214,6 +214,9 @@ def find_in_html(html: str,
-------
The line where the pattern was found, or None
"""
if not isinstance(pattern, str):
pattern = str(pattern)

soup = BeautifulSoup(html, 'html.parser')

# Compile regex patterns with case-insensitive flag
Expand Down Expand Up @@ -505,14 +508,22 @@ def has_error(self) -> bool:
@property
def is_rendered(self) -> bool:
"""
"Rendered" means that the target markdown is different from the source.
"Rendered" means that the target markdown
is different from the source;
more accurately, that the source markdown is not
contained in the target markdown.
Hence "not rendered" covers these two cases:
Hence "not rendered" is a "nothing happened".
It covers these cases:
1. An order to render was given, but there where actually
NO jinja2 directives.
2. A jinja2 rendering has not taken place at all.
2. A jinja2 rendering has not taken place at all
(some order to exclude the page).
3. A header and/or footer were added (in `on_pre_page_macros()
or in `on_post_page_macro()`) but the text itself
was not modified.
"""
return self.markdown != self.source_page.markdown
return self.source_page.markdown not in self.markdown



Expand Down
2 changes: 1 addition & 1 deletion test/module/docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bingo: Hello
{% endfor %}


## Mkdocs.yaml file (portion)
## Mkdocs.yal file (portion)

```
{{ include_file('mkdocs.yml', 0, 5)}}
Expand Down
46 changes: 45 additions & 1 deletion test/module/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


import pytest
import re

from test.fixture import DocProject, find_after

Expand Down Expand Up @@ -39,7 +40,23 @@ def test_pages():
assert VARIABLE_NAME in PROJECT.config.extra
price = PROJECT.config.extra.unit_price



# check the page metadata
# those metadata are not in the config file
metadata = page.metadata
assert 'user' in metadata
assert 'bottles' in metadata
assert 'announcement' in metadata

assert metadata.user == 'Joe'
assert page.find(metadata.user, header='Installed', header_level=4)
assert page.find(metadata.announcement, header='Accessing meta')
assert page.find(metadata.bottles.lemonade, header='Dot notation')
assert not page.find(metadata.user * 2, header='Macro') # negative test

assert 'bottles' not in PROJECT.config.extra
assert 'bottles' not in PROJECT.variables

# check that the `greeting` variable is rendered:
assert VARIABLE_NAME in PROJECT.variables
assert f"{price} euros" in page.markdown
Expand All @@ -58,7 +75,34 @@ def test_pages():
# ----------------
page = PROJECT.get_page('environment')

# read a few things that are in the tables
assert page.find('unit_price = 50', header='General list')
# there are two headers containing 'Macros':
assert page.find('say_hello', header='Macros$')


# test the `include_file()` method (used for the mkdocs.yaml file)
HEADER = r"^mkdocs.*portion"
assert page.find('site_name:', header=HEADER)
assert page.find('name: material', header=HEADER)
assert not page.find('foobar 417', header=HEADER) # negative control

# ----------------
# Literal page
# ----------------
page = PROJECT.get_page('literal')
# instruction not to render:
assert page.metadata.render_macros == False

assert page.is_rendered == False, f"Target: {page.markdown}, \nSource:{page.source_page.markdown}"

# Latex is not interpreted:
latex = re.escape(r"\begin{tabular}{|ccc|}")
assert page.find(latex, header='Offending Latex')

# Footer is processed (but not rendered)
assert page.find(r'now()', header='Pre-macro')
assert page.find('Not interpreted', header='Post-macro')


def test_strict():
Expand Down
4 changes: 4 additions & 0 deletions test/null/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
This __init__.py file is indispensable for pytest to
recognize its packages.
"""
7 changes: 7 additions & 0 deletions test/null/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Main Page

This project does not contain any Jinja2.

This is to test the simplest case possible.


3 changes: 3 additions & 0 deletions test/null/docs/second.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Second page

It does nothing special either (no Jinja2)
14 changes: 14 additions & 0 deletions test/null/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
site_name: Null test case (no Jinja2)
theme: mkdocs

nav:
- Home: index.md
- Next page: second.md

plugins:
- search
- macros

extra:
greeting: Hello World!

54 changes: 54 additions & 0 deletions test/null/test_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Testing the project
(C) Laurent Franceschetti 2024
"""


import pytest

from test.fixture import DocProject

CURRENT_PROJECT = 'simple'



def test_pages():
PROJECT = DocProject(CURRENT_PROJECT)
build_result = PROJECT.build(strict=False)
# did not fail
return_code = PROJECT.build_result.returncode
assert not return_code, "Failed when it should not"


# ----------------
# First page
# ----------------


page = PROJECT.get_page('index')
assert not page.is_rendered
assert not page.has_error



# ----------------
# Second page
# ----------------
# there is intentionally an error (`foo` does not exist)
page = PROJECT.get_page('second')

assert not page.is_rendered
assert not page.has_error

def test_strict():
"This project must fail"
PROJECT = DocProject(CURRENT_PROJECT)

# it must not fail with the --strict option,
PROJECT.build(strict=True)
assert not PROJECT.build_result.returncode, "Failed when it should not"




4 changes: 2 additions & 2 deletions test/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
find_in_html)


@click.command()

def test_low_level_fixtures():
"Test the low level fixtures"

Expand Down Expand Up @@ -119,7 +119,7 @@ def test_low_level_fixtures():
print(find_in_html(html_doc, 'under the main', header='Main header'))
print(find_in_html(html_doc, 'under the', header='sub header'))

@click.command()

def test_high_level_fixtures():
"""
Test a project
Expand Down

0 comments on commit b76efef

Please sign in to comment.