Skip to content

Commit

Permalink
(#3) added testing for SceneManager, SimpleManager, Scene
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed May 26, 2021
1 parent bf5a9f6 commit 07e8457
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/test/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
opens unittest.testcases.graphics.game to org.junit.platform.commons;

opens unittest.testcases.math to org.junit.platform.commons;

opens unittest.testcases.systems.control to org.junit.platform.commons;
}
4 changes: 2 additions & 2 deletions src/test/java/unittest/HeadlessHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.awt.GraphicsEnvironment;

import unittest.mock.MockManager;
import unittest.mock.systems.control.MockRunnableSimpleManager;

public class HeadlessHelper {

Expand All @@ -25,7 +25,7 @@ private static boolean headlessTest() {
}

public static void runFastJWith(Runnable runnable) {
FastJEngine.init("For those sweet, sweet testing purposes", new MockManager(runnable));
FastJEngine.init("For those sweet, sweet testing purposes", new MockRunnableSimpleManager(runnable));
try {
FastJEngine.run();
} catch (NullPointerException ignored) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unittest.mock;
package unittest.mock.systems.behaviors;

import io.github.lucasstarsz.fastj.math.Pointf;
import io.github.lucasstarsz.fastj.graphics.game.GameObject;
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/unittest/mock/systems/control/MockEmptyScene.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package unittest.mock.systems.control;

import io.github.lucasstarsz.fastj.graphics.Display;

import io.github.lucasstarsz.fastj.systems.control.Scene;

import java.util.UUID;

public class MockEmptyScene extends Scene {

public MockEmptyScene() {
super(UUID.randomUUID().toString());
}

@Override
public void load(Display display) {
}

@Override
public void unload(Display display) {
}

@Override
public void update(Display display) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package unittest.mock.systems.control;

import io.github.lucasstarsz.fastj.graphics.Display;

import io.github.lucasstarsz.fastj.systems.control.SimpleManager;

public class MockEmptySimpleManager extends SimpleManager {
@Override
public void init(Display display) {
}

@Override
public void update(Display display) {
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package unittest.mock;
package unittest.mock.systems.control;

import io.github.lucasstarsz.fastj.graphics.Display;

import io.github.lucasstarsz.fastj.systems.control.Scene;

public class MockScene extends Scene {
public class MockNameSettingScene extends Scene {

public MockScene() {
super("");
public MockNameSettingScene(String setName) {
super(setName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package unittest.mock;
package unittest.mock.systems.control;

import io.github.lucasstarsz.fastj.engine.FastJEngine;
import io.github.lucasstarsz.fastj.graphics.Display;

import io.github.lucasstarsz.fastj.systems.control.SimpleManager;

public class MockManager extends SimpleManager {
public class MockRunnableSimpleManager extends SimpleManager {

private final Runnable runnable;

public MockManager(Runnable runnable) {
public MockRunnableSimpleManager(Runnable runnable) {
this.runnable = runnable;
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/java/unittest/mock/systems/control/MockSceneManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package unittest.mock.systems.control;

import io.github.lucasstarsz.fastj.graphics.Display;

import io.github.lucasstarsz.fastj.systems.control.SceneManager;

public class MockSceneManager extends SceneManager {
@Override
public void init(Display display) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package unittest.mock.systems.control;

import io.github.lucasstarsz.fastj.graphics.Display;

import io.github.lucasstarsz.fastj.systems.control.Scene;

import java.util.UUID;

public abstract class MockUpdatableScene extends Scene {

public MockUpdatableScene() {
super(UUID.randomUUID().toString());
}

@Override
public void load(Display display) {

}

@Override
public void unload(Display display) {

}

@Override
public abstract void update(Display display);
}
11 changes: 11 additions & 0 deletions src/test/java/unittest/mock/systems/tags/MockTaggableEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package unittest.mock.systems.tags;

import io.github.lucasstarsz.fastj.graphics.Drawable;

import io.github.lucasstarsz.fastj.systems.control.Scene;

public class MockTaggableEntity extends Drawable {
@Override
public void destroy(Scene originScene) {
}
}
20 changes: 10 additions & 10 deletions src/test/java/unittest/testcases/graphics/game/GameObjectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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.systems.behaviors.MockBehavior;
import unittest.mock.systems.control.MockEmptyScene;
import unittest.mock.graphics.MockGameObject;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -31,7 +31,7 @@ void checkAddBehaviorToGameObject_shouldAllowMultiple() {
GameObject gameObject = new MockGameObject();

Behavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();
int behaviorCount = 255;

for (int i = 0; i < behaviorCount; i++) {
Expand All @@ -46,7 +46,7 @@ void checkClearBehaviorsFromGameObject_shouldRemoveAll() {
GameObject gameObject = new MockGameObject();

Behavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();
int behaviorCount = 255;

for (int i = 0; i < behaviorCount; i++) {
Expand All @@ -63,7 +63,7 @@ void checkRemoveBehaviorFromGameObject() {
GameObject gameObject = new MockGameObject();

Behavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();
int behaviorCount = 255;

for (int i = 0; i < behaviorCount; i++) {
Expand All @@ -81,7 +81,7 @@ void checkRemoveBehaviorFromGameObject() {
@Test
void checkAddAndRemoveBehaviorsFromGameObject_usingMethodChaining() {
Behavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();

GameObject gameObject = new MockGameObject()
.addBehavior(mockBehavior, mockScene) // 1
Expand All @@ -100,7 +100,7 @@ void checkAddAndRemoveBehaviorsFromGameObject_usingMethodChaining() {
void checkInitBehaviors_shouldInitializePointf() {
GameObject gameObject = new MockGameObject();
MockBehavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();

gameObject.addBehavior(mockBehavior, mockScene);
gameObject.initBehaviors();
Expand All @@ -112,7 +112,7 @@ void checkInitBehaviors_shouldInitializePointf() {
void checkUpdateBehaviors_shouldIncrementPointf() {
GameObject gameObject = new MockGameObject();
MockBehavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();

gameObject.addBehavior(mockBehavior, mockScene);
gameObject.initBehaviors();
Expand All @@ -130,7 +130,7 @@ void checkUpdateBehaviors_shouldIncrementPointf() {
void checkDestroyBehaviors_shouldMakePointfNull() {
GameObject gameObject = new MockGameObject();
MockBehavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();

gameObject.addBehavior(mockBehavior, mockScene);
gameObject.initBehaviors(); // pointf is not null here
Expand All @@ -144,7 +144,7 @@ void checkDestroyBehaviors_shouldMakePointfNull() {
void tryUpdateBehaviorWithoutInitializing_shouldThrowNullPointerException() {
GameObject gameObject = new MockGameObject();
MockBehavior mockBehavior = new MockBehavior();
Scene mockScene = new MockScene();
Scene mockScene = new MockEmptyScene();

gameObject.addBehavior(mockBehavior, mockScene);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package unittest.testcases.systems.control;

import io.github.lucasstarsz.fastj.systems.control.Scene;
import io.github.lucasstarsz.fastj.systems.control.SceneManager;

import org.junit.jupiter.api.Test;
import unittest.mock.systems.control.MockNameSettingScene;
import unittest.mock.systems.control.MockEmptyScene;
import unittest.mock.systems.control.MockSceneManager;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class SceneManagerTests {

@Test
void checkSceneManagerCreation() {
SceneManager sceneManager = new MockSceneManager();

assertEquals(0, sceneManager.getScenes().size(), "After creating the manager, it should not contain any scenes.");
assertNull(sceneManager.getCurrentScene(), "After creating the manager, the current scene should not yet have been set.");
assertFalse(sceneManager.isSwitchingScenes(), "After creating the manager, it should not be switching scenes.");
}

@Test
void checkSceneManagerSceneAdding() {
SceneManager sceneManager = new MockSceneManager();

Scene scene = new MockEmptyScene();
sceneManager.addScene(scene);

assertEquals(1, sceneManager.getScenes().size(), "After adding a scene to the manager, it should contain one scene.");
}

@Test
void trySceneManagerSceneAdding_withNameThatAlreadyExists() {
SceneManager sceneManager = new MockSceneManager();

String sceneName = "having two scenes with the same name should throw an exception";
Scene nameSettingScene1 = new MockNameSettingScene(sceneName);
Scene nameSettingScene2 = new MockNameSettingScene(sceneName);

sceneManager.addScene(nameSettingScene1);

Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.addScene(nameSettingScene2));

String expectedExceptionMessage = "The scene name \"" + sceneName + "\" is already in use."
+ System.lineSeparator()
+ "Scenes added: [" + sceneName + "]";
assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message.");
}

@Test
void checkSceneManagerSceneGetting_bySceneName() {
SceneManager sceneManager = new MockSceneManager();

Scene scene = new MockEmptyScene();
sceneManager.addScene(scene);

assertEquals(scene, sceneManager.getScene(scene.getSceneName()), "After adding a scene to the manager, getting that scene by its name should return the original scene.");
}

@Test
void trySceneManagerGetScene_withSceneNameThatDoesNotExist() {
SceneManager sceneManager = new MockSceneManager();

String sceneName = "trying to get a scene with a scene name that doesn't exist should throw an exception";
Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.getScene(sceneName));

String expectedExceptionMessage = "A scene with the name: \"" + sceneName + "\" hasn't been added!";
assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message.");
}

@Test
void checkSceneManagerSceneRemoving_bySceneObject() {
SceneManager sceneManager = new MockSceneManager();

Scene scene = new MockEmptyScene();
sceneManager.addScene(scene);
sceneManager.removeScene(scene);

assertEquals(0, sceneManager.getScenes().size(), "After removing the scene from the manager, it should not contain any scenes.");
}

@Test
void checkSceneManagerSceneRemoving_bySceneName() {
SceneManager sceneManager = new MockSceneManager();

Scene scene = new MockEmptyScene();
sceneManager.addScene(scene);
sceneManager.removeScene(scene.getSceneName());

assertEquals(0, sceneManager.getScenes().size(), "After removing the scene from the manager, it should not contain any scenes.");
}

@Test
void trySceneManagerSceneRemoving_bySceneName_withSceneNameThatDoesNotExist() {
SceneManager sceneManager = new MockSceneManager();

String sceneName = "trying to remove a scene with a scene name that doesn't exist should throw an exception";
Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.removeScene(sceneName));

String expectedExceptionMessage = "A scene with the name: \"" + sceneName + "\" hasn't been added!";
assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message.");
}

@Test
void checkSceneManagerCurrentSceneSetting_bySceneObject() {
SceneManager sceneManager = new MockSceneManager();

Scene scene = new MockEmptyScene();
sceneManager.addScene(scene);
sceneManager.setCurrentScene(scene);

assertEquals(scene, sceneManager.getCurrentScene(), "After setting the manager's current scene, getting the current scene should return the same scene.");
}

@Test
void checkSceneManagerCurrentSceneSetting_bySceneName() {
SceneManager sceneManager = new MockSceneManager();

Scene scene = new MockEmptyScene();
sceneManager.addScene(scene);
sceneManager.setCurrentScene(scene.getSceneName());

assertEquals(scene, sceneManager.getCurrentScene(), "After setting the manager's current scene, getting the current scene should return the same scene.");
}

@Test
void trySceneManagerSetCurrentScene_bySceneName_withSceneNameThatDoesNotExist() {
SceneManager sceneManager = new MockSceneManager();

String sceneName = "trying to set the current scene with a scene name that doesn't exist should throw an exception";
Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.setCurrentScene(sceneName));

String expectedExceptionMessage = "A scene with the name: \"" + sceneName + "\" hasn't been added!";
assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message.");
}
}
Loading

0 comments on commit 07e8457

Please sign in to comment.