I was writing a test that a reviewer asked for on one PR. The test failed for a reason that had nothing to do with that PR.

That accident became storytold/cadcraft#2602, in CADCraft, an open source CAD app (software for precise technical drawings) written in Rust.

The bug, shown in SVG

In SVG, any shape can carry a transform: translate moves it, rotate turns it, scale stretches it, skewX slants it. Groups (<g>) can carry one too, which applies to everything inside.

CADCraft's importer applied the group ones and quietly skipped the ones on shapes. So this:

<rect x="1" y="1" width="4" height="3" transform="translate(5,2)"/> 

landed at x 1 to 5 instead of 6 to 10. Every shape kind did the same: rect, circle, ellipse, line, polyline, polygon and path. The SVG spec (1.1, §7.5) is clear that every graphics element takes a transform.

How the test found it

The test was for #2202, my PR that adds rounded rectangle corners. FredApps asked for tests of each corner rule, including a rectangle under a skew, where round corners should turn oval. I put the skew transform straight on the rect. The rectangle came back not skewed at all. Corners weren't the problem. The transform was never read.

Why

The reader keeps a stack of transforms. Entering a group pushes "everything above me, then my transform", and leaving the group pops it. Each shape reads the top entry when it draws. But shapes are handled by a separate function, on_element, which never checked for a transform of its own.

The fix

Do the group trick for one element, then undo it:

let local = attr(e, b"transform").as_deref().and_then(parse_transform); if let Some(local) = local { self.stack.push(self.cur().then_before(local)); } let read = self.shape(name, e); if local.is_some() { self.stack.pop(); } read 

The pop lives here because the closing tag handler only pops for groups. And the result goes into read instead of using Rust's early return ?, because an early return would skip the pop and leave a stray transform on the stack for every shape after it. The seven shape readers stayed untouched.

The part I'd reuse anywhere: an answer key you already have

Hand writing expected coordinates for every shape under every transform would be slow and easy to get wrong. But group transforms already worked. A shape with transform="T" should come out exactly like the same shape inside <g transform="T">.

It's like checking a new calculator by typing the same sums into an old one you trust. You don't need to know the answers, only that both agree.

So the test does that for 7 shapes times 5 transforms, 35 pairs, and compares the imported geometry. It also checks a rotated circle stays a circle, a skewed one turns into an ellipse, the element's transform comes after its parent group's, the <line></line> form works, and the next shape isn't moved. Red on main, green with the fix.

Review

austin62jake merged it onto current main with six other PRs, ran cargo xtask ci, and approved, calling it "the smallest correct change" and noting the push and pop stay balanced even when a shape returns an error. It went in on 2 November 2026, merged by tanish2k09. And thanks to FredApps for the test request that led here.

The lesson

If the behaviour you need already works somewhere else in the code, compare against it. You skip working out every expected value by hand, and the comparison covers every combination you list.

Have you ever used one part of your code as the answer key (a test oracle) for another? What did it catch?

The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/cadcraft-svg-shape-transform