Skip to content

Commit

Permalink
feat: Added some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Apr 15, 2024
1 parent 4292a9f commit 97cd71d
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 22 deletions.
2 changes: 1 addition & 1 deletion distributor-common-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

dependencies {
compileOnlyApi(libs.immutables.annotations)
compileOnly(projects.distributorInternalAnnotations)
annotationProcessor(libs.immutables.processor)
compileOnlyApi(libs.slf4j.api)
compileOnlyApi(libs.bundles.mindustry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import com.xpdustry.distributor.api.annotation.PluginAnnotationScanner;
import com.xpdustry.distributor.api.event.EventSubscription;
import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.api.plugin.MindustryPlugin;
import com.xpdustry.distributor.api.plugin.PluginAware;
import com.xpdustry.distributor.api.scheduler.Cancellable;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.xpdustry.distributor.api.command;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.util.Collection;
import org.immutables.value.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.xpdustry.distributor.api.command;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.util.List;
import org.immutables.value.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package com.xpdustry.distributor.api.command;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.api.translation.LocaleHolder;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.util.Locale;
import org.immutables.value.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.xpdustry.distributor.api.content;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.util.List;
import mindustry.ctype.ContentType;
import mindustry.ctype.MappableContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.xpdustry.distributor.api.player;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package com.xpdustry.distributor.api.player;

import arc.util.Strings;
import com.xpdustry.distributor.api.internal.DistributorDataClassWithBuilder;
import com.xpdustry.distributor.internal.annotation.DistributorDataClassWithBuilder;
import java.text.Normalizer;
import java.util.Collection;
import java.util.EnumSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,8 @@ public Collection<Player> findOnlinePlayers(final Query query) {
}
}

if (matched == 1) {
if (query.isMatchExact()) {
return List.of(match);
}
result.add(match);
if (matched == 1 && query.isMatchExact()) {
return List.of(match);
} else {
result.addAll(matches);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import arc.util.serialization.Json;
import com.xpdustry.distributor.api.collection.MindustryCollections;
import com.xpdustry.distributor.api.internal.DistributorDataClassWithBuilder;
import com.xpdustry.distributor.internal.annotation.DistributorDataClassWithBuilder;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package com.xpdustry.distributor.api.service;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.api.plugin.MindustryPlugin;
import com.xpdustry.distributor.api.util.Priority;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.util.List;
import java.util.function.Supplier;
import org.immutables.value.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.xpdustry.distributor.api.translation;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.xpdustry.distributor.api.translation;

import com.xpdustry.distributor.api.internal.DistributorDataClass;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.util.Map;
import org.immutables.value.Value;

Expand Down
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;
}
}
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)));
}
}
Loading

0 comments on commit 97cd71d

Please sign in to comment.