From a8b908ca1dad805917413020d02ecea2f96ef5eb Mon Sep 17 00:00:00 2001 From: Thibaud Date: Fri, 27 Mar 2026 23:56:42 +0100 Subject: [PATCH] fix: SText.updateMeasuredBounds() must mutate internal bounds, not defensive copy Closes #27 --- .../ovh/gasser/newshapes/shapes/SText.java | 2 +- .../gasser/newshapes/shapes/STextTest.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/ovh/gasser/newshapes/shapes/SText.java b/src/main/java/ovh/gasser/newshapes/shapes/SText.java index d7b7007..ea2ea0d 100644 --- a/src/main/java/ovh/gasser/newshapes/shapes/SText.java +++ b/src/main/java/ovh/gasser/newshapes/shapes/SText.java @@ -57,7 +57,7 @@ public class SText extends AbstractShape { } public void updateMeasuredBounds(int width, int height) { - getBounds().setSize(Math.max(width, 0), Math.max(height, 0)); + bounds.setSize(Math.max(width, 0), Math.max(height, 0)); } @Override diff --git a/src/test/java/ovh/gasser/newshapes/shapes/STextTest.java b/src/test/java/ovh/gasser/newshapes/shapes/STextTest.java index 5a49c7c..3987097 100644 --- a/src/test/java/ovh/gasser/newshapes/shapes/STextTest.java +++ b/src/test/java/ovh/gasser/newshapes/shapes/STextTest.java @@ -44,4 +44,40 @@ class STextTest { SText text = SText.create(0, 0, null); assertEquals(SText.PLACEHOLDER_TEXT, text.getText()); } + + @Test + void testUpdateMeasuredBoundsActuallyUpdatesBounds() { + SText text = SText.create(10, 20, "Hello"); + text.updateMeasuredBounds(200, 30); + Rectangle bounds = text.getBounds(); + assertEquals(200, bounds.width, "updateMeasuredBounds must update width"); + assertEquals(30, bounds.height, "updateMeasuredBounds must update height"); + } + + @Test + void testUpdateMeasuredBoundsDoesNotChangePosition() { + SText text = SText.create(10, 20, "Hello"); + text.updateMeasuredBounds(200, 30); + Rectangle bounds = text.getBounds(); + assertEquals(10, bounds.x, "updateMeasuredBounds must not change x"); + assertEquals(20, bounds.y, "updateMeasuredBounds must not change y"); + } + + @Test + void testUpdateMeasuredBoundsNegativeClampedToZero() { + SText text = SText.create(0, 0, "Hello"); + text.updateMeasuredBounds(-5, -10); + Rectangle bounds = text.getBounds(); + assertEquals(0, bounds.width, "negative width must be clamped to 0"); + assertEquals(0, bounds.height, "negative height must be clamped to 0"); + } + + @Test + void testUpdateMeasuredBoundsZeroIsAllowed() { + SText text = SText.create(0, 0, "Hello"); + text.updateMeasuredBounds(0, 0); + Rectangle bounds = text.getBounds(); + assertEquals(0, bounds.width); + assertEquals(0, bounds.height); + } }