Adds tests for TypeValidationHelper
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
This commit is contained in:
parent
a79f2e273a
commit
c84c2cf30e
@ -77,7 +77,7 @@ public class BlacksmithPlugin extends JavaPlugin {
|
||||
public void reload() {
|
||||
this.reloadConfig();
|
||||
config.load();
|
||||
translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en"));
|
||||
translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en"), "en");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,7 +122,7 @@ public class BlacksmithPlugin extends JavaPlugin {
|
||||
translator = new Translator();
|
||||
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
|
||||
translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
|
||||
translator.loadLanguages(this.getDataFolder(), fileConfiguration.getString("language", "en"));
|
||||
translator.loadLanguages(this.getDataFolder(), fileConfiguration.getString("language", "en"), "en");
|
||||
BlacksmithPlugin.stringFormatter = new StringFormatter(this.getDescription().getPrefix(), translator);
|
||||
|
||||
//Set up Vault integration
|
||||
|
@ -210,4 +210,5 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
|
||||
public TranslatableMessage[] getAllMessages() {
|
||||
return BlacksmithTranslatableMessage.values();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,9 @@ package net.knarcraft.blacksmith.util;
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.config.SettingValueType;
|
||||
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -33,7 +35,8 @@ public final class TypeValidationHelper {
|
||||
case PERCENTAGE -> isPercentage(value, sender);
|
||||
case BOOLEAN -> true;
|
||||
case STRING_LIST, REFORGE_ABLE_ITEMS -> isStringList(value, sender);
|
||||
case MATERIAL, ENCHANTMENT -> false;
|
||||
case MATERIAL -> isMaterial(value, sender);
|
||||
case ENCHANTMENT -> isEnchantment(value, sender);
|
||||
};
|
||||
} catch (ClassCastException exception) {
|
||||
//This error signifies that an object is not a string, and of the wrong class
|
||||
@ -41,6 +44,40 @@ public final class TypeValidationHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given value is a material
|
||||
*
|
||||
* @param value <p>The value to check</p>
|
||||
* @param sender <p>The command sender to use for printing error messages</p>
|
||||
* @return <p>True if the value is a material</p>
|
||||
*/
|
||||
private static boolean isMaterial(Object value, CommandSender sender) {
|
||||
boolean isMaterial = value instanceof Material || (value instanceof String string &&
|
||||
InputParsingHelper.matchMaterial(string) != null);
|
||||
if (!isMaterial && sender != null) {
|
||||
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
|
||||
BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
|
||||
}
|
||||
return isMaterial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given value is an enchantment
|
||||
*
|
||||
* @param value <p>The value to check</p>
|
||||
* @param sender <p>The command sender to use for printing error messages</p>
|
||||
* @return <p>True if the value is an enchantment</p>
|
||||
*/
|
||||
private static boolean isEnchantment(Object value, CommandSender sender) {
|
||||
boolean isEnchantment = value instanceof Enchantment || (value instanceof String string &&
|
||||
InputParsingHelper.matchEnchantment(string) != null);
|
||||
if (!isEnchantment && sender != null) {
|
||||
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
|
||||
BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
|
||||
}
|
||||
return isEnchantment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given value is a string list
|
||||
*
|
||||
@ -65,16 +102,13 @@ public final class TypeValidationHelper {
|
||||
* @return <p>True if the value is a percentage</p>
|
||||
*/
|
||||
private static boolean isPercentage(Object value, CommandSender sender) {
|
||||
try {
|
||||
int intValue = ConfigHelper.asInt(value);
|
||||
return intValue >= 0 && intValue <= 100;
|
||||
} catch (NumberFormatException | NullPointerException exception) {
|
||||
if (sender != null) {
|
||||
boolean isPercentage = isPositiveInteger(value, null) && ConfigHelper.asInt(value) >= 0 &&
|
||||
ConfigHelper.asInt(value) <= 100;
|
||||
if (!isPercentage && sender != null) {
|
||||
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
|
||||
BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return isPercentage;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,170 @@
|
||||
package net.knarcraft.blacksmith.util;
|
||||
|
||||
import be.seeseemelk.mockbukkit.enchantments.EnchantmentMock;
|
||||
import net.knarcraft.blacksmith.config.SettingValueType;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for the type validator helper
|
||||
*/
|
||||
public class TypeValidatorHelperTest {
|
||||
|
||||
private static Enchantment curseOfBinding;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
//Note: When not testing against a real server, no default enchantments are loaded!
|
||||
curseOfBinding = new EnchantmentMock(NamespacedKey.minecraft("binding_curse"),
|
||||
"CURSE_OF_BINDING");
|
||||
Enchantment.registerEnchantment(curseOfBinding);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidPositiveDoubleValidTest() {
|
||||
SettingValueType type = SettingValueType.POSITIVE_DOUBLE;
|
||||
assertTrue(TypeValidationHelper.isValid(type, 6578d, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, 5346.4534, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "237.4378", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, 74567, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "843552", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidPositiveDoubleInvalidTest() {
|
||||
SettingValueType type = SettingValueType.POSITIVE_DOUBLE;
|
||||
assertFalse(TypeValidationHelper.isValid(type, "potato", null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, 354f, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, SettingValueType.STRING, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, -1, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, -0.45654, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidStringValidTest() {
|
||||
assertTrue(TypeValidationHelper.isValid(SettingValueType.STRING, "potato", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidStringInvalidTest() {
|
||||
SettingValueType type = SettingValueType.STRING;
|
||||
assertFalse(TypeValidationHelper.isValid(type, 5467, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, SettingValueType.POSITIVE_DOUBLE, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, "", null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, " ", null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidPositiveIntegerValidTest() {
|
||||
SettingValueType type = SettingValueType.POSITIVE_INTEGER;
|
||||
assertTrue(TypeValidationHelper.isValid(type, 6574567, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "785768", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidPositiveIntegerInvalidTest() {
|
||||
SettingValueType type = SettingValueType.POSITIVE_INTEGER;
|
||||
assertFalse(TypeValidationHelper.isValid(type, 0.2345, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, -1, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, "potato", null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidPercentageValidTest() {
|
||||
SettingValueType type = SettingValueType.PERCENTAGE;
|
||||
assertTrue(TypeValidationHelper.isValid(type, 33, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, 100, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, 0, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidPercentageInvalidTest() {
|
||||
SettingValueType type = SettingValueType.PERCENTAGE;
|
||||
assertFalse(TypeValidationHelper.isValid(type, -1, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, -73, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, 101, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, 14.4, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, "potato", null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidBooleanValidTest() {
|
||||
SettingValueType type = SettingValueType.BOOLEAN;
|
||||
//Note: As anything invalid will just be parsed to false, anything goes
|
||||
assertTrue(TypeValidationHelper.isValid(type, null, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "potato", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "True", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "False", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "On", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "Off", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidStringListValidTest() {
|
||||
SettingValueType type = SettingValueType.STRING_LIST;
|
||||
List<String> testList = new ArrayList<>();
|
||||
testList.add("fish");
|
||||
testList.add("potato");
|
||||
assertTrue(TypeValidationHelper.isValid(type, "1, 2, 3, 4, 5, 6, 7, 8", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, new ArrayList<>(), null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, testList, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "potato", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, new String[]{"a", "b", "c"}, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidStringListInvalidTest() {
|
||||
SettingValueType type = SettingValueType.STRING_LIST;
|
||||
assertFalse(TypeValidationHelper.isValid(type, null, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, 456, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, -2345.4321, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidMaterialValidTest() {
|
||||
SettingValueType type = SettingValueType.MATERIAL;
|
||||
assertTrue(TypeValidationHelper.isValid(type, Material.POTATO, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "POTATO", null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "minecraft:potato", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidMaterialInvalidTest() {
|
||||
SettingValueType type = SettingValueType.MATERIAL;
|
||||
assertFalse(TypeValidationHelper.isValid(type, null, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, 1, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, "random", null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, -546.678, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, true, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidEnchantmentValidTest() {
|
||||
SettingValueType type = SettingValueType.ENCHANTMENT;
|
||||
assertTrue(TypeValidationHelper.isValid(type, curseOfBinding, null));
|
||||
assertTrue(TypeValidationHelper.isValid(type, "binding_curse", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidEnchantmentInvalidTest() {
|
||||
SettingValueType type = SettingValueType.ENCHANTMENT;
|
||||
assertFalse(TypeValidationHelper.isValid(type, "potato", null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, 7675, null));
|
||||
assertFalse(TypeValidationHelper.isValid(type, null, null));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user