-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#3) Add Testing for GameObject class
- Loading branch information
1 parent
0821237
commit 6cbbc06
Showing
4 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package unittest.mock; | ||
|
||
import io.github.lucasstarsz.fastj.math.Pointf; | ||
|
||
import io.github.lucasstarsz.fastj.graphics.gameobject.GameObject; | ||
|
||
import io.github.lucasstarsz.fastj.systems.behaviors.Behavior; | ||
|
||
public class MockBehavior implements Behavior { | ||
|
||
private Pointf pointf; | ||
|
||
@Override | ||
public void init(GameObject obj) { | ||
pointf = new Pointf(); | ||
} | ||
|
||
@Override | ||
public void update(GameObject obj) { | ||
pointf.add(1f); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
pointf = null; | ||
} | ||
|
||
public Pointf getPointf() { | ||
return pointf.copy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package unittest.mock; | ||
|
||
import io.github.lucasstarsz.fastj.systems.control.Scene; | ||
import io.github.lucasstarsz.fastj.systems.render.Display; | ||
|
||
public class MockScene extends Scene { | ||
|
||
public MockScene() { | ||
super(""); | ||
} | ||
|
||
@Override | ||
public void load(Display display) { | ||
} | ||
|
||
@Override | ||
public void unload(Display display) { | ||
} | ||
|
||
@Override | ||
public void update(Display display) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package unittest.mock.graphics; | ||
|
||
import io.github.lucasstarsz.fastj.math.Pointf; | ||
|
||
import io.github.lucasstarsz.fastj.graphics.gameobject.GameObject; | ||
|
||
import io.github.lucasstarsz.fastj.systems.control.Scene; | ||
|
||
import java.awt.Graphics2D; | ||
|
||
public class MockGameObject extends GameObject { | ||
@Override | ||
public void destroy(Scene originScene) { | ||
|
||
} | ||
|
||
@Override | ||
public Pointf getTranslation() { | ||
return GameObject.DefaultTranslation; | ||
} | ||
|
||
@Override | ||
public float getRotation() { | ||
return GameObject.DefaultRotation; | ||
} | ||
|
||
@Override | ||
public Pointf getScale() { | ||
return GameObject.DefaultScale; | ||
} | ||
|
||
@Override | ||
public void translate(Pointf translationMod) { | ||
} | ||
|
||
@Override | ||
public void rotate(float rotationMod, Pointf centerpoint) { | ||
} | ||
|
||
@Override | ||
public void scale(Pointf scaleMod, Pointf centerpoint) { | ||
} | ||
|
||
@Override | ||
public void render(Graphics2D g) { | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
src/test/java/unittest/testcases/graphics/GameObjectTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package unittest.testcases.graphics; | ||
|
||
import io.github.lucasstarsz.fastj.math.Maths; | ||
|
||
import io.github.lucasstarsz.fastj.graphics.gameobject.GameObject; | ||
|
||
import io.github.lucasstarsz.fastj.systems.behaviors.Behavior; | ||
import io.github.lucasstarsz.fastj.systems.control.Scene; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import unittest.mock.MockBehavior; | ||
import unittest.mock.MockScene; | ||
import unittest.mock.graphics.MockGameObject; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class GameObjectTests { | ||
|
||
@Test | ||
public void checkCreateGameObject_behaviorsShouldBeEmpty() { | ||
GameObject gameObject = new MockGameObject(); | ||
assertEquals(0, gameObject.getBehaviors().size(), "When initially created, a GameObject's behavior list should contain no behaviors."); | ||
} | ||
|
||
@Test | ||
public void checkAddBehaviorToGameObject_shouldAllowMultiple() { | ||
GameObject gameObject = new MockGameObject(); | ||
|
||
Behavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
int behaviorCount = 255; | ||
|
||
for (int i = 0; i < behaviorCount; i++) { | ||
gameObject.addBehavior(mockBehavior, mockScene); | ||
} | ||
|
||
assertEquals(behaviorCount, gameObject.getBehaviors().size(), "When a behavior is added multiple times, the GameObject's behavior count should increase."); | ||
} | ||
|
||
@Test | ||
public void checkClearBehaviorsFromGameObject_shouldRemoveAll() { | ||
GameObject gameObject = new MockGameObject(); | ||
|
||
Behavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
int behaviorCount = 255; | ||
|
||
for (int i = 0; i < behaviorCount; i++) { | ||
gameObject.addBehavior(mockBehavior, mockScene); | ||
} | ||
assertNotEquals(0, gameObject.getBehaviors().size()); | ||
|
||
gameObject.clearAllBehaviors(); | ||
assertEquals(0, gameObject.getBehaviors().size(), "After clearing all behaviors from the GameObject, it should not contain any behaviors."); | ||
} | ||
|
||
@Test | ||
public void checkRemoveBehaviorFromGameObject() { | ||
GameObject gameObject = new MockGameObject(); | ||
|
||
Behavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
int behaviorCount = 255; | ||
|
||
for (int i = 0; i < behaviorCount; i++) { | ||
gameObject.addBehavior(mockBehavior, mockScene); | ||
} | ||
|
||
int expectedBehaviorCount = 200; | ||
for (int i = 0; i < behaviorCount - expectedBehaviorCount; i++) { | ||
gameObject.removeBehavior(mockBehavior, mockScene); | ||
} | ||
|
||
assertEquals(expectedBehaviorCount, gameObject.getBehaviors().size(), "When behaviors are removed, the behavior count should decrease."); | ||
} | ||
|
||
@Test | ||
public void checkAddAndRemoveBehaviorsFromGameObject_usingMethodChaining() { | ||
Behavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
|
||
GameObject gameObject = new MockGameObject() | ||
.addBehavior(mockBehavior, mockScene) // 1 | ||
.addBehavior(mockBehavior, mockScene) // 2 | ||
.addBehavior(mockBehavior, mockScene) // 3 | ||
.removeBehavior(mockBehavior, mockScene) // 2 | ||
.addBehavior(mockBehavior, mockScene) // 3 | ||
.removeBehavior(mockBehavior, mockScene) // 2 | ||
.removeBehavior(mockBehavior, mockScene) // 1 | ||
.addBehavior(mockBehavior, mockScene); // 2 | ||
|
||
assertEquals(2, gameObject.getBehaviors().size(), "After the sequence of adding and removing behaviors, the remaining behavior count should be 5."); | ||
} | ||
|
||
@Test | ||
public void checkInitBehaviors_shouldInitializePointf() { | ||
GameObject gameObject = new MockGameObject(); | ||
MockBehavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
|
||
gameObject.addBehavior(mockBehavior, mockScene); | ||
gameObject.initBehaviors(); | ||
|
||
assertNotNull(mockBehavior.getPointf(), "After initializing the GameObject's behaviors, its Pointf should not be null."); | ||
} | ||
|
||
@Test | ||
public void checkUpdateBehaviors_shouldIncrementPointf() { | ||
GameObject gameObject = new MockGameObject(); | ||
MockBehavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
|
||
gameObject.addBehavior(mockBehavior, mockScene); | ||
gameObject.initBehaviors(); | ||
|
||
int expectedIncrement = 15; | ||
for (int i = 0; i < expectedIncrement; i++) { | ||
gameObject.updateBehaviors(); | ||
} | ||
|
||
boolean condition = Maths.floatEquals(expectedIncrement, mockBehavior.getPointf().x) && Maths.floatEquals(expectedIncrement, mockBehavior.getPointf().y); | ||
assertTrue(condition, "After updating, the behavior's Pointf should have incremented."); | ||
} | ||
|
||
@Test | ||
public void checkDestroyBehaviors_shouldMakePointfNull() { | ||
GameObject gameObject = new MockGameObject(); | ||
MockBehavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
|
||
gameObject.addBehavior(mockBehavior, mockScene); | ||
gameObject.initBehaviors(); // pointf is not null here | ||
assertNotNull(mockBehavior.getPointf()); | ||
|
||
gameObject.destroyAllBehaviors(); // pointf is null here | ||
assertNull(mockBehavior.getPointf(), "After destroying the GameObject's behaviors, the Pointf should be null."); | ||
} | ||
|
||
@Test | ||
public void tryUpdateBehaviorWithoutInitializing_shouldThrowNullPointerException() { | ||
GameObject gameObject = new MockGameObject(); | ||
MockBehavior mockBehavior = new MockBehavior(); | ||
Scene mockScene = new MockScene(); | ||
|
||
gameObject.addBehavior(mockBehavior, mockScene); | ||
|
||
assertThrows(NullPointerException.class, gameObject::updateBehaviors, "Trying to update the behavior without initializing it should throw a null pointer exception on the Pointf."); | ||
} | ||
} |