Skip to content

Commit

Permalink
(#3) Add tests to cover GameObject methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Apr 15, 2021
1 parent 0b67510 commit 7066c84
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
44 changes: 31 additions & 13 deletions src/test/java/unittest/testcases/graphics/shapes/Model2DTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import io.github.lucasstarsz.fastj.graphics.gameobject.shapes.Model2D;
import io.github.lucasstarsz.fastj.graphics.gameobject.shapes.Polygon2D;

import java.awt.geom.AffineTransform;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
Expand Down Expand Up @@ -67,16 +69,24 @@ public void checkModel2DCreation_withPolygon2DArrayParam_andRandomlyGeneratedSho

boolean shouldRender = Maths.randomBoolean();
Pointf randomTranslation = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-50f, 50f);
Pointf randomScale = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-5000f, 5000f);
float expectedNormalizedRotation = randomRotation % 360;

Model2D model2D = new Model2D(polygons, randomTranslation, randomRotation, randomScale, shouldRender);

AffineTransform expectedTransform = new AffineTransform();
expectedTransform.setToScale(randomScale.x, randomScale.y);
expectedTransform.setToRotation(Math.toRadians(randomRotation), model2D.getCenter().x, model2D.getCenter().y);
expectedTransform.setToTranslation(randomTranslation.x, randomTranslation.y);

assertArrayEquals(polygons, model2D.getPolygons(), "The created model's Polygon2D array should match the original Polygon2D array.");
assertEquals(shouldRender, model2D.shouldRender(), "The created model's 'show' option should match the default show option.");
assertEquals(randomTranslation, model2D.getTranslation(), "The created model's translation should match the default translation.");
assertEquals(randomRotation, model2D.getRotation(), "The created model's rotation should match the default rotation.");
assertEquals(randomScale, model2D.getScale(), "The created model's scaling should match the default scale.");
assertEquals(shouldRender, model2D.shouldRender(), "The created model's 'show' option should match the random show option.");
assertEquals(randomTranslation, model2D.getTranslation(), "The created model's translation should match the random translation.");
assertEquals(randomRotation, model2D.getRotation(), "The created model's rotation should match the random rotation.");
assertEquals(expectedNormalizedRotation, model2D.getRotationWithin360(), "The created model's normalized rotation should match the normalized rotation.");
assertEquals(expectedTransform, model2D.getTransformation(), "The created polygon's generated transform should match the expected transform.");
assertEquals(randomScale, model2D.getScale(), "The created model's scaling should match the random scale.");
}

@Test
Expand All @@ -91,20 +101,28 @@ public void checkModel2DCreation_withPolygon2DArrayParam_andRandomlyGeneratedSho

boolean shouldRender = Maths.randomBoolean();
Pointf randomTranslation = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-50f, 50f);
Pointf randomScale = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-5000f, 5000f);
float expectedNormalizedRotation = randomRotation % 360;

Model2D model2D = (Model2D) new Model2D(polygons)
.setTranslation(randomTranslation)
.setRotation(randomRotation)
.setScale(randomScale)
.setShouldRender(shouldRender);

AffineTransform expectedTransform = new AffineTransform();
expectedTransform.setToScale(randomScale.x, randomScale.y);
expectedTransform.setToRotation(Math.toRadians(randomRotation), model2D.getCenter().x, model2D.getCenter().y);
expectedTransform.setToTranslation(randomTranslation.x, randomTranslation.y);

assertArrayEquals(polygons, model2D.getPolygons(), "The created model's Polygon2D array should match the original Polygon2D array.");
assertEquals(shouldRender, model2D.shouldRender(), "The created model's 'show' option should match the default show option.");
assertEquals(randomTranslation, model2D.getTranslation(), "The created model's translation should match the default translation.");
assertEquals(randomRotation, model2D.getRotation(), "The created model's rotation should match the default rotation.");
assertEquals(randomScale, model2D.getScale(), "The created model's scaling should match the default scale.");
assertEquals(shouldRender, model2D.shouldRender(), "The created model's 'show' option should match the expected show option.");
assertEquals(randomTranslation, model2D.getTranslation(), "The created model's translation should match the expected translation.");
assertEquals(randomRotation, model2D.getRotation(), "The created model's rotation should match the expected rotation.");
assertEquals(expectedNormalizedRotation, model2D.getRotationWithin360(), "The created model's normalized rotation should match the normalized rotation.");
assertEquals(expectedTransform, model2D.getTransformation(), "The created polygon's generated transform should match the expected transform.");
assertEquals(randomScale, model2D.getScale(), "The created model's scaling should match the expected scale.");
}

@Test
Expand Down Expand Up @@ -157,7 +175,7 @@ public void checkModel2DTranslation_shouldMatchExpected() {
public void checkModel2DRotation_aroundOrigin_shouldMatchExpected() {
Pointf[] square1 = DrawUtil.createBox(Pointf.Origin, 50f);
Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.Origin, 25f), 50f);
float randomRotation = Maths.random(-50f, 50f);
float randomRotation = Maths.random(-5000f, 5000f);

