Skip to content

Commit

Permalink
add test for path length
Browse files Browse the repository at this point in the history
  • Loading branch information
R4chel committed Mar 2, 2023
1 parent 8b0e30a commit ded3a6e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion tests/test_circle.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
from typing import Tuple

import numpy as np
import pytest

import vsketch

from .utils import bounds_equal, line_count_equal
from .utils import bounds_equal, length_equal, line_count_equal


def test_circle_default(vsk: vsketch.Vsketch) -> None:
# default should be diameter
vsk.detail(0.01) # make sure we have a tight bound match
vsk.circle(0, 0, 5)
assert line_count_equal(vsk, 1)
assert length_equal(vsk, 5 * np.pi)
assert bounds_equal(vsk, -2.5, -2.5, 2.5, 2.5)


def test_circle_radius(vsk: vsketch.Vsketch) -> None:
vsk.circle(0, 0, radius=5)
assert line_count_equal(vsk, 1)
assert length_equal(vsk, 5 * 2 * np.pi)
assert bounds_equal(vsk, -5, -5, 5, 5)


def test_circle_diameter(vsk: vsketch.Vsketch) -> None:
vsk.detail(0.01) # make sure we have a tight bound match
vsk.circle(0, 0, diameter=5)
assert line_count_equal(vsk, 1)
assert length_equal(vsk, 5 * np.pi)
assert bounds_equal(vsk, -2.5, -2.5, 2.5, 2.5)


Expand Down
4 changes: 3 additions & 1 deletion tests/test_line.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from .utils import bounds_equal, line_count_equal
from .utils import bounds_equal, length_equal, line_count_equal, line_exists


def test_line(vsk):
vsk.line(5, 5, 10, 5)
assert line_count_equal(vsk, 1)
assert line_exists(vsk, [5 + 5j, 10 + 5j])
assert length_equal(vsk, 5)
assert bounds_equal(vsk, 5, 5, 10, 5)
3 changes: 2 additions & 1 deletion tests/test_rect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import vsketch

from .utils import bounds_equal, line_count_equal, line_exists
from .utils import bounds_equal, length_equal, line_count_equal, line_exists


def test_rect_default_success(vsk: vsketch.Vsketch) -> None:
vsk.rect(0, 0, 2, 4)
assert line_count_equal(vsk, 1)
assert line_exists(vsk, np.array([0, 2, 2 + 4j, 4j, 0], dtype=complex), strict=False)
assert length_equal(vsk, (2+4)*2)


@pytest.mark.parametrize(
Expand Down
11 changes: 11 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def bounds_equal(
)


def length_equal(
vsk: vsketch.Vsketch, length: float
) -> bool:
"""Asserts that sketch length is approximately equal to those provided"""

length_ = vsk.document.length()
return bool(
length is not None
and np.isclose(length_, length, rtol=1e-03)
)

def line_count_equal(vsk: vsketch.Vsketch, *args: Union[int, Tuple[int, int]]) -> bool:
"""Asserts that layers have the given number of path. Any number of path count can be
passed as argument, either as single int (layer ID is then inferred or as (layer_ID, n)
Expand Down

0 comments on commit ded3a6e

Please sign in to comment.