mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-23 15:36:54 +00:00
Add some jqwik arbitraries for Minecraft types
This requires supporting registries in our platform test code. Thankfully this is mostly the same as what we can do in Fabric - the duplication is unfortunate - but it's easy enough.
This commit is contained in:
parent
5a7259e4c9
commit
5b2fdec6ca
@ -21,6 +21,7 @@ import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
@ -50,6 +51,7 @@ import net.minecraft.world.phys.Vec3;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
@ -68,30 +70,41 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
|
||||
throw new UnsupportedOperationException("Cannot create config file inside tests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> RegistryWrappers.RegistryWrapper<T> wrap(ResourceKey<Registry<T>> registry) {
|
||||
throw new UnsupportedOperationException("Cannot query registry inside tests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> RegistrationHelper<T> createRegistrationHelper(ResourceKey<Registry<T>> registry) {
|
||||
throw new UnsupportedOperationException("Cannot query registry inside tests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K> ResourceLocation getRegistryKey(ResourceKey<Registry<K>> registry, K object) {
|
||||
throw new UnsupportedOperationException("Cannot query registry inside tests");
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> Registry<T> getRegistry(ResourceKey<Registry<T>> id) {
|
||||
var registry = (Registry<T>) BuiltInRegistries.REGISTRY.get(id.location());
|
||||
if (registry == null) throw new IllegalArgumentException("Unknown registry " + id);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K> K getRegistryObject(ResourceKey<Registry<K>> registry, ResourceLocation id) {
|
||||
throw new UnsupportedOperationException("Cannot query registry inside tests");
|
||||
public <T> ResourceLocation getRegistryKey(ResourceKey<Registry<T>> registry, T object) {
|
||||
var key = getRegistry(registry).getKey(object);
|
||||
if (key == null) throw new IllegalArgumentException(object + " was not registered in " + registry);
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
|
||||
var value = getRegistry(registry).get(id);
|
||||
if (value == null) throw new IllegalArgumentException(id + " was not registered in " + registry);
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> RegistryWrappers.RegistryWrapper<T> wrap(ResourceKey<Registry<T>> registry) {
|
||||
return new RegistryWrapperImpl<>(registry.location(), getRegistry(registry));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
|
||||
throw new UnsupportedOperationException("Cannot query registries");
|
||||
return getRegistry(registry).get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -239,4 +252,48 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
|
||||
public String getInstalledVersion() {
|
||||
return "1.0";
|
||||
}
|
||||
|
||||
private record RegistryWrapperImpl<T>(
|
||||
ResourceLocation name, Registry<T> registry
|
||||
) implements RegistryWrappers.RegistryWrapper<T> {
|
||||
@Override
|
||||
public int getId(T object) {
|
||||
return registry.getId(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getKey(T object) {
|
||||
var key = registry.getKey(object);
|
||||
if (key == null) throw new IllegalArgumentException(object + " was not registered in " + name);
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(ResourceLocation location) {
|
||||
var object = registry.get(location);
|
||||
if (object == null) throw new IllegalArgumentException(location + " was not registered in " + name);
|
||||
return object;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public T tryGet(ResourceLocation location) {
|
||||
return registry.get(location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable T byId(int id) {
|
||||
return registry.byId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return registry.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return registry.iterator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,67 @@
|
||||
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
|
||||
//
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dan200.computercraft.test.shared;
|
||||
|
||||
import dan200.computercraft.shared.platform.RegistryWrappers;
|
||||
import net.jqwik.api.Arbitraries;
|
||||
import net.jqwik.api.Arbitrary;
|
||||
import net.jqwik.api.Combinators;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link Arbitrary} implementations for Minecraft types.
|
||||
*/
|
||||
public final class MinecraftArbitraries {
|
||||
public static <T> Arbitrary<T> ofRegistry(RegistryWrappers.RegistryWrapper<T> registry) {
|
||||
return Arbitraries.of(registry.stream().toList());
|
||||
}
|
||||
|
||||
public static <T> Arbitrary<TagKey<T>> tagKey(ResourceKey<? extends Registry<T>> registry) {
|
||||
return resourceLocation().map(x -> TagKey.create(registry, x));
|
||||
}
|
||||
|
||||
public static Arbitrary<Item> item() {
|
||||
return ofRegistry(RegistryWrappers.ITEMS);
|
||||
}
|
||||
|
||||
public static Arbitrary<ItemStack> nonEmptyItemStack() {
|
||||
return Combinators.combine(item().filter(x -> x != Items.AIR), Arbitraries.integers().between(1, 64)).as(ItemStack::new);
|
||||
}
|
||||
|
||||
public static Arbitrary<ItemStack> itemStack() {
|
||||
return Arbitraries.oneOf(List.of(Arbitraries.just(ItemStack.EMPTY), nonEmptyItemStack()));
|
||||
}
|
||||
|
||||
public static Arbitrary<BlockPos> blockPos() {
|
||||
// BlockPos has a maximum range that can be sent over the network - use those.
|
||||
var xz = Arbitraries.integers().between(-3_000_000, -3_000_000);
|
||||
var y = Arbitraries.integers().between(-1024, 1024);
|
||||
return Combinators.combine(xz, y, xz).as(BlockPos::new);
|
||||
}
|
||||
|
||||
public static Arbitrary<ResourceLocation> resourceLocation() {
|
||||
return Combinators.combine(
|
||||
Arbitraries.strings().ofMinLength(1).withChars("abcdefghijklmnopqrstuvwxyz_"),
|
||||
Arbitraries.strings().ofMinLength(1).withChars("abcdefghijklmnopqrstuvwxyz_-/")
|
||||
).as(ResourceLocation::new);
|
||||
}
|
||||
|
||||
public static Arbitrary<SoundEvent> soundEvent() {
|
||||
return Arbitraries.oneOf(List.of(
|
||||
resourceLocation().map(SoundEvent::createVariableRangeEvent),
|
||||
Combinators.combine(resourceLocation(), Arbitraries.floats()).as(SoundEvent::createFixedRangeEvent)
|
||||
));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user