Skip to content

One test sample list per shape enum

The tests that check a property on every shape draw their shapes from one list per shape enum, in tests/common/samples.rs: curves() for Curve (and ElementaryCurve through as_elementary), curves_2d() for Curve2D, surfaces() for Surface (and ElementarySurface). Each list holds at least one sample of every variant, in a tilted placement, plus the cases a single sample would miss: a rational periodic B-spline curve in each tier, a rational Bézier curve, extrusions of a line, a circle and a B-spline profile, revolutions of a circle and of a line skew to the axis, and rational B-spline and Bézier surfaces.

The compiler's list checks the samples. src/shape_enum/tests.rs includes samples.rs with #[path] and expands each enum's own variant list macro (ADR 0017) into a check that every variant it names matches at least one sample. A variant added to an enum is added to its list, since the forwarding matches do not compile otherwise, so cargo test then fails, naming the variant, until samples.rs has a sample of it. The include runs inside the crate because the list macros are crate-private; lib.rs declares extern crate self as geomcore under cfg(test) so the same file resolves geomcore::... both there and in the integration tests that use it through mod common.

The properties run over every sample. tests/shape_conformance.rs checks that the shared methods of the traits agree on every sample, on a trimmed piece of every curve and on the elementary enums: the tangent, the curvature and its centre, and the normal follow from the derivatives; the principal, mean and Gaussian curvatures agree; the batch methods return what the single ones do; a periodic shape is closed; and a point of the shape projects onto itself at distance 0, with contains and parameter_of agreeing. transform_and_reversal.rs (reversal round trip, a transform commuting with evaluation), bounding_box.rs (the box holds the shape, and exists exactly when the shape is bounded), combined_evaluation.rs and the generic tests of surface_reversal.rs take their shapes from the same lists, so each covers every variant where it used to cover a hand-picked subset that left the swept surfaces out. The four unit tests that compared each Surface method with the same method on the shape it holds are deleted: the forwarding is generated from the list, so they compared a call with itself. Their one real check, that the normal is the unit cross product of the first derivatives, moved to shape_conformance.rs and now runs on every surface.

One decoder for the fixtures. tests/common/shapes.rs decodes a curve or surface recorded in a fixture from its type tag into a Curve, Curve2D or Surface, and replays recorded samples, inversions and projections on any shape through the traits. vs_fixtures.rs and swept_fixtures.rs use it in place of a case struct and a checker per type. A fixture that groups its cases by kind instead of tagging them ("lines": [...]) is tagged as it is read, so the fixture files stay as recorded.

Considered options

  • An exhaustive match per enum in the tests, one arm per variant: rejected. The enums are #[non_exhaustive], so the match only checks inside the crate, and it is a second hand-written list beside the variant list macro, which is what the deleted dispatch tests were. Expanding the macro the forwarding already uses keeps one list.
  • Move the conformance tests into the crate: rejected. They exercise the public interface, which is what the integration tests are for (ARCHITECTURE.md), and the samples are useful to every integration test. Only the coverage check, which needs the crate-private macros, runs inside.
  • A sample list in tests/common with no coverage check: rejected. It is the hand list it replaces: a new variant would be absent from every property test without a sign.
  • Generate the samples from the variant list: rejected. A sample needs per-type construction arguments, so a macro would only move the list into its arguments.

Consequences

Adding a curve or surface type means adding a sample to samples.rs too; cargo test names the missing one. Every property test that takes the lists then covers it with no further edit. A test whose property only holds on some variants filters the list and says why, rather than keeping its own. A new fixture of a shape the decoder knows needs no struct or checker of its own.