Skip to content

Commit

Permalink
(#3) Text2D unit tests, simplify headless check
Browse files Browse the repository at this point in the history
Other Changes:
- Simplified the `FastJEngine#error(...)` method
    - The method no longer closes the JVM instance. Instead, it closes the game and throws the provided exception with the provided error message.
  • Loading branch information
lucasstarsz committed Apr 12, 2021
1 parent 4fdc7c4 commit 7d2d258
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,8 @@ public static <T> void warning(T warningMessage) {
* @param exception The exception that caused a need for this method call.
*/
public static <T> void error(T errorMessage, Exception exception) {
System.err.println("ERROR: " + errorMessage);
exception.printStackTrace();
System.exit(-1);
FastJEngine.closeGame();
throw new IllegalStateException("ERROR: " + errorMessage, exception);
}

/**
Expand Down
107 changes: 100 additions & 7 deletions src/test/java/unittest/testcases/graphics/text/Text2DTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.github.lucasstarsz.fastj.graphics.gameobject.text.Text2D;
import io.github.lucasstarsz.fastj.math.Maths;
import io.github.lucasstarsz.fastj.math.Pointf;
import io.github.lucasstarsz.fastj.systems.render.Display;
import io.github.lucasstarsz.fastj.systems.control.Scene;
import org.junit.jupiter.api.BeforeEach;
import io.github.lucasstarsz.fastj.systems.render.Display;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import unittest.mock.MockManager;

Expand All @@ -17,15 +17,23 @@
import java.awt.GraphicsEnvironment;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class Text2DTests {

@BeforeEach
public void onlyRunOnWindows() {
// hate to do it to ya, unix, but idk how to run github actions in non-headless mode
String headless = System.getProperty("java.awt.headless");
assumeTrue(!GraphicsEnvironment.isHeadless() || headless != null && headless.equalsIgnoreCase("false"));
@BeforeAll
public static void onlyRunIfNotHeadless() {
// because idk how to run github actions in non-headless mode
boolean isHeadless = GraphicsEnvironment.isHeadless();
System.out.println(
"This graphics environment is... " + (isHeadless
? "headless. Well that's unfortunate... this device isn't running in headless mode, so Text2D tests cannot be conducted."
: "not headless. Good."
)
);

assumeTrue(!isHeadless);
}

@Test
Expand Down Expand Up @@ -129,4 +137,89 @@ public void update(Display display) {

FastJEngine.run();
}

@Test
public void checkTranslateText2D_shouldMatchExpected() {
FastJEngine.init("For those sweet, sweet testing purposes", new MockManager(new Scene("") {
@Override
public void load(Display display) {
String text = "Hello, world!";
Pointf originalTranslation = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));
Pointf randomTranslation = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));

Text2D text2D = new Text2D(text, originalTranslation);
text2D.translate(randomTranslation);

Pointf expectedTranslation = Pointf.add(originalTranslation, randomTranslation);
Pointf actualTranslation = text2D.getTranslation();

assertEquals(expectedTranslation, actualTranslation, "The actual translation should match the expected translation.");
}

@Override
public void unload(Display display) {
}

@Override
public void update(Display display) {
FastJEngine.closeGame();
}
}));

FastJEngine.run();
}

@Test
public void checkRotateText2D_shouldThrowError() {
FastJEngine.init("For those sweet, sweet testing purposes", new MockManager(new Scene("") {
@Override
public void load(Display display) {
String text = "Hello, world!";
Pointf originalTranslation = Pointf.Origin.copy();
float randomRotation = Maths.random(-50f, 50f);

Text2D text2D = new Text2D(text, originalTranslation);
Throwable exception = assertThrows(IllegalStateException.class, () -> text2D.rotate(randomRotation, Pointf.Origin));
assertEquals(exception.getMessage(), "ERROR: The game crashed, due to the call of a method not yet implemented.");
}

@Override
public void unload(Display display) {
}

@Override
public void update(Display display) {
FastJEngine.closeGame();
}
}));

FastJEngine.run();
}

@Test
public void checkScaleText2D_shouldThrowException() {
FastJEngine.init("For those sweet, sweet testing purposes", new MockManager(new Scene("") {
@Override
public void load(Display display) {
String text = "Hello, world!";
Pointf originalTranslation = Pointf.Origin.copy();
Pointf randomScale = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f));

Text2D text2D = new Text2D(text, originalTranslation);
Throwable exception = assertThrows(IllegalStateException.class, () -> text2D.scale(randomScale, originalTranslation));
assertEquals(exception.getMessage(), "ERROR: The game crashed, due to the call of a method not yet implemented.");
}

@Override
public void unload(Display display) {
}

@Override
public void update(Display display) {
FastJEngine.closeGame();
}
}));

FastJEngine.run();
}
}

0 comments on commit 7d2d258

Please sign in to comment.