fix: SText.updateMeasuredBounds() mutates defensive copy instead of internal state #32

Merged
thib8956 merged 1 commits from fix/issue-27-stext-update-measured-bounds into master 2026-03-27 23:22:21 +00:00
2 changed files with 37 additions and 1 deletions

View File

@@ -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

View File

@@ -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);
}
}