From 551e770f5f981053885e2e1a043b4ecb54a80aef Mon Sep 17 00:00:00 2001 From: Ben Woodworth Date: Thu, 2 Jul 2020 09:34:13 +1000 Subject: [PATCH] SPIGOT-5900: Add test for the order of Material enum entries --- src/test/java/org/bukkit/MaterialTest.java | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/java/org/bukkit/MaterialTest.java b/src/test/java/org/bukkit/MaterialTest.java index 3b905b73af..efac35cb7b 100644 --- a/src/test/java/org/bukkit/MaterialTest.java +++ b/src/test/java/org/bukkit/MaterialTest.java @@ -3,9 +3,14 @@ package org.bukkit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import com.google.common.collect.Maps; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.stream.StreamSupport; import net.minecraft.server.IRegistry; import net.minecraft.server.Item; import net.minecraft.server.MinecraftKey; @@ -43,4 +48,28 @@ public class MaterialTest extends AbstractTestingBase { assertThat(materials, is(Collections.EMPTY_MAP)); } + + @Test + public void verifyMaterialOrder() { + List expectedOrder = new ArrayList<>(Material.values().length); + + // Start with items in the same order as IRegistry.ITEM + StreamSupport.stream(IRegistry.ITEM.spliterator(), false) + .map(CraftMagicNumbers::getMaterial) + .forEach(expectedOrder::add); + + // Then non-item blocks in the same order as IRegistry.BLOCK + StreamSupport.stream(IRegistry.BLOCK.spliterator(), false) + .map(CraftMagicNumbers::getMaterial) + .filter(block -> !block.isItem()) + .forEach(expectedOrder::add); + + // Then legacy materials in order of ID + Arrays.stream(Material.values()) + .filter(Material::isLegacy) + .sorted(Comparator.comparingInt(Material::getId)) + .forEach(expectedOrder::add); + + assertArrayEquals(Material.values(), expectedOrder.toArray()); + } }