Skip to content

Commit

Permalink
Remove old SVG cropping tests
Browse files Browse the repository at this point in the history
The new tests with fixtures cover the same cases
  • Loading branch information
jams2 authored and zerolab committed Jul 6, 2023
1 parent 5bf882e commit 8d7990c
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions tests/test_svg_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,24 +290,33 @@ def test_resize_preserves_original_image(self):


class SvgCropTestCase(SvgWrapperTestCase):
def test_simple_crop(self):
f = BytesIO(b'<svg width="42" height="42" viewBox="0 0 42 42"></svg>')
svg_image_file = Image.open(f)
cropped = svg_image_file.crop((5, 5, 15, 15))
self.assertEqual(cropped.get_size(), (10, 10))
self.assertEqual(cropped.image.view_box, ViewBox(5, 5, 10, 10))
def test_crop(self):
"""
Test cropping SVGs.
# Check the underlying etree has been updated
self.assertEqual(float(cropped.image.root.get("width")), 10)
self.assertEqual(float(cropped.image.root.get("height")), 10)
self.assertEqual(
[float(x) for x in cropped.image.root.get("viewBox").split()],
[5, 5, 10, 10],
)
This finds, recursively, all of the relevant SVG files in the
tests/images/svg/originals directory. For each file it finds, it:
- opens the file;
- performs a crop based on the filename; and
- compares the attributes on the cropped SVG in memory to the expected
cropped version stored in the corresponding tests/images/svg/results
subdirectory.
def test_crop_with_aspect_ratio_mismatch(self):
"""
Test cropping SVGs where the viewport aspect ratio does not match the viewBox aspect ratio.
An SVG named `some-name-crop-original.svg' will be cropped to the
dimensions of its original viewport.
A specific bounding rect for a crop can be specified in the file name
like this:
<some descriptive prefix>-crop-<left>_<top>_<right>_<bottom>.svg
For example, the file `my-svg-crop-0_10_20_30.svg' will be cropped to
the rect (left=0, top=10, right=20, bottom=30).
The cropping test files are organised into subdirectories by their
`preserveAspectRatio' value. To save duplication, files with specific
crop directives are symlinks to their corresponding 'original' crop
file.
"""

def get_original_crop_rect(svg_image):
Expand Down Expand Up @@ -356,23 +365,6 @@ def test_crop_preserves_original_image(self):
svg.crop((0, 0, 10, 10))
self.assertEqual(svg.image.view_box, ViewBox(0, 0, 42, 42))

def test_crop_with_transform(self):
# user coordinates scaled by 2 and translated 50 units south
svg = SvgImage(
self.get_svg_wrapper(
width=50,
height=100,
view_box="0 0 25 25",
preserve_aspect_ratio="xMidYMax meet",
)
)

cropped = svg.crop((10, 10, 20, 20))
self.assertEqual(cropped.get_size(), (10, 10))
self.assertEqual(cropped.image.view_box, ViewBox(5, -20, 5, 5))
self.assertEqual(float(cropped.image.root.get("width")), 10)
self.assertEqual(float(cropped.image.root.get("height")), 10)

def test_crop_throws(self):
f = BytesIO(b'<svg width="42" height="42" viewBox="0 0 42 42"></svg>')
svg_image_file = Image.open(f)
Expand Down

0 comments on commit 8d7990c

Please sign in to comment.