Remove several unused test files

This commit is contained in:
Jonathan Coates 2024-04-28 19:54:39 +01:00
parent bf203bb1f3
commit 0aca6a4dc9
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
4 changed files with 0 additions and 382 deletions

View File

@ -1,53 +0,0 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.test.shared;
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.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
/**
* {@link Arbitrary} implementations for Minecraft types.
*/
public final class MinecraftArbitraries {
public static <T> Arbitrary<T> ofRegistry(Registry<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(BuiltInRegistries.ITEM);
}
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<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);
}
}

View File

@ -1,26 +0,0 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.test.shared;
import dan200.computercraft.test.core.StructuralEquality;
import net.minecraft.world.item.ItemStack;
import org.hamcrest.Description;
/**
* {@link StructuralEquality} implementations for Minecraft types.
*/
public class MinecraftEqualities {
public static final StructuralEquality<ItemStack> itemStack = new StructuralEquality<>() {
@Override
public boolean equals(ItemStack left, ItemStack right) {
return ItemStack.isSameItemSameComponents(left, right) && left.getCount() == right.getCount();
}
@Override
public void describe(Description description, ItemStack object) {
description.appendValue(object).appendValue(object.getComponentsPatch());
}
};
}

View File

@ -1,153 +0,0 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.test.core;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
/**
* Concrete implementations for {@link StructuralEquality}.
*/
final class StructuralEqualities {
static final DefaultEquality DEFAULT = new DefaultEquality();
private StructuralEqualities() {
}
static <T> void describeNullable(Description description, StructuralEquality<T> equality, @Nullable T value) {
if (value == null) {
description.appendText("null");
} else {
equality.describe(description, value);
}
}
static final class DefaultEquality implements StructuralEquality<Object> {
private DefaultEquality() {
}
@Override
public boolean equals(Object left, Object right) {
return Objects.equals(left, right);
}
@Override
public void describe(Description description, Object object) {
description.appendValue(object);
}
}
static final class AllEquality<T> implements StructuralEquality<T> {
private final List<StructuralEquality<T>> equalities;
AllEquality(List<StructuralEquality<T>> equalities) {
this.equalities = equalities;
}
@Override
public boolean equals(T left, T right) {
return equalities.stream().allMatch(x -> x.equals(left, right));
}
@Override
public void describe(Description description, T object) {
description.appendText("{");
var separator = false;
for (var equality : equalities) {
if (separator) description.appendText(", ");
separator = true;
equality.describe(description, object);
}
description.appendText("}");
}
}
static final class FeatureEquality<T, U> implements StructuralEquality<T> {
private final String desc;
private final Function<T, U> get;
private final StructuralEquality<? super U> inner;
FeatureEquality(String desc, Function<T, U> get, StructuralEquality<? super U> inner) {
this.desc = desc;
this.inner = inner;
this.get = get;
}
private @Nullable U get(T value) {
return get.apply(value);
}
@Override
public boolean equals(T left, T right) {
var leftInner = get(left);
var rightInner = get(right);
if (leftInner == null) return rightInner == null;
if (rightInner == null) return false;
return inner.equals(leftInner, rightInner);
}
@Override
public void describe(Description description, T object) {
description.appendText(desc).appendText("=>");
describeNullable(description, inner, get.apply(object));
}
}
record ListEquality<T>(StructuralEquality<T> equality) implements StructuralEquality<List<T>> {
@Override
public boolean equals(List<T> left, List<T> right) {
if (left.size() != right.size()) return false;
for (var i = 0; i < left.size(); i++) {
if (!equality.equals(left.get(i), right.get(i))) return false;
}
return true;
}
@Override
public void describe(Description description, List<T> object) {
description.appendText("[");
var separator = false;
for (var value : object) {
if (separator) description.appendText(", ");
separator = true;
equality.describe(description, value);
}
description.appendText("]");
}
}
static final class EqualityMatcher<T> extends TypeSafeMatcher<T> {
private final StructuralEquality<T> equality;
private final T equalTo;
EqualityMatcher(Class<T> klass, StructuralEquality<T> equality, T equalTo) {
super(klass);
this.equality = equality;
this.equalTo = equalTo;
}
@Override
public boolean matchesSafely(T actual) {
if (actual == null) return false;
return equality.equals(actual, equalTo);
}
@Override
public void describeTo(Description description) {
equality.describe(description, equalTo);
}
@Override
protected void describeMismatchSafely(T value, Description description) {
description.appendText("was ");
describeNullable(description, equality, value);
}
}
}

