1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Added more unit tests

This commit is contained in:
TheBusyBiscuit 2021-12-08 18:30:10 +01:00
parent b2e391e099
commit 074571ac22
2 changed files with 93 additions and 0 deletions

View File

@ -72,6 +72,12 @@ public class BiomeMap<T> implements Keyed {
dataMap.putAll(map.dataMap);
}
public boolean remove(@Nonnull Biome biome) {
Validate.notNull(biome, "The biome cannot be null.");
return dataMap.remove(biome) != null;
}
/**
* {@inheritDoc}
*/

View File

@ -2,9 +2,11 @@ package io.github.thebusybiscuit.slimefun4.utils.biomes;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Biome;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@ -69,6 +71,91 @@ class TestBiomeMapParser {
assertMisconfiguration(AS_STRING, "[{\"biomes\": []}]");
}
@Test
@DisplayName("Test Biome entry being misconfigured")
void testInvalidBiomeEntry() {
assertMisconfiguration(AS_STRING, "[{\"value\": \"hello\", \"biomes\": {}}]");
assertMisconfiguration(AS_STRING, "[{\"value\": \"hello\", \"biomes\": 1234}]");
assertMisconfiguration(AS_STRING, "[{\"value\": \"hello\", \"biomes\": \"I am Groot.\"}]");
}
@Test
@DisplayName("Test unknown Biomes")
void testUnknownBiomes() {
assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [1, 2, 3]}]");
assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"hello world\"]}]");
assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"slimefun:slime_forest\"]}]");
assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"minecraft:walshy_desert\"]}]");
}
@Test
@DisplayName("Test double Biome registration")
void testDoubleBiomes() {
Biome biome = Biome.OCEAN;
assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"" + biome.getKey() + "\"]}, {\"value\": 2, \"biomes\": [\"" + biome.getKey() + "\"]}]");
}
@Test
@DisplayName("Test different value converters")
void testValueConversion() {
Biome biome = Biome.OCEAN;
String stringValue = "hello world";
BiomeMap<String> stringMap = createBiomeMap(AS_STRING, "[{\"value\": \"" + stringValue + "\", \"biomes\": [\"" + biome.getKey() + "\"]}]");
Assertions.assertEquals(stringValue, stringMap.get(biome));
int intValue = 1024;
BiomeMap<Integer> intMap = createBiomeMap(AS_INT, "[{\"value\": \"" + intValue + "\", \"biomes\": [\"" + biome.getKey() + "\"]}]");
Assertions.assertEquals(intValue, intMap.get(biome));
}
@Test
@DisplayName("Test working BiomeMap from JSON")
void testBiomeMapFromJson() {
Biome biome1 = Biome.JUNGLE;
Biome biome2 = Biome.OCEAN;
Biome biome3 = Biome.DESERT;
BiomeMap<Integer> biomes = createBiomeMap(AS_INT, "[{\"value\":1,\"biomes\":[\"" + biome1.getKey() + "\", \"" + biome2.getKey() + "\"]}, {\"value\":2, \"biomes\":[\"" + biome3.getKey() + "\"]}]");
Assertions.assertTrue(biomes.contains(biome1));
Assertions.assertEquals(1, biomes.get(biome1));
Assertions.assertTrue(biomes.contains(biome2));
Assertions.assertEquals(1, biomes.get(biome2));
Assertions.assertTrue(biomes.contains(biome3));
Assertions.assertEquals(2, biomes.get(biome3));
}
@Test
@DisplayName("Test working BiomeMap manually")
void testManualBiomeMap() {
BiomeMap<String> biomes = new BiomeMap<>(key);
Biome biome = Biome.OCEAN;
String value = "Under the sea";
Assertions.assertTrue(biomes.put(biome, value));
Assertions.assertTrue(biomes.contains(biome));
Assertions.assertEquals(value, biomes.get(biome));
Assertions.assertTrue(biomes.remove(biome));
Assertions.assertFalse(biomes.contains(biome));
}
@ParametersAreNonnullByDefault
private @Nonnull <T> BiomeMap<T> createBiomeMap(Function<JsonElement, T> function, String json) {
BiomeMapParser<T> parser = new BiomeMapParser<>(key, function);
try {
parser.read(json);
} catch (BiomeMapException e) {
Assertions.fail(e);
}
return parser.buildBiomeMap();
}
@ParametersAreNonnullByDefault
private <T> void assertMisconfiguration(Function<JsonElement, T> function, String json) {
BiomeMapParser<T> parser = new BiomeMapParser<>(key, function);