Skip to content

Commit

Permalink
test: Add basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Apr 6, 2023
1 parent ab492d1 commit 3ce5c0e
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 9 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,33 @@ from pathlib import Path
import resvg_py
from PIL import Image

r = resvg_py.Resvg()
options = resvg_py.SVGOptions()
r = resvg_py.Resvg(options)

with Path("resources/examples/svg/simple.svg").open("r") as f:
rendered = r.render(f.read(), 120, 120)
with Path("resources/examples/svg/octocat.svg").open("r") as f:
rendered = r.render(f.read(), 400, 400)
array = rendered.as_array()
im = Image.fromarray(array)
im.save("image.png")

```

## Development

### Build

```sh
maturin develop
```

### Test

```sh
pytest
```

To update the snapshots:

```sh
pytest --snapshot-update
```
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ dependencies = [
"Pillow",
]

[project.optional-dependencies]
dev = ["pytest", "pytest-snapshot"]

[tool.maturin]
python-source = "python"
features = ["pyo3/extension-module"]
Expand All @@ -25,7 +28,8 @@ select = ["ALL"]
target-version = "py37"

[tool.ruff.isort]
known-first-party = ["resvg"]
known-first-party = ["resvg_py"]

[tool.ruff.per-file-ignores]
"**/*.pyi" = ["D1"]
"tests/test_*.py" = ["D1"]
Binary file added resources/examples/png/octocat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions resources/examples/svg/octocat.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions resvg_py.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class SVGOptions:
def __init__(
self: SVGOptions,
*,
dpi: float,
default_width: float,
default_height: float,
resources_dir: os.PathLike | None,
dpi: float = 96.0,
default_width: float = 100.0,
default_height: float = 100.0,
resources_dir: os.PathLike | None = None,
) -> None: ...

class Resvg:
def __init__(self: Resvg, options: SVGOptions | None) -> None: ...
def __init__(self: Resvg, options: SVGOptions | None = None) -> None: ...
def render(self: Resvg, svg: str, width: int, height: int) -> RenderedImage: ...

class RenderedImage:
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Unit tests."""
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions tests/test_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Run the resvg-py example."""

from __future__ import annotations

import io
import typing as t
from pathlib import Path

import pytest
from PIL import Image

import resvg_py

if t.TYPE_CHECKING:
from pytest_snapshot.plugin import Snapshot


@pytest.mark.parametrize(
"svg_file",
[
pytest.param("resources/examples/svg/octocat.svg", id="octocat"),
],
)
def test_render(snapshot: Snapshot, svg_file: str) -> None:
options = resvg_py.SVGOptions()
r = resvg_py.Resvg(options)

with Path(svg_file).open("r") as input_file:
rendered = r.render(input_file.read(), 400, 400)
array = rendered.as_array()

with io.BytesIO() as png_file:
im = Image.fromarray(array)
im.save(png_file, format="PNG")
snapshot.assert_match(png_file.getvalue(), "output.png")

0 comments on commit 3ce5c0e

Please sign in to comment.