From 8a64d8ae510aa32293009e6ee2e405071de48a53 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Fri, 27 Mar 2026 16:24:37 +0100 Subject: [PATCH] test: add ColorAttributes unit tests Add 16 tests covering constructor behavior, fill/stroke flags and colors, null color handling, ID constant, Attributes interface, toString output, and instance independence. Closes #7 --- .../attributes/ColorAttributesTest.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/test/java/ovh/gasser/newshapes/attributes/ColorAttributesTest.java diff --git a/src/test/java/ovh/gasser/newshapes/attributes/ColorAttributesTest.java b/src/test/java/ovh/gasser/newshapes/attributes/ColorAttributesTest.java new file mode 100644 index 0000000..f5a454b --- /dev/null +++ b/src/test/java/ovh/gasser/newshapes/attributes/ColorAttributesTest.java @@ -0,0 +1,129 @@ +package ovh.gasser.newshapes.attributes; + +import org.junit.jupiter.api.Test; + +import java.awt.*; + +import static org.junit.jupiter.api.Assertions.*; + +class ColorAttributesTest { + + @Test + void testConstructorStoresFilledFlag() { + ColorAttributes attrs = new ColorAttributes(true, false, Color.RED, Color.BLACK); + assertTrue(attrs.filled, "filled flag should be true when constructed with true"); + } + + @Test + void testConstructorStoresStrokedFlag() { + ColorAttributes attrs = new ColorAttributes(false, true, Color.RED, Color.BLACK); + assertTrue(attrs.stroked, "stroked flag should be true when constructed with true"); + } + + @Test + void testConstructorStoresFilledColor() { + ColorAttributes attrs = new ColorAttributes(true, false, Color.BLUE, Color.BLACK); + assertEquals(Color.BLUE, attrs.filledColor, "filledColor should match the constructor argument"); + } + + @Test + void testConstructorStoresStrokedColor() { + ColorAttributes attrs = new ColorAttributes(false, true, Color.RED, Color.GREEN); + assertEquals(Color.GREEN, attrs.strokedColor, "strokedColor should match the constructor argument"); + } + + @Test + void testFilledAndStrokedBothTrue() { + ColorAttributes attrs = new ColorAttributes(true, true, Color.RED, Color.BLUE); + assertTrue(attrs.filled); + assertTrue(attrs.stroked); + } + + @Test + void testFilledAndStrokedBothFalse() { + ColorAttributes attrs = new ColorAttributes(false, false, Color.RED, Color.BLUE); + assertFalse(attrs.filled); + assertFalse(attrs.stroked); + } + + @Test + void testNullFilledColor() { + ColorAttributes attrs = new ColorAttributes(true, false, null, Color.BLACK); + assertNull(attrs.filledColor, "filledColor should accept null"); + } + + @Test + void testNullStrokedColor() { + ColorAttributes attrs = new ColorAttributes(false, true, Color.RED, null); + assertNull(attrs.strokedColor, "strokedColor should accept null"); + } + + @Test + void testBothColorsNull() { + ColorAttributes attrs = new ColorAttributes(false, false, null, null); + assertNull(attrs.filledColor); + assertNull(attrs.strokedColor); + } + + @Test + void testGetIDReturnsCorrectValue() { + ColorAttributes attrs = new ColorAttributes(false, false, Color.RED, Color.BLACK); + assertEquals(ColorAttributes.ID, attrs.getID()); + } + + @Test + void testIDConstant() { + assertEquals("COLOR_ATTRS", ColorAttributes.ID); + } + + @Test + void testImplementsAttributes() { + ColorAttributes attrs = new ColorAttributes(false, false, Color.RED, Color.BLACK); + assertInstanceOf(Attributes.class, attrs); + } + + @Test + void testToStringContainsAllFields() { + ColorAttributes attrs = new ColorAttributes(true, false, Color.RED, Color.BLUE); + String str = attrs.toString(); + assertTrue(str.contains("filled=true"), "toString should contain filled value"); + assertTrue(str.contains("stroked=false"), "toString should contain stroked value"); + assertTrue(str.contains("filledColor"), "toString should contain filledColor"); + assertTrue(str.contains("strokedColor"), "toString should contain strokedColor"); + } + + @Test + void testToStringWithNullColors() { + ColorAttributes attrs = new ColorAttributes(false, false, null, null); + String str = attrs.toString(); + assertNotNull(str, "toString should not throw with null colors"); + assertTrue(str.contains("filledColor=null"), "toString should show null filledColor"); + assertTrue(str.contains("strokedColor=null"), "toString should show null strokedColor"); + } + + @Test + void testTwoInstancesAreIndependent() { + ColorAttributes attrs1 = new ColorAttributes(true, false, Color.RED, Color.BLACK); + ColorAttributes attrs2 = new ColorAttributes(false, true, Color.BLUE, Color.GREEN); + + assertTrue(attrs1.filled); + assertFalse(attrs2.filled); + assertFalse(attrs1.stroked); + assertTrue(attrs2.stroked); + assertEquals(Color.RED, attrs1.filledColor); + assertEquals(Color.BLUE, attrs2.filledColor); + } + + @Test + void testFieldsAreImmutable() { + Color fillColor = Color.RED; + Color strokeColor = Color.BLACK; + ColorAttributes attrs = new ColorAttributes(true, true, fillColor, strokeColor); + + // Since fields are final, verify they retain their values + assertEquals(Color.RED, attrs.filledColor); + assertEquals(Color.BLACK, attrs.strokedColor); + assertTrue(attrs.filled); + assertTrue(attrs.stroked); + } +}