Skip to content

Commit

Permalink
test: add code fences tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oniboni committed May 17, 2024
1 parent b6aa162 commit 709f7b6
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 29 deletions.
3 changes: 3 additions & 0 deletions tests/data/template/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test

$code_block
8 changes: 8 additions & 0 deletions tests/data/template/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
site_name: Test pages for mkdocs-kroki-plugin

theme:
name: material

plugins:
- kroki:
FencePrefix: ''
186 changes: 186 additions & 0 deletions tests/test_fences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import pytest

from tests.utils import MkDocsTemplateHelper

TEST_CASES = {
"#35": """
```` plantuml
stuff containing ```
````
""",
"https://spec.commonmark.org/0.31.2/#example-119": """
``` mermaid
<
>
```
""",
"https://spec.commonmark.org/0.31.2/#example-120": """
~~~ mermaid
<
>
~~~
""",
"https://spec.commonmark.org/0.31.2/#example-122": """
``` mermaid
aaa
~~~
```
""",
"https://spec.commonmark.org/0.31.2/#example-123": """
~~~ mermaid
aaa
```
~~~
""",
"https://spec.commonmark.org/0.31.2/#example-125": """
~~~~ mermaid
aaa
~~~
~~~~
""",
"https://spec.commonmark.org/0.31.2/#example-129": """
``` mermaid
```
""",
"https://spec.commonmark.org/0.31.2/#example-130": """
``` mermaid
```
""",
}

TEST_CASES_NOT_SUPPORTED = {
"https://spec.commonmark.org/0.31.2/#example-121": """
`` mermaid
foo
``
""",
"https://spec.commonmark.org/0.31.2/#example-124": """
```` mermaid
aaa
```
``````
""",
"https://spec.commonmark.org/0.31.2/#example-126": """
``` mermaid
""",
"https://spec.commonmark.org/0.31.2/#example-127": """
````` mermaid
```
aaa
""",
"https://spec.commonmark.org/0.31.2/#example-128": """
> ``` mermaid
> aaa
bbb
""",
"https://spec.commonmark.org/0.31.2/#example-131": """
``` mermaid
aaa
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-132": """
```
aaa
aaa
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-133": """
```
aaa
aaa
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-135": """
```
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-136": """
```
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-140": """
foo
```
bar
```
baz
""",
"https://spec.commonmark.org/0.31.2/#example-141": """
foo
---
~~~
bar
~~~
# baz
""",
"https://spec.commonmark.org/0.31.2/#example-146": """
~~~ aa ``` ~~~
foo
~~~
""",
"https://spec.commonmark.org/0.31.2/#example-147": """
```
``` aaa
```
""",
}

TEST_CASES_ESCAPED = {
"https://spec.commonmark.org/0.31.2/#example-134": """
```
aaa
```
""",
}


@pytest.mark.parametrize("test_code_block", [pytest.param(v, id=k) for k, v in TEST_CASES.items()])
@pytest.mark.usefixtures("kroki_dummy")
def test_fences(test_code_block) -> None:
with MkDocsTemplateHelper(test_code_block) as mkdocs_helper:
mkdocs_helper.set_http_method("POST")
result = mkdocs_helper.invoke_build()

assert result.exit_code == 0
image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg"))
assert len(image_files) == 1


@pytest.mark.parametrize(
"test_code_block",
[pytest.param(v, id=k) for k, v in TEST_CASES_NOT_SUPPORTED.items()]
+ [pytest.param(v, id=k) for k, v in TEST_CASES_ESCAPED.items()],
)
@pytest.mark.usefixtures("kroki_dummy")
def test_fences_not_supported(test_code_block) -> None:
with MkDocsTemplateHelper(test_code_block) as mkdocs_helper:
mkdocs_helper.set_http_method("POST")
result = mkdocs_helper.invoke_build()

assert result.exit_code == 0
image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg"))
assert len(image_files) == 0


@pytest.mark.usefixtures("kroki_dummy")
def test_pandoc_fenced_code_blocks() -> None:
with MkDocsTemplateHelper("""~~~~~~~~~~~~~~~~ mermaid
~~~~~~~~~~
code including tildes
~~~~~~~~~~
~~~~~~~~~~~~~~~~""") as mkdocs_helper:
mkdocs_helper.set_http_method("POST")
result = mkdocs_helper.invoke_build()

assert result.exit_code == 0
image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg"))
assert len(image_files) == 1
25 changes: 0 additions & 25 deletions tests/test_whitespace.py

This file was deleted.

18 changes: 14 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import os
import pathlib
import shutil
import tempfile
from contextlib import AbstractContextManager
from pathlib import Path
from string import Template
from typing import Literal

import yaml
Expand All @@ -13,8 +13,6 @@

from tests.compat import chdir

logging.basicConfig(level=logging.INFO)


class NoPluginEntryError(ValueError):
def __init__(self) -> None:
Expand All @@ -26,6 +24,7 @@ def __init__(self, test_case: str) -> None:
self.config_file = None
self.test_case = test_case
self.test_dir = Path(tempfile.mkdtemp())
self._copy_test_case()

def _copy_test_case(self) -> None:
# equals to `../data`, using current source file as a pin
Expand All @@ -41,7 +40,6 @@ def _dump_config(self) -> None:
yaml.safe_dump(self.config_file, file)

def __enter__(self) -> "MkDocsHelper":
self._copy_test_case()
self._load_config()

return self
Expand Down Expand Up @@ -69,3 +67,15 @@ def invoke_build(self) -> Result:
runner = CliRunner()
with chdir(self.test_dir):
return runner.invoke(build_command)


class MkDocsTemplateHelper(MkDocsHelper):
def _substitute_code_block(self, code_block: str):
with open(self.test_dir / "docs/index.md") as in_file:
file_content = Template(in_file.read())
with open(self.test_dir / "docs/index.md", "w") as out_file:
out_file.write(file_content.substitute(code_block=code_block))

def __init__(self, code_block: str) -> None:
super().__init__("template")
self._substitute_code_block(code_block)

0 comments on commit 709f7b6

Please sign in to comment.