generated from xpdustry/template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
338 additions
and
22 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
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
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
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
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
139 changes: 139 additions & 0 deletions
139
...or-common-api/src/test/java/com/xpdustry/distributor/api/player/PlayerLookupImplTest.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,139 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.api.player; | ||
|
||
import java.util.Base64; | ||
import java.util.Random; | ||
import mindustry.gen.Groups; | ||
import mindustry.gen.Player; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public final class PlayerLookupImplTest { | ||
|
||
private static final Random random = new Random(); | ||
private static int counter = 1; | ||
|
||
private static final Player PLAYER_1 = createPlayer("deez"); | ||
private static final Player PLAYER_2 = createPlayer("deez nuts"); | ||
private static final Player PLAYER_3 = createPlayer("phinner"); | ||
private static final Player PLAYER_4 = createPlayer("[green]zeta"); | ||
private static final Player PLAYER_5 = createPlayer("[cyan]zeta"); | ||
private static final Player PLAYER_6 = createPlayer(PLAYER_1.uuid()); | ||
private static final Player PLAYER_7 = createPlayer("#" + PLAYER_4.id()); | ||
|
||
@BeforeEach | ||
void addAllPlayers() { | ||
Groups.init(); | ||
Groups.player.add(PLAYER_1); | ||
Groups.player.add(PLAYER_2); | ||
Groups.player.add(PLAYER_3); | ||
Groups.player.add(PLAYER_4); | ||
Groups.player.add(PLAYER_5); | ||
Groups.player.add(PLAYER_6); | ||
Groups.player.add(PLAYER_7); | ||
} | ||
|
||
@AfterEach | ||
void clearPlayers() { | ||
Groups.clear(); | ||
} | ||
|
||
@Test | ||
void find_by_name_simple() { | ||
assertQueryResult(PlayerLookup.Query.of("phinner"), PLAYER_3); | ||
} | ||
|
||
@Test | ||
void find_by_name_colored() { | ||
assertQueryResult(PlayerLookup.Query.of("[green]phinner"), PLAYER_3); | ||
} | ||
|
||
@Test | ||
void find_by_name_partial() { | ||
assertQueryResult(PlayerLookup.Query.of("de"), PLAYER_1, PLAYER_2); | ||
} | ||
|
||
@Test | ||
void find_by_name_exact() { | ||
assertQueryResult(PlayerLookup.Query.of("deez"), PLAYER_1); | ||
} | ||
|
||
@Test | ||
void find_by_name_exact_with_all() { | ||
assertQueryResult( | ||
PlayerLookup.Query.builder() | ||
.setInput("deez") | ||
.setMatchExact(false) | ||
.build(), | ||
PLAYER_1, | ||
PLAYER_2); | ||
} | ||
|
||
@Test | ||
void find_by_name_accents() { | ||
assertQueryResult(PlayerLookup.Query.of("phînnér"), PLAYER_3); | ||
} | ||
|
||
@Test | ||
void find_by_names_multiple_exact() { | ||
assertQueryResult(PlayerLookup.Query.of("zeta"), PLAYER_4, PLAYER_5); | ||
} | ||
|
||
@Test | ||
void find_by_entity_id_exact() { | ||
assertQueryResult(PlayerLookup.Query.of("#4"), PLAYER_4); | ||
} | ||
|
||
@Test | ||
void find_by_entity_id_with_all() { | ||
assertQueryResult( | ||
PlayerLookup.Query.builder().setInput("#4").setMatchExact(false).build(), PLAYER_4, PLAYER_7); | ||
} | ||
|
||
@Test | ||
void find_by_uuid() { | ||
assertQueryResult(PlayerLookup.Query.of(PLAYER_3.uuid())); | ||
assertQueryResult( | ||
PlayerLookup.Query.builder() | ||
.setInput(PLAYER_3.uuid()) | ||
.addField(PlayerLookup.Field.UUID) | ||
.build(), | ||
PLAYER_3); | ||
} | ||
|
||
private void assertQueryResult(final PlayerLookup.Query query, final Player... result) { | ||
final var lookup = new PlayerLookupImpl(PlayerLookup.DEFAULT_NORMALIZER); | ||
assertThat(lookup.findOnlinePlayers(query)).containsExactlyInAnyOrder(result); | ||
} | ||
|
||
private static Player createPlayer(final String name) { | ||
final var uuid = new byte[16]; | ||
random.nextBytes(uuid); | ||
final var player = Mockito.mock(Player.class); | ||
Mockito.when(player.name()).thenReturn(name); | ||
Mockito.when(player.uuid()).thenReturn(Base64.getEncoder().encodeToString(uuid)); | ||
Mockito.when(player.id()).thenReturn(counter++); | ||
return player; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...ommon-api/src/test/java/com/xpdustry/distributor/api/scheduler/MindustryTimeUnitTest.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,80 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.api.scheduler; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public final class MindustryTimeUnitTest { | ||
|
||
@ParameterizedTest | ||
@EnumSource(MindustryTimeUnit.class) | ||
void test_java_unit(final MindustryTimeUnit unit) { | ||
if (unit != MindustryTimeUnit.TICKS) { | ||
assertThat(unit.getJavaTimeUnit()).isPresent(); | ||
} else { | ||
assertThat(unit.getJavaTimeUnit()).isEmpty(); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideJavaMindustryTimeUnits") | ||
void test_java_conversion(final MindustryTimeUnit unitA, final MindustryTimeUnit unitB) { | ||
assertThat(unitA.getJavaTimeUnit() | ||
.orElseThrow() | ||
.convert(1000L, unitB.getJavaTimeUnit().orElseThrow())) | ||
.isEqualTo(unitA.convert(1000L, unitB)); | ||
} | ||
|
||
@Test | ||
void test_tick_conversion() { | ||
assertThat(MindustryTimeUnit.TICKS.convert(60L, MindustryTimeUnit.TICKS)) | ||
.isEqualTo(60L); | ||
|
||
assertThat(MindustryTimeUnit.TICKS.convert(2L, MindustryTimeUnit.SECONDS)) | ||
.isEqualTo(120L); | ||
|
||
assertThat(MindustryTimeUnit.SECONDS.convert(120L, MindustryTimeUnit.TICKS)) | ||
.isEqualTo(2L); | ||
} | ||
|
||
@Test | ||
void test_tick_conversion_overflow() { | ||
assertThat(MindustryTimeUnit.TICKS.convert(+10_000_000_000_000L, MindustryTimeUnit.DAYS)) | ||
.isEqualTo(Long.MAX_VALUE); | ||
|
||
assertThat(MindustryTimeUnit.TICKS.convert(-10_000_000_000_000L, MindustryTimeUnit.DAYS)) | ||
.isEqualTo(Long.MIN_VALUE); | ||
} | ||
|
||
private static Stream<Arguments> provideJavaMindustryTimeUnits() { | ||
return Arrays.stream(MindustryTimeUnit.values()) | ||
.filter(unit -> unit != MindustryTimeUnit.TICKS) | ||
.flatMap(unit1 -> Arrays.stream(MindustryTimeUnit.values()) | ||
.filter(unit2 -> unit2 != MindustryTimeUnit.TICKS) | ||
.map(unit2 -> Arguments.of(unit1, unit2))); | ||
} | ||
} |
Oops, something went wrong.