package ovh.gasser.newshapes.shapes; import org.junit.jupiter.api.Test; import ovh.gasser.newshapes.App; import ovh.gasser.newshapes.attributes.ColorAttributes; import ovh.gasser.newshapes.attributes.SelectionAttributes; import java.awt.Color; import java.awt.Rectangle; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.*; class SCollectionTest { @Test void testCreateWithShapes() { SRectangle rect = SRectangle.create(10, 20, 100, 50); SCircle circle = SCircle.create(50, 50, 25); SCollection collection = SCollection.of(rect, circle); assertNotNull(collection); assertEquals(2, collection.stream().count()); } @Test void testAdd() { SCollection collection = SCollection.of(); SRectangle rect = SRectangle.create(10, 20, 100, 50); collection.add(rect); assertEquals(1, collection.stream().count()); assertSame(rect, collection.iterator().next()); } @Test void testRemove() { SRectangle rect = SRectangle.create(10, 20, 100, 50); SCollection collection = SCollection.of(rect); collection.remove(rect); assertEquals(0, collection.stream().count()); } @Test void testIterator() { SRectangle rect1 = SRectangle.create(10, 20, 100, 50); SRectangle rect2 = SRectangle.create(30, 40, 60, 70); SCollection collection = SCollection.of(rect1, rect2); Iterator iterator = collection.iterator(); assertTrue(iterator.hasNext()); assertSame(rect1, iterator.next()); assertTrue(iterator.hasNext()); assertSame(rect2, iterator.next()); assertFalse(iterator.hasNext()); } @Test void testStream() { SRectangle rect1 = SRectangle.create(10, 20, 100, 50); SRectangle rect2 = SRectangle.create(30, 40, 60, 70); SCircle circle = SCircle.create(50, 50, 25); SCollection collection = SCollection.of(rect1, rect2, circle); List shapes = collection.stream().collect(Collectors.toList()); assertEquals(3, shapes.size()); assertTrue(shapes.contains(rect1)); assertTrue(shapes.contains(rect2)); assertTrue(shapes.contains(circle)); } @Test void testGetBoundsEmptyCollection() { SCollection collection = SCollection.of(); Rectangle bounds = collection.getBounds(); assertEquals(App.WIN_SIZE.width, bounds.width); assertEquals(App.WIN_SIZE.height, bounds.height); } @Test void testGetBoundsWithChildren() { SRectangle rect1 = SRectangle.create(10, 10, 50, 50); SRectangle rect2 = SRectangle.create(100, 100, 80, 40); SCollection collection = SCollection.of(rect1, rect2); Rectangle bounds = collection.getBounds(); // Union should cover from (10,10) to (180,140) assertEquals(10, bounds.x); assertEquals(10, bounds.y); assertEquals(170, bounds.width); // 100+80-10 = 170 assertEquals(130, bounds.height); // 100+40-10 = 130 } @Test void testTranslate() { SRectangle rect = SRectangle.create(10, 20, 100, 50); SCollection collection = SCollection.of(rect); collection.translate(5, 10); Rectangle bounds = rect.getBounds(); assertEquals(15, bounds.x); assertEquals(30, bounds.y); } @Test void testClone() { SRectangle rect = SRectangle.create(10, 20, 100, 50); SCollection original = SCollection.of(rect); SCollection cloned = (SCollection) original.clone(); assertNotSame(original, cloned); assertEquals(original.stream().count(), cloned.stream().count()); // Verify SelectionAttributes is added to clone assertNotNull(cloned.getAttributes(SelectionAttributes.ID)); // Verify deep copy - modifying clone doesn't affect original Iterator clonedIterator = cloned.iterator(); Shape clonedChild = clonedIterator.next(); clonedChild.translate(100, 100); Rectangle originalBounds = rect.getBounds(); assertEquals(10, originalBounds.x); assertEquals(20, originalBounds.y); } @Test void testToString() { SCollection collection = SCollection.of(); String str = collection.toString(); assertTrue(str.contains("SCollection")); } @Test void testAddAttributesPropagatesToChildren() { SRectangle rect = SRectangle.create(10, 20, 100, 50); SCollection collection = SCollection.of(rect); ColorAttributes attrs = new ColorAttributes(true, true, Color.RED, Color.BLUE); collection.addAttributes(attrs); ColorAttributes childAttrs = (ColorAttributes) rect.getAttributes(ColorAttributes.ID); assertNotNull(childAttrs); assertEquals(Color.RED, childAttrs.filledColor); assertEquals(Color.BLUE, childAttrs.strokedColor); } @Test void testGetAttributesReturnsChildColor() { SRectangle rect = SRectangle.create(10, 20, 100, 50, Color.GREEN); SCircle circle = SCircle.create(50, 50, 25); SCollection collection = SCollection.of(rect, circle); ColorAttributes attrs = (ColorAttributes) collection.getAttributes(ColorAttributes.ID); assertNotNull(attrs); // First child's color should be returned (strokedColor for SRectangle) assertEquals(Color.GREEN, attrs.strokedColor); } }