1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2026-01-03 10:59:03 +00:00

Add test for potion durations

This commit is contained in:
Jonathan Coates
2025-12-24 08:51:15 +00:00
parent 90e7307fb4
commit 24fd27d2a3
4 changed files with 98 additions and 2 deletions

View File

@@ -93,7 +93,7 @@ item has a certain tag:
```lua
--- Check if the item in the turtle's inventory is a log.
local function is_log(slot)
local ok, block = turtle.getItemDetails(slot, true)
local ok, block = turtle.getItemDetail(slot, true)
return ok and block.tags["minecraft:logs"]
end
```

View File

@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2025 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.details;
import dan200.computercraft.api.detail.VanillaDetailRegistries;
import dan200.computercraft.test.core.CustomMatchers;
import dan200.computercraft.test.shared.WithMinecraft;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
@WithMinecraft
class ItemDetailsTest {
@BeforeAll
public static void setup() {
VanillaDetailRegistries.ITEM_STACK.addProvider(ItemDetails::fill);
}
/**
* Test that all potion-imbued items (potions, throwables and arrows) have the correct duration.
*/
@Test
public void testPotionDurations() {
assertThat(
VanillaDetailRegistries.ITEM_STACK.getDetails(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.LONG_NIGHT_VISION)),
containsEntryWith("potionEffects", contains(allOf(containsEntry("name", "minecraft:night_vision"), containsEntry("duration", 480.0))))
);
assertThat(
VanillaDetailRegistries.ITEM_STACK.getDetails(PotionUtils.setPotion(new ItemStack(Items.LINGERING_POTION), Potions.LONG_NIGHT_VISION)),
containsEntryWith("potionEffects", contains(allOf(containsEntry("name", "minecraft:night_vision"), containsEntry("duration", 120.0))))
);
assertThat(
VanillaDetailRegistries.ITEM_STACK.getDetails(PotionUtils.setPotion(new ItemStack(Items.SPLASH_POTION), Potions.LONG_NIGHT_VISION)),
containsEntryWith("potionEffects", contains(allOf(containsEntry("name", "minecraft:night_vision"), containsEntry("duration", 480.0))))
);
assertThat(
VanillaDetailRegistries.ITEM_STACK.getDetails(PotionUtils.setPotion(new ItemStack(Items.TIPPED_ARROW), Potions.LONG_NIGHT_VISION)),
containsEntryWith("potionEffects", contains(allOf(containsEntry("name", "minecraft:night_vision"), containsEntry("duration", 60.0))))
);
}
private static Matcher<Map<? extends String, ?>> containsEntry(String key, Object value) {
return CustomMatchers.containsEntry(key, value);
}
@SuppressWarnings("unchecked")
private static Matcher<Map<? extends String, ?>> containsEntryWith(String key, Matcher<?> value) {
return CustomMatchers.containsEntryWith(key, (Matcher<? super Object>) value);
}
}

View File

@@ -430,7 +430,7 @@ class Turtle_Test {
}
/**
* Checks turtles can use IDetailProviders by getting details for a printed page.
* Checks turtles can use [VanillaDetailRegistries.ITEM_STACK] by getting details for a printed page.
*/
@GameTest
fun Item_detail_provider(helper: GameTestHelper) = helper.sequence {

View File

@@ -5,11 +5,14 @@
package dan200.computercraft.test.core;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
public class CustomMatchers {
/**
@@ -25,4 +28,32 @@ public class CustomMatchers {
public static <T> Matcher<Iterable<? extends T>> containsWith(List<T> items, Function<T, Matcher<? super T>> matcher) {
return contains(items.stream().map(matcher).toList());
}
/**
* An alternative to {@link org.hamcrest.Matchers#hasEntry(Object, Object)}, that acts as a projection, rather than
* searching the map.
*
* @param key The key to extract.
* @param value The expected value.
* @param <K> The type of keys in the map.
* @param <V> The type of values in the map.
* @return A matcher that projects out of a map.
*/
public static <K, V> Matcher<Map<? extends K, ? extends V>> containsEntry(K key, V value) {
return containsEntryWith(key, is(value));
}
/**
* An alternative to {@link org.hamcrest.Matchers#hasEntry(Matcher, Matcher)}, that acts as a projection, rather
* than searching the map.
*
* @param key The key to extract.
* @param value The expected value.
* @param <K> The type of keys in the map.
* @param <V> The type of values in the map.
* @return A matcher that projects out of a map.
*/
public static <K, V> Matcher<Map<? extends K, ? extends V>> containsEntryWith(K key, Matcher<? super V> value) {
return ContramapMatcher.contramap(value, new StringDescription().appendValue(key).toString(), x -> x.get(key));
}
}