Skip to content

Commit b76efef

Browse files
author
Laurent Franceschetti
committed
Improved tests (#244)
- 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)"
1 parent 6265d90 commit b76efef

File tree

9 files changed

+146
-9
lines changed

9 files changed

+146
-9
lines changed

test/fixture.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def get_frontmatter(text:str) -> tuple[str, dict]:
191191
markdown = parts[2]
192192
except IndexError:
193193
markdown = ''
194-
return (markdown, frontmatter, metadata)
194+
return (markdown.strip(), frontmatter, metadata)
195195
else:
196196
return (text, '', {})
197197

@@ -214,6 +214,9 @@ def find_in_html(html: str,
214214
-------
215215
The line where the pattern was found, or None
216216
"""
217+
if not isinstance(pattern, str):
218+
pattern = str(pattern)
219+
217220
soup = BeautifulSoup(html, 'html.parser')
218221

219222
# Compile regex patterns with case-insensitive flag
@@ -505,14 +508,22 @@ def has_error(self) -> bool:
505508
@property
506509
def is_rendered(self) -> bool:
507510
"""
508-
"Rendered" means that the target markdown is different from the source.
511+
"Rendered" means that the target markdown
512+
is different from the source;
513+
more accurately, that the source markdown is not
514+
contained in the target markdown.
509515
510-
Hence "not rendered" covers these two cases:
516+
Hence "not rendered" is a "nothing happened".
517+
It covers these cases:
511518
1. An order to render was given, but there where actually
512519
NO jinja2 directives.
513-
2. A jinja2 rendering has not taken place at all.
520+
2. A jinja2 rendering has not taken place at all
521+
(some order to exclude the page).
522+
3. A header and/or footer were added (in `on_pre_page_macros()
523+
or in `on_post_page_macro()`) but the text itself
524+
was not modified.
514525
"""
515-
return self.markdown != self.source_page.markdown
526+
return self.source_page.markdown not in self.markdown
516527

517528

518529

test/module/docs/environment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bingo: Hello
1717
{% endfor %}
1818

1919

20-
## Mkdocs.yaml file (portion)
20+
## Mkdocs.yal file (portion)
2121

2222
```
2323
{{ include_file('mkdocs.yml', 0, 5)}}

test/module/test_site.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
import pytest
9+
import re
910

1011
from test.fixture import DocProject, find_after
1112

@@ -39,7 +40,23 @@ def test_pages():
3940
assert VARIABLE_NAME in PROJECT.config.extra
4041
price = PROJECT.config.extra.unit_price
4142

42-
43+
44+
# check the page metadata
45+
# those metadata are not in the config file
46+
metadata = page.metadata
47+
assert 'user' in metadata
48+
assert 'bottles' in metadata
49+
assert 'announcement' in metadata
50+
51+
assert metadata.user == 'Joe'
52+
assert page.find(metadata.user, header='Installed', header_level=4)
53+
assert page.find(metadata.announcement, header='Accessing meta')
54+
assert page.find(metadata.bottles.lemonade, header='Dot notation')
55+
assert not page.find(metadata.user * 2, header='Macro') # negative test
56+
57+
assert 'bottles' not in PROJECT.config.extra
58+
assert 'bottles' not in PROJECT.variables
59+
4360
# check that the `greeting` variable is rendered:
4461
assert VARIABLE_NAME in PROJECT.variables
4562
assert f"{price} euros" in page.markdown
@@ -58,7 +75,34 @@ def test_pages():
5875
# ----------------
5976
page = PROJECT.get_page('environment')
6077

78+
# read a few things that are in the tables
79+
assert page.find('unit_price = 50', header='General list')
80+
# there are two headers containing 'Macros':
81+
assert page.find('say_hello', header='Macros$')
82+
83+
84+
# test the `include_file()` method (used for the mkdocs.yaml file)
85+
HEADER = r"^mkdocs.*portion"
86+
assert page.find('site_name:', header=HEADER)
87+
assert page.find('name: material', header=HEADER)
88+
assert not page.find('foobar 417', header=HEADER) # negative control
89+
90+
# ----------------
91+
# Literal page
92+
# ----------------
93+
page = PROJECT.get_page('literal')
94+
# instruction not to render:
95+
assert page.metadata.render_macros == False
96+
97+
assert page.is_rendered == False, f"Target: {page.markdown}, \nSource:{page.source_page.markdown}"
98+
99+
# Latex is not interpreted:
100+
latex = re.escape(r"\begin{tabular}{|ccc|}")
101+
assert page.find(latex, header='Offending Latex')
61102

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

63107

64108
def test_strict():

test/null/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
This __init__.py file is indispensable for pytest to
3+
recognize its packages.
4+
"""

test/null/docs/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Main Page
2+
3+
This project does not contain any Jinja2.
4+
5+
This is to test the simplest case possible.
6+
7+

test/null/docs/second.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Second page
2+
3+
It does nothing special either (no Jinja2)

test/null/mkdocs.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
site_name: Null test case (no Jinja2)
2+
theme: mkdocs
3+
4+
nav:
5+
- Home: index.md
6+
- Next page: second.md
7+
8+
plugins:
9+
- search
10+
- macros
11+
12+
extra:
13+
greeting: Hello World!
14+

test/null/test_site.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Testing the project
3+
4+
(C) Laurent Franceschetti 2024
5+
"""
6+
7+
8+
import pytest
9+
10+
from test.fixture import DocProject
11+
12+
CURRENT_PROJECT = 'simple'
13+
14+
15+
16+
def test_pages():
17+
PROJECT = DocProject(CURRENT_PROJECT)
18+
build_result = PROJECT.build(strict=False)
19+
# did not fail
20+
return_code = PROJECT.build_result.returncode
21+
assert not return_code, "Failed when it should not"
22+
23+
24+
# ----------------
25+
# First page
26+
# ----------------
27+
28+
29+
page = PROJECT.get_page('index')
30+
assert not page.is_rendered
31+
assert not page.has_error
32+
33+
34+
35+
# ----------------
36+
# Second page
37+
# ----------------
38+
# there is intentionally an error (`foo` does not exist)
39+
page = PROJECT.get_page('second')
40+
41+
assert not page.is_rendered
42+
assert not page.has_error
43+
44+
def test_strict():
45+
"This project must fail"
46+
PROJECT = DocProject(CURRENT_PROJECT)
47+
48+
# it must not fail with the --strict option,
49+
PROJECT.build(strict=True)
50+
assert not PROJECT.build_result.returncode, "Failed when it should not"
51+
52+
53+
54+

test/test_fixture.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
find_in_html)
1717

1818

19-
@click.command()
19+
2020
def test_low_level_fixtures():
2121
"Test the low level fixtures"
2222

@@ -119,7 +119,7 @@ def test_low_level_fixtures():
119119
print(find_in_html(html_doc, 'under the main', header='Main header'))
120120
print(find_in_html(html_doc, 'under the', header='sub header'))
121121

122-
@click.command()
122+
123123
def test_high_level_fixtures():
124124
"""
125125
Test a project

0 commit comments

Comments
 (0)