Skip to content

Commit

Permalink
fix: fix isBefore, also added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Sep 11, 2024
1 parent cf98cd8 commit 6877d9e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public boolean isMinecraftVersion(int minecraftVersion) {
* It is equivalent to the "minor" version
* <p>
* Example: {@literal "1.13"} returns {@literal 13}<br />
* Exampe: {@literal "1.13.2"} returns {@literal 13_2}
* Example: {@literal "1.13.2"} returns {@literal 13_2}
*
* @param minecraftVersion
* The {@link Integer} version to match
Expand Down Expand Up @@ -278,7 +278,22 @@ public boolean isBefore(@Nonnull MinecraftVersion version) {
* @return True if this version is before, False if this version is virtual or otherwise.
*/
public boolean isBefore(int minecraftVersion, int patchVersion) {
return !isVirtual() && (this.majorVersion < minecraftVersion && this.minorVersion < patchVersion);
if (isVirtual()) {
return false;
}

if (this.majorVersion < minecraftVersion) {
return true;
}
if (this.majorVersion > minecraftVersion) {
return false;
}

if (this.minorVersion == -1) {
return patchVersion > 0;
} else {
return this.minorVersion < patchVersion;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ void testIsBefore() {
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_17.isBefore(MinecraftVersion.MINECRAFT_1_16));
}

@Test
@DisplayName("Test if Minecraft versions #isBefore behaves correctly for minor versions")
void testIsBeforeMinor() {
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_18.isBefore(16, 5));
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_18.isBefore(17, 1));
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_18.isBefore(18, 0));

Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_20.isBefore(20, 0));
Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_20.isBefore(20, 2));
Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_20.isBefore(20, 4));
Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_20.isBefore(20, 5));

Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_20_5.isBefore(20, 4));
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_20_5.isBefore(20, 5));
Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_20_5.isBefore(20, 6));
}

@Test
@DisplayName("Test correct behaviour for MinecraftVersion.UNKNOWN.isBefore(...)")
void testIsBeforeUnknown() {
Expand Down

0 comments on commit 6877d9e

Please sign in to comment.