Start adding API tests

This commit is contained in:
James Seibel
2022-06-06 22:25:33 -05:00
parent 2410ad9f23
commit 8c31236ccd
3 changed files with 224 additions and 0 deletions
@@ -0,0 +1,34 @@
/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2022 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.seibel.lod.core.api.external.apiObjects.enums;
/**
*
* @author Leonardo Amato
* @version 2022-3-26
*/
public enum DhApiVerticalQuality
{
LOW,
MEDIUM,
HIGH,
ULTRA
}
@@ -0,0 +1,118 @@
package com.seibel.lod.core.util;
import java.io.InvalidObjectException;
/**
* Methods related to handling and using enums.
*
* @author James Seibel
* @version 2022-6-6
*/
public class EnumUtil
{
/**
* Attempts to find the enum value with the given name
* (ignoring case).
*
* @param enumType The class of the enum of parse
* @param enumName the string value to parse
* @param <T> The Enum type to parse
* @throws InvalidObjectException if no enum exists with the given enumName
*/
public static <T extends Enum<T>> T parseEnumIgnoreCase(Class<T> enumType, String enumName) throws InvalidObjectException
{
// attempt to find an enum with enumName
for (T enumValue : enumType.getEnumConstants())
{
if (enumValue.name().equalsIgnoreCase(enumName))
{
return enumValue;
}
}
// no enum found
throw new InvalidObjectException("No Enum of type [" + enumType.getSimpleName() + "] exists with the name [" + enumName + "]. Possible enum values are: [" + createEnumCsv(enumType) + "]" );
}
/**
* Returns a comma separated list of all possible enum values
* for the given enumType. <Br><Br>
*
* Example output: <Br>
* "NEAR, FAR, NEAR_AND_FAR"
*/
public static <T extends Enum<T>> String createEnumCsv(Class<T> enumType)
{
StringBuilder str = new StringBuilder();
T[] enumValues = enumType.getEnumConstants();
for (int i = 0; i < enumValues.length; i++)
{
if (i == 0)
{
// the first value doesn't need a comma
str.append(enumValues[i].name());
}
else
{
str.append(", ").append(enumValues[i].name());
}
}
return str.toString();
}
/** Returns true if both enums contain the same values. */
public static <Ta extends Enum<Ta>, Tb extends Enum<Tb>> EnumComparisonResult compareEnumsByValues(Class<Ta> alphaEnum, Class<Tb> betaEnum)
{
Ta[] alphaValues = alphaEnum.getEnumConstants();
Tb[] betaValues = betaEnum.getEnumConstants();
// compare the number of enum values
if (alphaValues.length != betaValues.length)
{
return new EnumComparisonResult(false, createFailMessageHeader(alphaEnum, betaEnum) + "the enums have [" + alphaValues.length + "] and [" + betaValues.length + "] values respectively.");
}
// check that each value exists in both enums
for(Ta alphaVal : alphaValues)
{
boolean valueFoundInBothEnums = false;
for(Tb betaVal : betaValues)
{
if (alphaVal.name().equals(betaVal.name()))
{
valueFoundInBothEnums = true;
break;
}
}
if (!valueFoundInBothEnums)
{
// an enum value wasn't found
return new EnumComparisonResult(false, createFailMessageHeader(alphaEnum, betaEnum) + "the enum value [" + alphaVal.name() + "] wasn't found in [" + betaEnum.getSimpleName() + "].");
}
}
// every enum value is the same
return new EnumComparisonResult(true, "");
}
/** helper method */
public static <Ta extends Enum<Ta>, Tb extends Enum<Tb>> String createFailMessageHeader(Class<Ta> alphaEnum, Class<Tb> betaEnum)
{
return "The enums [" + alphaEnum.getSimpleName() + "] and [" + betaEnum.getSimpleName() + "] aren't equal: ";
}
/** helper object */
public static class EnumComparisonResult
{
public final boolean success;
public final String failMessage;
public EnumComparisonResult(boolean newSuccess, String newFailMessage)
{
this.success = newSuccess;
this.failMessage = newFailMessage;
}
}
}
+72
View File
@@ -0,0 +1,72 @@
import com.seibel.lod.core.api.external.apiObjects.enums.DhApiFogDistance;
import com.seibel.lod.core.api.external.apiObjects.enums.DhApiVerticalQuality;
import com.seibel.lod.core.enums.config.VerticalQuality;
import com.seibel.lod.core.util.EnumUtil;
import org.junit.Test;
import org.junit.Assert;
/**
* These tests were primary created to confirm that the
* API enums are properly synced with their Core variants.
*
*
* @author James Seibel
* @version 2022-6-6
*/
public class ApiEnumSyncTests
{
/** This is just a quick example to confirm the testing system is set up correctly. */
@Test
public void ExampleTests()
{
Assert.assertTrue("Example test 1", true);
Assert.assertFalse("Example test 2", false);
}
/** Make sure each DhApi enum has the same values as its corresponding core enum. */
@Test
public void ConfirmEnumsAreSynced()
{
//================================//
// base case tests to make sure //
// the tests are set up correctly //
//================================//
// this should always succeed (comparing the same enum to itself)
AssertEnumsValuesAreEqual(VerticalQuality.class, VerticalQuality.class, true);
// this should always fail (two completely different enums)
AssertEnumsValuesAreEqual(VerticalQuality.class, DhApiFogDistance.class, false);
//=========================//
// actual enum comparisons //
//=========================//
// TODO using reflection I should be able to automatically find and compare each enum in the Api to its corresponding Core object
AssertEnumsValuesAreEqual(VerticalQuality.class, DhApiVerticalQuality.class);
}
private <Ta extends Enum<Ta>, Tb extends Enum<Tb>> void AssertEnumsValuesAreEqual(Class<Ta> alphaEnum, Class<Tb> betaEnum)
{
AssertEnumsValuesAreEqual(alphaEnum, betaEnum, true);
}
private <Ta extends Enum<Ta>, Tb extends Enum<Tb>> void AssertEnumsValuesAreEqual(Class<Ta> alphaEnum, Class<Tb> betaEnum, boolean shouldBeEqual)
{
EnumUtil.EnumComparisonResult comparisonResult = EnumUtil.compareEnumsByValues(alphaEnum, betaEnum);
if (shouldBeEqual)
{
Assert.assertTrue(comparisonResult.failMessage, comparisonResult.success);
}
else
{
Assert.assertFalse(comparisonResult.failMessage, comparisonResult.success);
}
}
}