View File

@ -1,150 +0,0 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.test.core;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Function;
/**
* A basic mechanism for checking equality of two objects, suitable for use with Hamcrest {@linkplain Matcher matchers}.
* <p>
* This is intended for when an object does not override {@link Object#equals(Object)} itself,
*
* @param <T> The type of the value to check.
*/
public interface StructuralEquality<T> {
/**
* Check if two non-null values are equal.
*
* @param left The first value to check.
* @param right The second value to check.
* @return If these values are equal.
*/
boolean equals(T left, T right);
/**
* Describe this value.
*
* @param description The description to write to.
* @param object The object to describe.
*/
void describe(Description description, T object);
/**
* Lift this equality to a list of values.
*
* @return A equality for a list of values.
*/
default StructuralEquality<List<T>> list() {
return new StructuralEqualities.ListEquality<>(this);
}
/**
* Convert this equality instance to a {@link Matcher}.
*
* @param klass The expected type of this object.
* @param expected The expected value.
* @return A matcher which checks if its input is equal to the given value.
*/
default Matcher<T> asMatcher(Class<T> klass, T expected) {
return new StructuralEqualities.EqualityMatcher<>(klass, this, expected);
}
/**
* The default {@link StructuralEquality} implementation, which just uses {@link Object#equals(Object)}.
*
* @return The default equality.
*/
static StructuralEquality<Object> defaultEquality() {
return StructuralEqualities.DEFAULT;
}
/**
* Checks all equalities match. This is intended for use with {@link #at(String, Function, StructuralEquality)}, to
* check the structure of an object.
*
* @param equalities The equalities which should match.
* @param <T> The type of the object to match.
* @return The newly created {@link StructuralEquality} object.
*/
@SafeVarargs
@SuppressWarnings("varargs")
static <T> StructuralEquality<T> all(StructuralEquality<T>... equalities) {
return new StructuralEqualities.AllEquality<>(List.of(equalities));
}
/**
* Create an equality which checks if {@code f(x) = f(y)}, where {@code f} is some projection function (such as
* reading a field).
*
* @param desc The description of this projection.
* @param project The projection function.
* @param inner The inner equality,
* @param <T> The type of the object to check.
* @param <U> The type of the "inner" object, projected out by {@code f}
* @return The newly created {@link StructuralEquality} object.
*/
static <T, U> StructuralEquality<T> at(String desc, Function<T, U> project, StructuralEquality<? super U> inner) {
return new StructuralEqualities.FeatureEquality<>(desc, project, inner);
}
/**
* A simple version of {@link #at(String, Function, StructuralEquality)} which uses {@link #defaultEquality()} .
*
* @param desc The description of this projection.
* @param project The projection function.
* @param <T> The type of the object to check.
* @param <U> The type of the "inner" object, projected out by {@code f}
* @return The newly created {@link StructuralEquality} object.
*/
static <T, U> StructuralEquality<T> at(String desc, Function<T, U> project) {
return at(desc, project, defaultEquality());
}
/**
* A hacky version of {@link #at(String, Function, StructuralEquality)} which projects out a private field.
*
* @param klass The class where the field is defined.
* @param fieldName The name of the field.
* @param inner The inner equality.
* @param <T> The type of the object to check.
* @param <U> The type of the field's.
* @return The newly created {@link StructuralEquality} object.
*/
@SuppressWarnings("unchecked")
static <T, U> StructuralEquality<T> field(Class<T> klass, String fieldName, StructuralEquality<U> inner) {
Field field;
try {
field = klass.getDeclaredField(fieldName);
field.setAccessible(true);
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException("Cannot find field", e);
}
return new StructuralEqualities.FeatureEquality<>(fieldName, x -> {
try {
return (U) field.get(x);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Cannot read field", e);
}
}, inner);
}
/**
* A hacky version of {@link #at(String, Function)} which projects out a private field.
*
* @param klass The class where the field is defined.
* @param fieldName The name of the field.
* @param <T> The type of the object to check.
* @return The newly created {@link StructuralEquality} object.
*/
static <T> StructuralEquality<T> field(Class<T> klass, String fieldName) {
return field(klass, fieldName, defaultEquality());
}
}