1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Update unit tests for ItemStackWrapper

This commit is contained in:
Andrew Wong 2021-05-18 14:32:40 +02:00
parent 3dfc83ac6a
commit 7085f0d0a8
No known key found for this signature in database
GPG Key ID: F59F2C3DA0936DE4

View File

@ -34,7 +34,7 @@ class TestItemStackWrapper {
@DisplayName("Test if an ItemStackWrappers can be compared properly (With ItemMeta)") @DisplayName("Test if an ItemStackWrappers can be compared properly (With ItemMeta)")
void testEqualityWithItemMeta() { void testEqualityWithItemMeta() {
ItemStack item = new CustomItem(Material.LAVA_BUCKET, "&4SuperHot.exe", "", "&6Hello"); ItemStack item = new CustomItem(Material.LAVA_BUCKET, "&4SuperHot.exe", "", "&6Hello");
ItemStackWrapper wrapper = new ItemStackWrapper(item); ItemStackWrapper wrapper = ItemStackWrapper.ofItem(item);
Assertions.assertEquals(item.getType(), wrapper.getType()); Assertions.assertEquals(item.getType(), wrapper.getType());
Assertions.assertEquals(item.hasItemMeta(), wrapper.hasItemMeta()); Assertions.assertEquals(item.hasItemMeta(), wrapper.hasItemMeta());
@ -46,7 +46,7 @@ class TestItemStackWrapper {
@DisplayName("Test if an ItemStackWrappers can be compared properly (No ItemMeta)") @DisplayName("Test if an ItemStackWrappers can be compared properly (No ItemMeta)")
void testEqualityWithoutItemMeta() { void testEqualityWithoutItemMeta() {
ItemStack item = new ItemStack(Material.DIAMOND_AXE); ItemStack item = new ItemStack(Material.DIAMOND_AXE);
ItemStackWrapper wrapper = new ItemStackWrapper(item); ItemStackWrapper wrapper = ItemStackWrapper.ofItem(item);
Assertions.assertEquals(item.getType(), wrapper.getType()); Assertions.assertEquals(item.getType(), wrapper.getType());
Assertions.assertEquals(item.hasItemMeta(), wrapper.hasItemMeta()); Assertions.assertEquals(item.hasItemMeta(), wrapper.hasItemMeta());
@ -58,7 +58,7 @@ class TestItemStackWrapper {
@DisplayName("Test if an ItemStackWrapper is immutable") @DisplayName("Test if an ItemStackWrapper is immutable")
void testImmutability() { void testImmutability() {
ItemStack item = new CustomItem(Material.LAVA_BUCKET, "&4SuperHot.exe", "", "&6Hello"); ItemStack item = new CustomItem(Material.LAVA_BUCKET, "&4SuperHot.exe", "", "&6Hello");
ItemStackWrapper wrapper = new ItemStackWrapper(item); ItemStackWrapper wrapper = ItemStackWrapper.ofItem(item);
Assertions.assertThrows(UnsupportedOperationException.class, () -> wrapper.setType(Material.BEDROCK)); Assertions.assertThrows(UnsupportedOperationException.class, () -> wrapper.setType(Material.BEDROCK));
Assertions.assertThrows(UnsupportedOperationException.class, () -> wrapper.setAmount(3)); Assertions.assertThrows(UnsupportedOperationException.class, () -> wrapper.setAmount(3));
@ -69,6 +69,20 @@ class TestItemStackWrapper {
Assertions.assertThrows(UnsupportedOperationException.class, () -> wrapper.equals(wrapper)); Assertions.assertThrows(UnsupportedOperationException.class, () -> wrapper.equals(wrapper));
} }
@Test
@DisplayName("Test if the ItemStackWrapper static method constructors are checking for nested wrapping properly")
void testWrapperChecking() {
ItemStack item = new CustomItem(Material.IRON_INGOT, "A Name", "line 1", "line2");
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(item);
ItemStackWrapper secondWrap = ItemStackWrapper.ofItem(wrapper);
// We want to check that the wrapper returned is of reference equality
Assertions.assertTrue(wrapper == secondWrap);
ItemStackWrapper forceSecondWrap = ItemStackWrapper.forceWrapItem(wrapper);
// Want to check that the wrapper returned is of different reference equality
Assertions.assertFalse(wrapper == forceSecondWrap);
assertWrapped(wrapper, forceSecondWrap);
}
@Test @Test
@DisplayName("Test wrapping an ItemStack Array") @DisplayName("Test wrapping an ItemStack Array")
void testWrapArray() { void testWrapArray() {
@ -80,19 +94,35 @@ class TestItemStackWrapper {
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
assertWrapped(items[i], wrappers[i]); assertWrapped(items[i], wrappers[i]);
} }
ItemStackWrapper[] nestedWrap = ItemStackWrapper.wrapArray(wrappers);
Assertions.assertEquals(wrappers.length, nestedWrap.length);
for (int i = 0; i < wrappers.length; i++) {
// We want to check that the wrapper returned is of reference equality
Assertions.assertTrue(wrappers[i] == nestedWrap[i]);
}
} }
@Test @Test
@DisplayName("Test wrapping an ItemStack List") @DisplayName("Test wrapping an ItemStack List")
void testWrapList() { void testWrapList() {
List<ItemStack> items = Arrays.asList(new ItemStack(Material.DIAMOND), null, new ItemStack(Material.EMERALD), new CustomItem(Material.REDSTONE, "&4Firey thing", "with lore :o")); List<ItemStack> items = Arrays.asList(new ItemStack(Material.DIAMOND), null, new ItemStack(Material.EMERALD), new CustomItem(Material.REDSTONE, "&4Firey thing", "with lore :o"));
List<ItemStackWrapper> wrappers = ItemStackWrapper.wrapList(items); List<? extends ItemStack> wrappers = ItemStackWrapper.wrapList(items);
Assertions.assertEquals(items.size(), wrappers.size()); Assertions.assertEquals(items.size(), wrappers.size());
for (int i = 0; i < items.size(); i++) { for (int i = 0; i < items.size(); i++) {
assertWrapped(items.get(i), wrappers.get(i)); assertWrapped(items.get(i), wrappers.get(i));
} }
final List<ItemStackWrapper> nestedWrappers = ItemStackWrapper.wrapList((List<ItemStack>) wrappers);
Assertions.assertEquals(wrappers.size(), nestedWrappers.size());
for (int i = 0; i < items.size(); i++) {
// We want to check that the wrapper returned is of reference equality
Assertions.assertTrue(wrappers.get(i) == nestedWrappers.get(i));
}
} }
private void assertWrapped(ItemStack expected, ItemStack actual) { private void assertWrapped(ItemStack expected, ItemStack actual) {