-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial unit tests #153
Draft
MattSturgeon
wants to merge
28
commits into
MinecraftFreecam:main
Choose a base branch
from
MattSturgeon:test/init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Initial unit tests #153
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
bb3109d
Add JUnit 5 dependency
MattSturgeon 5358c5e
Add Mockito dependency
MattSturgeon 6b3ffab
Use `run` working directory for tests
MattSturgeon 4130505
Create BootstrapMinecraft annotation
shartte 960e295
Stub FreecamPosition test
MattSturgeon dd36db9
Continue stubbing initial test
MattSturgeon badced0
Add AssertJ dependency
MattSturgeon 68dd556
Add JUnit Parameterized Tests dependency
MattSturgeon c73815a
Add a reflection util
MattSturgeon 248911e
AssertJ setRotation test
MattSturgeon f452942
Test quaternion is updated by setRotation
MattSturgeon e068b40
Add EnableMockito annotation
MattSturgeon ec2adb2
Use EnableMockito annotation
MattSturgeon e2d915d
Move testing utils to separate project
MattSturgeon 6b507b7
Fix testing utils imports
MattSturgeon 8b100b8
Enable fabric-specific tests
MattSturgeon 8450d32
Audit mixin in fabric environment
MattSturgeon 1563829
Validate Minecraft in fabric environment
MattSturgeon 357f0ae
Test moveForward() together with setRotation()
MattSturgeon b2a3c0a
Add constructor test for swimming position
MattSturgeon ff74175
Minor improvements
MattSturgeon bc48eca
ci: new build workflow
MattSturgeon 41e7fcb
ci: run tests in main workflow
MattSturgeon 953678b
ci: add status badge to README
MattSturgeon e421196
Add constructor rotation test
MattSturgeon 42b7581
fixup! Add constructor test for swimming position
MattSturgeon e4e2c18
Test moving position after mirroring
MattSturgeon e6fa497
Test chunk pos
MattSturgeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,35 @@ | ||
name: Gradle (build & test) | ||
|
||
on: | ||
push: | ||
branches: &branches | ||
- 'main' | ||
- '1.16' | ||
- '1.17' | ||
- '1.18' | ||
- '1.19' | ||
pull_request: | ||
branches: *branches | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build_and_test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up JDK | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
distribution: microsoft | ||
- name: Build | ||
uses: gradle/gradle-build-action@v2 | ||
with: | ||
arguments: build | ||
- name: Test | ||
uses: gradle/gradle-build-action@v2 | ||
with: | ||
# We could run `check` instead, but we may want to add checks we don't want to use in CI...? | ||
arguments: test |
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
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
205 changes: 205 additions & 0 deletions
205
common/src/test/java/net/xolt/freecam/util/FreecamPositionTest.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,205 @@ | ||
package net.xolt.freecam.util; | ||
|
||
import com.mojang.authlib.GameProfile; | ||
import net.minecraft.client.multiplayer.ClientLevel; | ||
import net.minecraft.client.player.RemotePlayer; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.Pose; | ||
import net.minecraft.world.level.ChunkPos; | ||
import net.minecraft.world.phys.Vec2; | ||
import net.minecraft.world.phys.Vec3; | ||
import net.xolt.freecam.testing.extension.BootstrapMinecraft; | ||
import net.xolt.freecam.testing.extension.EnableMockito; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.withPrecision; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@EnableMockito | ||
@BootstrapMinecraft | ||
class FreecamPositionTest { | ||
|
||
Entity entity; | ||
FreecamPosition position; | ||
|
||
static double[] distances() { | ||
return new double[] { 1, -1, 2_000_000_001, 0.00456789, -0.0000445646456456060456, 2.5 }; | ||
} | ||
|
||
static Vec2[] rotations() { | ||
return new Vec2[] { Vec2.ZERO, Vec2.MIN, Vec2.MAX, Vec2.UNIT_X, Vec2.UNIT_Y, Vec2.NEG_UNIT_X, Vec2.NEG_UNIT_Y }; | ||
} | ||
|
||
static Vec3[] positions() { | ||
return new Vec3[] { Vec3.ZERO, new Vec3(1, 1, 1), new Vec3(1000, 100, 10) }; | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
ClientLevel level = mock(ClientLevel.class); | ||
when(level.getSharedSpawnPos()).thenReturn(BlockPos.ZERO); | ||
when(level.getSharedSpawnAngle()).thenReturn(0f); | ||
GameProfile profile = new GameProfile(new UUID(0, 0), "TestPlayer"); | ||
entity = new RemotePlayer(level, profile); | ||
position = new FreecamPosition(entity); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(Pose.class) | ||
@DisplayName("Use entity position, adjusted for pose") | ||
void init_position(Pose pose) { | ||
entity.setPose(pose); | ||
double diff = entity.getEyeHeight(pose) - entity.getEyeHeight(Pose.SWIMMING); | ||
FreecamPosition swimPos = new FreecamPosition(entity); | ||
|
||
assertThat(swimPos.x).as("x is %01.2f".formatted(entity.getX())).isEqualTo(entity.getX()); | ||
assertThat(swimPos.y).as("y is %01.2f higher than %01.2f".formatted(diff, entity.getY())).isEqualTo(entity.getY() + diff, withPrecision(0.0000004)); | ||
assertThat(swimPos.z).as("z is %01.2f".formatted(entity.getZ())).isEqualTo(entity.getZ()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("rotations") | ||
@DisplayName("Uses entity rotation") | ||
void init_rotation(Vec2 rotation) { | ||
entity.setXRot(rotation.x); | ||
entity.setYRot(rotation.y); | ||
FreecamPosition rotatedPos = new FreecamPosition(entity); | ||
|
||
assertThat(rotatedPos.yaw).as("yaw is %01.2f".formatted(rotation.y)).isEqualTo(rotation.y); | ||
assertThat(rotatedPos.pitch).as("pitch is %01.2f".formatted(rotation.x)).isEqualTo(rotation.x); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("distances") | ||
@DisplayName("Moves forward on x axis") | ||
void moveForward_x(double distance) { | ||
float yaw = -90; | ||
float pitch = 0; | ||
|
||
double x = position.x; | ||
double y = position.y; | ||
double z = position.z; | ||
|
||
position.setRotation(yaw, pitch); | ||
position.moveForward(distance); | ||
|
||
assertThat(position.x).as("x increased by " + distance).isEqualTo(x + distance); | ||
assertThat(position.y).as("y is unchanged").isEqualTo(y); | ||
assertThat(position.z).as("z is unchanged").isEqualTo(z); | ||
|
||
// Moving the same distance after a mirror should revert | ||
position.mirrorRotation(); | ||
position.moveForward(distance); | ||
|
||
assertThat(position.x).as("x is reverted").isEqualTo(x); | ||
assertThat(position.y).as("y is unchanged").isEqualTo(y); | ||
assertThat(position.z).as("z is unchanged").isEqualTo(z); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("distances") | ||
@DisplayName("Moves forward on y axis") | ||
void moveForward_y(double distance) { | ||
float yaw = 0; | ||
float pitch = -90; | ||
|
||
double x = position.x; | ||
double y = position.y; | ||
double z = position.z; | ||
|
||
position.setRotation(yaw, pitch); | ||
position.moveForward(distance); | ||
|
||
assertThat(position.x).as("x is unchanged").isEqualTo(x); | ||
assertThat(position.y).as("y increased by " + distance).isEqualTo(y + distance); | ||
assertThat(position.z).as("z is unchanged").isEqualTo(z); | ||
|
||
// Moving the same distance after a mirror should revert | ||
position.mirrorRotation(); | ||
position.moveForward(distance); | ||
|
||
assertThat(position.x).as("x is unchanged").isEqualTo(x); | ||
assertThat(position.y).as("y is reverted").isEqualTo(y); | ||
assertThat(position.z).as("z is unchanged").isEqualTo(z); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("distances") | ||
@DisplayName("Moves forward on z axis") | ||
void moveForward_z(double distance) { | ||
float yaw = 0; | ||
float pitch = 0; | ||
|
||
double x = position.x; | ||
double y = position.y; | ||
double z = position.z; | ||
|
||
position.setRotation(yaw, pitch); | ||
position.moveForward(distance); | ||
|
||
assertThat(position.x).as("x is unchanged").isEqualTo(x); | ||
assertThat(position.y).as("y is unchanged").isEqualTo(y); | ||
assertThat(position.z).as("z increased by " + distance).isEqualTo(z + distance); | ||
|
||
// Moving the same distance after a mirror should revert | ||
position.mirrorRotation(); | ||
position.moveForward(distance); | ||
|
||
assertThat(position.x).as("x is unchanged").isEqualTo(x); | ||
assertThat(position.y).as("y is unchanged").isEqualTo(y); | ||
assertThat(position.z).as("z is reverted").isEqualTo(z); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("setRotation correctly sets yaw & pitch") | ||
@ValueSource(floats = { -16.456f, 0, 10, 2.5f, 2000008896.546f }) | ||
void setRotation_YawPitch(float number) { | ||
final float constant = 10; | ||
assertThat(position).isNotNull().satisfies( | ||
position -> { | ||
position.setRotation(number, constant); | ||
assertThat(position).as("Yaw is set correctly").satisfies( | ||
p -> assertThat(p.yaw).as("Yaw is set to (var) " + number).isEqualTo(number), | ||
p -> assertThat(p.pitch).as("Pitch is set to (const) " + constant).isEqualTo(constant) | ||
); | ||
}, | ||
position -> { | ||
position.setRotation(constant, number); | ||
assertThat(position).as("Pitch is set correctly").satisfies( | ||
p -> assertThat(p.yaw).as("Yaw is set to (const) " + constant).isEqualTo(constant), | ||
p -> assertThat(p.pitch).as("Pitch is set to (var) " + number).isEqualTo(number) | ||
); | ||
} | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("positions") | ||
@DisplayName("ChunkPos should be 16 times smaller than position") | ||
void chunkPos(Vec3 pos) { | ||
position.x = pos.x; | ||
position.y = pos.y; | ||
position.z = pos.z; | ||
// Should be 16 times smaller than x y z position, rounded down | ||
int x = (int) (pos.x / 16); | ||
int z = (int) (pos.z / 16); | ||
ChunkPos chunkPos = position.getChunkPos(); | ||
assertThat(chunkPos.x).isEqualTo(x); | ||
assertThat(chunkPos.z).isEqualTo(z); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
fabric/src/test/java/net/xolt/freecam/environment/BootstrapTest.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,15 @@ | ||
package net.xolt.freecam.environment; | ||
|
||
import net.minecraft.server.Bootstrap; | ||
import net.xolt.freecam.testing.extension.BootstrapMinecraft; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@BootstrapMinecraft | ||
class BootstrapTest { | ||
@Test | ||
@DisplayName("Validate Minecraft is bootstrapped") | ||
void validateBootstrap() { | ||
Bootstrap.validate(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
fabric/src/test/java/net/xolt/freecam/environment/MixinTest.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,15 @@ | ||
package net.xolt.freecam.environment; | ||
|
||
import net.xolt.freecam.testing.extension.BootstrapMinecraft; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.spongepowered.asm.mixin.MixinEnvironment; | ||
|
||
@BootstrapMinecraft | ||
class MixinTest { | ||
@Test | ||
@DisplayName("Audit mixin environment") | ||
void mixinEnvironmentAudit() { | ||
MixinEnvironment.getCurrentEnvironment().audit(); | ||
} | ||
} |
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
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
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,11 @@ | ||
architectury { | ||
common(rootProject.enabled_platforms.split(',')) | ||
} | ||
|
||
dependencies { | ||
// Needed for Environment annotation | ||
modCompileOnly "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}" | ||
implementation(platform("org.junit:junit-bom:${rootProject.junit_version}")) | ||
implementation("org.junit.jupiter:junit-jupiter-api") | ||
implementation "org.mockito:mockito-junit-jupiter:${rootProject.mockito_version}" | ||
} |
16 changes: 16 additions & 0 deletions
16
test-utils/src/main/java/net/xolt/freecam/testing/extension/BootstrapMinecraft.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,16 @@ | ||
package net.xolt.freecam.testing.extension; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Ensure Minecraft is bootstrapped before running tests. | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(BootstrapMinecraftExtension.class) | ||
public @interface BootstrapMinecraft {} |
15 changes: 15 additions & 0 deletions
15
test-utils/src/main/java/net/xolt/freecam/testing/extension/BootstrapMinecraftExtension.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,15 @@ | ||
package net.xolt.freecam.testing.extension; | ||
|
||
import net.minecraft.SharedConstants; | ||
import net.minecraft.server.Bootstrap; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.Extension; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
public class BootstrapMinecraftExtension implements Extension, BeforeAllCallback { | ||
@Override | ||
public void beforeAll(ExtensionContext context) { | ||
SharedConstants.tryDetectVersion(); | ||
Bootstrap.bootStrap(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess GitHub doesn't like yaml anchors...