From 0aca6a4dc95024eb7ea9cb871f6ffe31b22aae3f Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 28 Apr 2024 19:54:39 +0100 Subject: [PATCH] Remove several unused test files --- .../test/shared/MinecraftArbitraries.java | 53 ------ .../test/shared/MinecraftEqualities.java | 26 --- .../test/core/StructuralEqualities.java | 153 ------------------ .../test/core/StructuralEquality.java | 150 ----------------- 4 files changed, 382 deletions(-) delete mode 100644 projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftArbitraries.java delete mode 100644 projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftEqualities.java delete mode 100644 projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEqualities.java delete mode 100644 projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEquality.java diff --git a/projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftArbitraries.java b/projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftArbitraries.java deleted file mode 100644 index 08473e1b2..000000000 --- a/projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftArbitraries.java +++ /dev/null @@ -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 Arbitrary ofRegistry(Registry registry) { - return Arbitraries.of(registry.stream().toList()); - } - - public static Arbitrary> tagKey(ResourceKey> registry) { - return resourceLocation().map(x -> TagKey.create(registry, x)); - } - - public static Arbitrary item() { - return ofRegistry(BuiltInRegistries.ITEM); - } - - public static Arbitrary nonEmptyItemStack() { - return Combinators.combine(item().filter(x -> x != Items.AIR), Arbitraries.integers().between(1, 64)).as(ItemStack::new); - } - - public static Arbitrary 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() { - return Combinators.combine( - Arbitraries.strings().ofMinLength(1).withChars("abcdefghijklmnopqrstuvwxyz_"), - Arbitraries.strings().ofMinLength(1).withChars("abcdefghijklmnopqrstuvwxyz_-/") - ).as(ResourceLocation::new); - } -} diff --git a/projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftEqualities.java b/projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftEqualities.java deleted file mode 100644 index 342623048..000000000 --- a/projects/common/src/testFixtures/java/dan200/computercraft/test/shared/MinecraftEqualities.java +++ /dev/null @@ -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 = 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()); - } - }; -} diff --git a/projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEqualities.java b/projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEqualities.java deleted file mode 100644 index 9517cc526..000000000 --- a/projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEqualities.java +++ /dev/null @@ -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 void describeNullable(Description description, StructuralEquality equality, @Nullable T value) { - if (value == null) { - description.appendText("null"); - } else { - equality.describe(description, value); - } - } - - static final class DefaultEquality implements StructuralEquality { - 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 implements StructuralEquality { - private final List> equalities; - - AllEquality(List> 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 implements StructuralEquality { - private final String desc; - private final Function get; - private final StructuralEquality inner; - - FeatureEquality(String desc, Function get, StructuralEquality 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(StructuralEquality equality) implements StructuralEquality> { - @Override - public boolean equals(List left, List 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 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 extends TypeSafeMatcher { - private final StructuralEquality equality; - private final T equalTo; - - EqualityMatcher(Class klass, StructuralEquality 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); - } - } -} diff --git a/projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEquality.java b/projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEquality.java deleted file mode 100644 index 5045ebf43..000000000 --- a/projects/core/src/testFixtures/java/dan200/computercraft/test/core/StructuralEquality.java +++ /dev/null @@ -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}. - *

- * This is intended for when an object does not override {@link Object#equals(Object)} itself, - * - * @param The type of the value to check. - */ -public interface StructuralEquality { - /** - * 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() { - 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 asMatcher(Class 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 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 The type of the object to match. - * @return The newly created {@link StructuralEquality} object. - */ - @SafeVarargs - @SuppressWarnings("varargs") - static StructuralEquality all(StructuralEquality... 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 The type of the object to check. - * @param The type of the "inner" object, projected out by {@code f} - * @return The newly created {@link StructuralEquality} object. - */ - static StructuralEquality at(String desc, Function project, StructuralEquality 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 The type of the object to check. - * @param The type of the "inner" object, projected out by {@code f} - * @return The newly created {@link StructuralEquality} object. - */ - static StructuralEquality at(String desc, Function 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 The type of the object to check. - * @param The type of the field's. - * @return The newly created {@link StructuralEquality} object. - */ - @SuppressWarnings("unchecked") - static StructuralEquality field(Class klass, String fieldName, StructuralEquality 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 The type of the object to check. - * @return The newly created {@link StructuralEquality} object. - */ - static StructuralEquality field(Class klass, String fieldName) { - return field(klass, fieldName, defaultEquality()); - } -}