Polygon2D[] expectedPolygons = {
new Polygon2D(square1),
Expand All @@ -182,7 +200,7 @@ public void checkModel2DRotation_aroundModelCenter_shouldMatchExpected() {
Pointf[] square1 = DrawUtil.createBox(Pointf.Origin, 50f);
Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.Origin, 25f), 50f);
Pointf expectedModelCenter = Pointf.subtract(square2[2], square1[0]).divide(2f).add(square1[0]);
float randomRotation = Maths.random(-50f, 50f);
float randomRotation = Maths.random(-5000f, 5000f);

Polygon2D[] expectedPolygons = {
new Polygon2D(square1),
Expand All @@ -207,7 +225,7 @@ public void checkModel2DRotation_aroundRandomCenter_shouldMatchExpected() {
Pointf[] square1 = DrawUtil.createBox(Pointf.Origin, 50f);
Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.Origin, 25f), 50f);
Pointf randomCenter = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-50f, 50f);
float randomRotation = Maths.random(-5000f, 5000f);

Polygon2D[] expectedPolygons = {
new Polygon2D(square1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.lucasstarsz.fastj.graphics.gameobject.shapes.Polygon2D;

import java.awt.Color;
import java.awt.geom.AffineTransform;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -58,16 +59,24 @@ public void checkPolygon2DCreation_withPointfArrayParam_andRandomlyGeneratedColo
boolean shouldRender = Maths.randomBoolean();

Pointf randomTranslation = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-50f, 50f);
Pointf randomScale = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-5000f, 5000f);
float expectedNormalizedRotation = randomRotation % 360;

Polygon2D polygon2D = new Polygon2D(square, randomTranslation, randomRotation, randomScale, randomColor, shouldFill, shouldRender);

AffineTransform expectedTransform = new AffineTransform();
expectedTransform.setToScale(randomScale.x, randomScale.y);
expectedTransform.setToRotation(Math.toRadians(randomRotation), polygon2D.getCenter().x, polygon2D.getCenter().y);
expectedTransform.setToTranslation(randomTranslation.x, randomTranslation.y);

assertEquals(randomColor, polygon2D.getColor(), "The created polygon's color should match the randomly generated color.");
assertEquals(shouldFill, polygon2D.isFilled(), "The created polygon's 'fill' option should match the randomly generated fill option.");
assertEquals(shouldRender, polygon2D.shouldRender(), "The created polygon's 'show' option should match the randomly generated show option.");
assertEquals(randomTranslation, polygon2D.getTranslation(), "The created polygon's translation should match the randomly generated translation.");
assertEquals(randomRotation, polygon2D.getRotation(), "The created polygon's rotation should match the randomly generated rotation.");
assertEquals(expectedNormalizedRotation, polygon2D.getRotationWithin360(), "The created model's normalized rotation should match the normalized rotation.");
assertEquals(expectedTransform, polygon2D.getTransformation(), "The created polygon's generated transform should match the expected transform.");
assertEquals(randomScale, polygon2D.getScale(), "The created polygon's scaling should match the randomly generated scale.");
assertArrayEquals(square, polygon2D.getOriginalPoints(), "The created polygon's Pointf array should match the original Pointf array.");
}
Expand All @@ -81,8 +90,9 @@ public void checkPolygon2DCreation_withPointfArrayParam_andRandomlyGeneratedColo
boolean shouldRender = Maths.randomBoolean();

Pointf randomTranslation = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-50f, 50f);
Pointf randomScale = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
float randomRotation = Maths.random(-5000f, 5000f);
float expectedNormalizedRotation = randomRotation % 360;

Polygon2D polygon2D = (Polygon2D) new Polygon2D(square)
.setColor(randomColor)
Expand All @@ -92,11 +102,18 @@ public void checkPolygon2DCreation_withPointfArrayParam_andRandomlyGeneratedColo
.setScale(randomScale)
.setShouldRender(shouldRender);

AffineTransform expectedTransform = new AffineTransform();
expectedTransform.setToScale(randomScale.x, randomScale.y);
expectedTransform.setToRotation(Math.toRadians(randomRotation), polygon2D.getCenter().x, polygon2D.getCenter().y);
expectedTransform.setToTranslation(randomTranslation.x, randomTranslation.y);

assertEquals(randomColor, polygon2D.getColor(), "The created polygon's color should match the randomly generated color.");
assertEquals(shouldFill, polygon2D.isFilled(), "The created polygon's 'fill' option should match the randomly generated fill option.");
assertEquals(shouldRender, polygon2D.shouldRender(), "The created polygon's 'show' option should match the randomly generated show option.");
assertEquals(randomTranslation, polygon2D.getTranslation(), "The created polygon's translation should match the randomly generated translation.");
assertEquals(randomRotation, polygon2D.getRotation(), "The created polygon's rotation should match the randomly generated rotation.");
assertEquals(expectedNormalizedRotation, polygon2D.getRotationWithin360(), "The created model's normalized rotation should match the normalized rotation.");
assertEquals(expectedTransform, polygon2D.getTransformation(), "The created polygon's generated transform should match the expected transform.");
assertEquals(randomScale, polygon2D.getScale(), "The created polygon's scaling should match the randomly generated scale.");
assertArrayEquals(square, polygon2D.getOriginalPoints(), "The created polygon's Pointf array should match the original Pointf array.");
}
Expand Down

0 comments on commit 7066c84

Please sign in to comment.