Skip to content

Commit

Permalink
(#10) simplify linear gradient exceptions, finished needed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jun 5, 2021
1 parent 2073827 commit 0124d91
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tech.fastj.graphics.util.gradients;

import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Boundary;
import tech.fastj.graphics.Drawable;
Expand Down Expand Up @@ -41,13 +40,10 @@ public class LinearGradientBuilder implements GradientBuilder {
*/
private void position(Drawable drawable, Boundary start, Boundary end) {
if (start.equals(end)) {
FastJEngine.error(
GradientBuilder.GradientCreationError,
new IllegalStateException(
"The starting and ending positions for a gradient must not be the same."
+ System.lineSeparator()
+ "Both positions evaluated to: " + start.name()
)
throw new IllegalArgumentException(
"The starting and ending positions for a gradient must not be the same."
+ System.lineSeparator()
+ "Both positions evaluated to: " + start.name()
);
}

Expand All @@ -63,13 +59,10 @@ private void position(Drawable drawable, Boundary start, Boundary end) {
*/
private void position(Pointf start, Pointf end) {
if (start.equals(end)) {
FastJEngine.error(
GradientBuilder.GradientCreationError,
new IllegalStateException(
"The starting and ending positions for a gradient must not be the same."
+ System.lineSeparator()
+ "Both positions evaluated to: " + start
)
throw new IllegalArgumentException(
"The starting and ending positions for a gradient must not be the same."
+ System.lineSeparator()
+ "Both positions evaluated to: " + start
);
}

Expand All @@ -86,10 +79,12 @@ private void position(Pointf start, Pointf end) {
* @return The {@code LinearGradientBuilder}, for method chaining.
*/
public LinearGradientBuilder withColor(Color color) {
if (count == Gradients.ColorLimit) {
FastJEngine.error(GradientBuilder.GradientCreationError,
new IllegalStateException("Gradients cannot contain more than " + Gradients.ColorLimit + " colors.")
);
if (count >= Gradients.ColorLimit) {
throw new IllegalStateException("Gradients cannot contain more than " + Gradients.ColorLimit + " colors.");
}

if (color == null) {
throw new IllegalArgumentException("Gradients cannot contain null color values.");
}

colors[count] = color;
Expand All @@ -107,20 +102,12 @@ public LinearGradientBuilder withColor(Color color) {
* @return The {@code LinearGradientBuilder}, for method chaining.
*/
public LinearGradientBuilder withColors(Color... colors) {
if (count == Gradients.ColorLimit) {
FastJEngine.error(GradientBuilder.GradientCreationError,
new IllegalStateException("Gradients cannot contain more than " + Gradients.ColorLimit + " colors.")
);
if (count + colors.length > Gradients.ColorLimit) {
throw new IllegalStateException("Gradients cannot contain more than " + Gradients.ColorLimit + " colors.");
}

if (Arrays.stream(colors).anyMatch(Objects::isNull)) {
FastJEngine.error(GradientBuilder.GradientCreationError,
new IllegalStateException(
"Gradients cannot contain null color values."
+ System.lineSeparator()
+ "Colors: " + Arrays.toString(colors)
)
);
throw new IllegalArgumentException("Gradients cannot contain null color values.");
}

System.arraycopy(colors, count, this.colors, 0, count + colors.length);
Expand All @@ -136,13 +123,10 @@ public LinearGradientBuilder withColors(Color... colors) {
@Override
public LinearGradientPaint build() {
if (count < 2) {
FastJEngine.error(
GradientBuilder.GradientCreationError,
new IllegalStateException(
"Gradients must contain at least 2 colors."
+ System.lineSeparator()
+ "This gradient builder only contains the following gradients: " + Arrays.toString(colors)
)
throw new IllegalStateException(
"Gradients must contain at least 2 colors."
+ System.lineSeparator()
+ "This gradient builder only contains the following gradients: " + Arrays.toString(Arrays.stream(colors).filter(Objects::nonNull).toArray())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import tech.fastj.math.Maths;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Boundary;
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.util.gradients.Gradients;
import tech.fastj.graphics.util.gradients.LinearGradientBuilder;

import java.awt.Color;
import java.awt.LinearGradientPaint;

import org.junit.jupiter.api.Test;
import unittest.mock.graphics.MockDrawable;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class LinearGradientTests {
Expand Down Expand Up @@ -664,4 +669,141 @@ void checkCreateLinearGradient_withEightRandomColors_usingRandomLocations_usingW
);
}
}

@Test
void tryCreateLinearGradient_butPositionUsesTheSamePointfsForStartAndEndParams() {
Pointf invalid_startAndEndPosition = randomPointf();
Throwable exception = assertThrows(IllegalArgumentException.class, () -> Gradients.linearGradient(invalid_startAndEndPosition, invalid_startAndEndPosition));

String expectedExceptionMessage = "The starting and ending positions for a gradient must not be the same."
+ System.lineSeparator()
+ "Both positions evaluated to: " + invalid_startAndEndPosition;
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void tryCreateLinearGradient_butPositionUsesTheSameBoundariesForStartAndEndParams() {
Drawable mockDrawable = new MockDrawable();
Boundary invalid_startAndEndPosition = Boundary.values()[Maths.randomInteger(0, Boundary.values().length - 1)];
Throwable exception = assertThrows(IllegalArgumentException.class, () -> Gradients.linearGradient(mockDrawable, invalid_startAndEndPosition, invalid_startAndEndPosition));

String expectedExceptionMessage = "The starting and ending positions for a gradient must not be the same."
+ System.lineSeparator()
+ "Both positions evaluated to: " + invalid_startAndEndPosition.name();
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void tryCreateLinearGradient_butTooManyColorsWereAdded_usingWithColor() {
Color randomColor1 = randomColor();
Color randomColor2 = randomColor();
Color randomColor3 = randomColor();
Color randomColor4 = randomColor();
Color randomColor5 = randomColor();
Color randomColor6 = randomColor();
Color randomColor7 = randomColor();
Color randomColor8 = randomColor();
Color invalid_randomColor9 = randomColor();

Pointf randomStartingPoint = randomPointf();
Pointf randomEndingPoint = randomPointf();

LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.withColor(randomColor3)
.withColor(randomColor4)
.withColor(randomColor5)
.withColor(randomColor6)
.withColor(randomColor7)
.withColor(randomColor8);

Throwable exception = assertThrows(IllegalStateException.class, () -> linearGradientBuilder.withColor(invalid_randomColor9));

String expectedExceptionMessage = "Gradients cannot contain more than " + Gradients.ColorLimit + " colors.";
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void tryCreateLinearGradient_butTooManyColorsWereAdded_usingWithColors() {
Color randomColor1 = randomColor();
Color randomColor2 = randomColor();
Color randomColor3 = randomColor();
Color randomColor4 = randomColor();
Color randomColor5 = randomColor();
Color randomColor6 = randomColor();
Color randomColor7 = randomColor();
Color randomColor8 = randomColor();
Color invalid_randomColor9 = randomColor();

Pointf randomStartingPoint = randomPointf();
Pointf randomEndingPoint = randomPointf();

LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColors(randomColor1, randomColor2, randomColor3, randomColor4, randomColor5, randomColor6, randomColor7, randomColor8);

Throwable exception = assertThrows(IllegalStateException.class, () -> linearGradientBuilder.withColors(invalid_randomColor9));

String expectedExceptionMessage = "Gradients cannot contain more than " + Gradients.ColorLimit + " colors.";
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void tryCreateLinearGradient_butAddedNullColor_usingWithColor() {
Color invalid_nullColor = null;

Pointf randomStartingPoint = randomPointf();
Pointf randomEndingPoint = randomPointf();

LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(randomStartingPoint, randomEndingPoint);
Throwable exception = assertThrows(IllegalArgumentException.class, () -> linearGradientBuilder.withColor(invalid_nullColor));

String expectedExceptionMessage = "Gradients cannot contain null color values.";
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void tryCreateLinearGradient_butAddedNullColor_usingWithColors() {
Color invalid_nullColor = null;

Pointf randomStartingPoint = randomPointf();
Pointf randomEndingPoint = randomPointf();

LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(randomStartingPoint, randomEndingPoint);
Throwable exception = assertThrows(IllegalArgumentException.class, () -> linearGradientBuilder.withColors(invalid_nullColor));

String expectedExceptionMessage = "Gradients cannot contain null color values.";
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void tryCreateLinearGradient_butTooFewColorsWereAddedToBuild_withColorCountOfZero() {
Pointf randomStartingPoint = randomPointf();
Pointf randomEndingPoint = randomPointf();

LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(randomStartingPoint, randomEndingPoint);
Throwable exception = assertThrows(IllegalStateException.class, linearGradientBuilder::build);

String expectedExceptionMessage = "Gradients must contain at least 2 colors."
+ System.lineSeparator()
+ "This gradient builder only contains the following gradients: []";
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void tryCreateLinearGradient_butTooFewColorsWereAddedToBuild_withColorCountOfOne() {
Color randomColor1 = randomColor();

Pointf randomStartingPoint = randomPointf();
Pointf randomEndingPoint = randomPointf();

LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1);
Throwable exception = assertThrows(IllegalStateException.class, linearGradientBuilder::build);

String expectedExceptionMessage = "Gradients must contain at least 2 colors."
+ System.lineSeparator()
+ "This gradient builder only contains the following gradients: [" + randomColor1 + "]";
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}
}

0 comments on commit 0124d91

Please sign in to comment.