Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for dough-data #65

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
24e1a1b
Add unit tests for CopyUtils
md5sha256 Jul 29, 2021
025f787
Merge branch 'main' into tests/copy-utils
md5sha256 Jul 29, 2021
dc88fb3
Make DummyData package-private
md5sha256 Jul 29, 2021
97136c2
Add unit tests for the dough-data blocks api
md5sha256 Aug 5, 2021
97ec643
Add unit tests for CopyUtils
md5sha256 Jul 29, 2021
a29531d
Make DummyData package-private
md5sha256 Jul 29, 2021
2cad21d
Add unit tests for the dough-data blocks api
md5sha256 Aug 5, 2021
0cce86f
Merge branch 'tests/dough-data' of https://github.com/baked-libs/doug…
md5sha256 Aug 5, 2021
7b16a0b
Address PR changes
md5sha256 Aug 6, 2021
257e968
Add tests for WeightedNode
md5sha256 Aug 6, 2021
e951ac6
Add CollectionTestUtils and TestLoopIterator
md5sha256 Aug 6, 2021
de407ed
Add test for empty iterator in TestLoopIterator
md5sha256 Aug 6, 2021
3079c80
Add tests for Pair and OptionalPair
md5sha256 Aug 6, 2021
b2db9d3
Add tests for OptionalMap
md5sha256 Aug 7, 2021
11c20de
Add tests for KeyMap
md5sha256 Aug 7, 2021
3b00a9b
Add tests for TriStateOptional
md5sha256 Aug 7, 2021
6651fb8
Add tests for RandomizedSet
md5sha256 Aug 8, 2021
014f71b
Fix code smells
md5sha256 Aug 8, 2021
fa27c37
Merge branch 'main' into tests/dough-data
md5sha256 Aug 8, 2021
13b419c
Fix package
md5sha256 Aug 8, 2021
aba3d52
Initial work on TestPersistentUUID and fixes for PerisstentUUID
md5sha256 Aug 8, 2021
57daeca
Add tests for PersistentYAMLDataType
md5sha256 Aug 8, 2021
a6c3243
Fix access modifier
md5sha256 Aug 8, 2021
8710733
Add tests for PersistJsonDataTypea
md5sha256 Aug 8, 2021
58da2fd
Merge branch 'main' into tests/dough-data
TheBusyBiscuit Jan 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Optional<T> find(@Nonnull Predicate<T> predicate) {
int start = index;

T current = next();
while (index != start || predicate.test(current)) {
while (index != start && !predicate.test(current)) {
current = next();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private PersistentUUIDDataType() {}
Validate.notNull(ints, "The provided integer array cannot be null!");
Validate.isTrue(ints.length == 4, "The integer array must have a length of 4.");

return new UUID(ints[0] | ints[1] & 0xFFFFFFFFL, ints[2] | ints[3] & 0xFFFFFFFFL);
return new UUID(((long) ints[0] << 32) | ints[1] & 0xFFFFFFFFL, ((long) ints[2] << 32) | ints[3] & 0xFFFFFFFFL);
}

public static @Nonnull int[] toIntArray(@Nonnull UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.github.bakedlibs.dough.blocks;

import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.WorldMock;
import org.bukkit.Location;
import org.bukkit.World;
import org.junit.jupiter.api.*;

class TestBlockPosition {

@BeforeAll
static void init() {
MockBukkit.mock();
}

@AfterAll
static void teardown() {
MockBukkit.unmock();
}

@Test
@DisplayName("Test the coordinate getters")
void testCoordinateGetters() {
int x = 10;
int y = 1000;
int z = 50;
World world = new WorldMock();
BlockPosition blockPosition = new BlockPosition(world, x, y, z);
Assertions.assertEquals(x, blockPosition.getX());
Assertions.assertEquals(y, blockPosition.getY());
Assertions.assertEquals(z, blockPosition.getZ());
}

@Test
void testCloning() {
int x = 10;
int y = 1000;
int z = 50;
World world = new WorldMock();
BlockPosition blockPosition = new BlockPosition(world, x, y, z);
Location location = blockPosition.toLocation();
Assertions.assertEquals(blockPosition.getPosition(), BlockPosition.getAsLong(location));
Assertions.assertEquals(blockPosition, new BlockPosition(world, blockPosition.getPosition()));
Assertions.assertEquals(x, location.getBlockX());
Assertions.assertEquals(y, location.getBlockY());
Assertions.assertEquals(z, location.getBlockZ());
Assertions.assertEquals(blockPosition, new BlockPosition(location));
Assertions.assertEquals(blockPosition.hashCode(), new BlockPosition(location).hashCode());
Assertions.assertEquals(x >> 4, blockPosition.getChunkX());
Assertions.assertEquals(z >> 4, blockPosition.getChunkZ());
}

@Test
void testInvalidWorld() {
World world = new WorldMock();
BlockPosition position = new BlockPosition(world, 1, 1, 1);
BlockPosition copy = new BlockPosition(world, 1, 1, 1);
world = null;
// Force garbage collection
System.gc();
String msg = "The reference of this BlockPositions world has been cleared";
Assertions.assertThrows(IllegalStateException.class, position::getWorld, msg);
Assertions.assertThrows(IllegalStateException.class, position::getBlock, msg);
Assertions.assertThrows(IllegalStateException.class, position::getChunk, msg);
// If the world is un-loaded the equals method should always return false.
Assertions.assertNotEquals(position, copy);
}

@Test
void testInvalidEquality() {
World world = new WorldMock();
Assertions.assertNotEquals(1, new BlockPosition(world, 1, 1, 1));
}

@Test
void testToString() {
String name = "world";
WorldMock world = new WorldMock();
world.setName(name);
BlockPosition position = new BlockPosition(world, 1, 1, 1);
String expected = "BlockPosition(world=world, x=1, y=1, z=1, position=" + position.getPosition() + ")";
Assertions.assertEquals(expected, position.toString());
String expectedNoRef = "BlockPosition(world=<no reference>, x=1, y=1, z=1, position=" + position.getPosition() + ")";
world = null;
// Force GC
System.gc();
Assertions.assertEquals(expectedNoRef, position.toString());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.github.bakedlibs.dough.blocks;

import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.WorldMock;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.junit.jupiter.api.*;

class TestChunkPosition {

@BeforeAll
static void init() {
MockBukkit.mock();
}

@AfterAll
static void teardown() {
MockBukkit.unmock();
}

@Test
@DisplayName("Test the coordinate getters")
void testCoordinateGetters() {
int x = 10;
int z = 50;
World world = new WorldMock();
ChunkPosition chunkPosition = new ChunkPosition(world, x, z);
Assertions.assertEquals(x, chunkPosition.getX());
Assertions.assertEquals(z, chunkPosition.getZ());
}

@Test
void testConstructors() {
int x = 10;
int z = 50;
World world = new WorldMock();
ChunkPosition chunkPosition = new ChunkPosition(world, x, z);
ChunkPosition fromLocation = new ChunkPosition(new Location(world, x << 4, 1, z << 4));
Assertions.assertEquals(chunkPosition, fromLocation);
Assertions.assertEquals(chunkPosition.hashCode(), fromLocation.hashCode());
Assertions.assertEquals(chunkPosition, new ChunkPosition(world.getChunkAt(x, z)));
Assertions.assertEquals(chunkPosition.getPosition(), ChunkPosition.getAsLong(x, z));
Assertions.assertEquals(chunkPosition, new ChunkPosition(world, chunkPosition.getPosition()));
}

@Test
void testInvalidWorld() {
World world = new WorldMock();
ChunkPosition position = new ChunkPosition(world, 1, 1);
ChunkPosition copy = new ChunkPosition(world, 1, 1);
world = null;
// Force garbage collection
System.gc();
String msg = "The reference of this ChunkPositions world has been cleared";
Assertions.assertThrows(IllegalStateException.class, position::getWorld, msg);
Assertions.assertThrows(IllegalStateException.class, position::getChunk, msg);
// If the world is un-loaded the equals method should always return false.
Assertions.assertNotEquals(position, copy);
}

@Test
void testInvalidEquality() {
World world = new WorldMock();
Assertions.assertNotEquals(1, new ChunkPosition(world, 1, 1));
}

@Test
void testToString() {
String name = "world";
WorldMock world = new WorldMock();
world.setName(name);
ChunkPosition position = new ChunkPosition(world, 1, 1);
String expected = "ChunkPosition(world=world, x=1, z=1, position=" + position.getPosition() + ")";
Assertions.assertEquals(expected, position.toString());
String expectedNoRef = "ChunkPosition(world=<no reference>, x=1, z=1, position=" + position.getPosition() + ")";
world = null;
// Force GC
System.gc();
Assertions.assertEquals(expectedNoRef, position.toString());
}

@Test
void testIsLoaded() {
World world = new WorldMock();
Chunk chunk = world.getChunkAt(1, 1);
ChunkPosition position = new ChunkPosition(chunk);
Assertions.assertEquals(chunk.isLoaded(), position.isLoaded());
chunk = null;
world = null;
// Force GC
System.gc();
Assertions.assertFalse(position.isLoaded());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.bakedlibs.dough.collections;

import org.junit.jupiter.api.Assertions;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Map;

public class CollectionTestUtils {

static <T> void assertHaveEqualElements(@Nonnull Collection<T> c1, @Nonnull Collection<T> c2) {
Assertions.assertEquals(c1.size(), c2.size());
Assertions.assertTrue(c1.containsAll(c2));
}

static <K, V> void assertHaveEqualElements(@Nonnull Map<K, V> m1, @Nonnull Map<K, V> m2) {
Assertions.assertEquals(m1.size(), m2.size());
for (Map.Entry<K, V> entry : m1.entrySet()) {
Assertions.assertEquals(m2.get(entry.getKey()), entry.getValue());
}
}

static <T> void assertHaveClonedElements(@Nonnull Collection<T> c1, @Nonnull Collection<T> c2) {
Assertions.assertEquals(c1.size(), c2.size());
for (T t1 : c1) {
for (T t2 : c2) {
// If they are the same, it did not clone and thus we fail.
Assertions.assertNotSame(t1, t2);
if (t1.equals(t2)) {
// If they are equal, the element has been cloned, thus, we break and check the next element.
break;
}
}
}
}

static <K, V> void assertHaveClonedElements(@Nonnull Map<K, V> m1, @Nonnull Map<K, V> m2) {
Assertions.assertEquals(m1.size(), m2.size());
for (Map.Entry<K, V> entry : m1.entrySet()) {
V otherValue = m2.get(entry.getKey());
// If they are the same, it did not clone and thus we fail.
Assertions.assertNotSame(entry.getValue(), otherValue);
if (entry.getValue().equals(otherValue)) {
// If they are equal, the element has been cloned, thus, we break and check the next element.
break;
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.bakedlibs.dough.collections;

import javax.annotation.Nonnull;

class DummyData implements Cloneable {

private final int value;

DummyData(int value) {
this.value = value;
}

private DummyData(@Nonnull DummyData other) {
this.value = other.value;
}

@Override
public @Nonnull DummyData clone() {
try {
super.clone();
} catch (CloneNotSupportedException ex) {
ex.printStackTrace();
}
return new DummyData(this);
}

@Override
public boolean equals(final Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

DummyData dummyData = (DummyData) o;

return value == dummyData.value;
}

@Override
public int hashCode() {
return value;
}

@Override
public String toString() {
return "DummyData{" + "value=" + value + '}';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.bakedlibs.dough.collections;

import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;

import javax.annotation.Nonnull;

public class DummyKeyed implements Keyed {

private final NamespacedKey key;

public DummyKeyed(@Nonnull NamespacedKey key) {
this.key = key;
}

@Override
public @Nonnull NamespacedKey getKey() {
return key;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.bakedlibs.dough.collections;

import org.junit.jupiter.api.Assertions;

public class PairUtils {

static <P, S> void assertValues(Pair<P, S> pair, P expectedPrimary, S expectedSecondary) {
Assertions.assertSame(expectedPrimary, pair.getFirstValue());
Assertions.assertSame(expectedSecondary, pair.getSecondValue());
}

static <P, S> void assertValues(OptionalPair<P, S> pair, P expectedPrimary, S expectedSecondary) {
if (expectedPrimary == null) {
Assertions.assertFalse(pair.getFirstValue().isPresent());
} else {
Assertions.assertEquals(expectedPrimary, pair.getFirstValue().orElse(null));
}
if (expectedSecondary == null) {
Assertions.assertFalse(pair.getSecondValue().isPresent());
} else {
Assertions.assertEquals(expectedSecondary, pair.getSecondValue().orElse(null));
}
}

}
Loading