Commit 3171cde0 authored by primetoxinz's avatar primetoxinz

Work on unit testing. Kinda weird.

parent 3804797b
......@@ -47,6 +47,9 @@ dependencies {
deobfCompile "team.chisel.ctm:CTM:+"
compile "MCMultiPart2:MCMultiPart:2.4.1"
runtime "MCMultiPart2:MCMultiPart:2.4.1"
compile 'org.easytesting:fest-assert:1.4'
compile 'org.easytesting:fest-util:1.1.6'
}
......
......@@ -10,6 +10,7 @@ import betterwithmods.network.MessageGuiShake;
import betterwithmods.network.MessageHarnessSync;
import betterwithmods.network.NetworkHandler;
import betterwithmods.proxy.IProxy;
import betterwithmods.testing.BWMTests;
import net.minecraftforge.common.ForgeModContainer;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.SidedProxy;
......@@ -66,6 +67,7 @@ public class BWMod {
proxy.postInit(evt);
BWRegistry.postPostInit();
}
@Mod.EventHandler
......@@ -76,6 +78,9 @@ public class BWMod {
@Mod.EventHandler
public void serverStarting(FMLServerStartingEvent evt) {
ModuleLoader.serverStarting(evt);
if(isDev()) {
BWMTests.runTests();
}
}
@Mod.EventHandler
......@@ -83,4 +88,8 @@ public class BWMod {
FakePlayerHandler.setPlayer(null);
FakePlayerHandler.setCreativePlayer(null);
}
public static boolean isDev() {
return BWMod.VERSION.equalsIgnoreCase("${version}");
}
}
package betterwithmods.testing;
import betterwithmods.testing.base.BaseTest;
import com.google.common.collect.Lists;
import java.util.List;
public class BWMTests {
private static final List<Class<? extends BaseTest>> TESTS = Lists.newArrayList();
static {
TESTS.add(CookingPotTests.class);
}
private static void runTest(Class<? extends BaseTest> clazz) {
try {
clazz.newInstance().run();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void runTests() {
for (Class<? extends BaseTest> clazz : TESTS) {
runTest(clazz);
}
}
}
package betterwithmods.testing;
import betterwithmods.common.registry.bulk.manager.CookingPotManager;
import betterwithmods.common.registry.bulk.recipes.CookingPotRecipe;
import betterwithmods.testing.base.BaseTest;
import betterwithmods.testing.base.Before;
import betterwithmods.testing.base.Test;
import com.google.common.collect.Lists;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import org.fest.assertions.Assertions;
import java.util.List;
public class CookingPotTests extends BaseTest {
private CookingPotManager TEST_MANAGER;
private List<Ingredient> inputs = Lists.newArrayList(Ingredient.fromStacks(new ItemStack(Blocks.COBBLESTONE)));
private List<ItemStack> outputs = Lists.newArrayList(new ItemStack(Items.DIAMOND));
private CookingPotRecipe recipe = new CookingPotRecipe(inputs, outputs, 1);
@Before
public void beforeTest() {
TEST_MANAGER = new CookingPotManager();
}
@Test
public void testRecipeAddition() {
Assertions.assertThat(TEST_MANAGER.getRecipes()).isEmpty();
TEST_MANAGER.addRecipe(recipe);
Assertions.assertThat(TEST_MANAGER.getRecipes()).hasSize(1);
}
@Test
public void testRecipeRemoval() {
Assertions.assertThat(recipe.isInvalid()).isFalse();
Assertions.assertThat(TEST_MANAGER.getRecipes()).isEmpty();
TEST_MANAGER.addRecipe(recipe);
Assertions.assertThat(TEST_MANAGER.getRecipes()).isNotEmpty();
TEST_MANAGER.remove(recipe);
}
}
package betterwithmods.testing.base;
import betterwithmods.BWMod;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseTest {
private void before() {
Class<? extends BaseTest> clazz = this.getClass();
Method[] methods = clazz.getMethods();
for(Method method: methods) {
if(method.isAnnotationPresent(Before.class)) {
try {
method.invoke(this);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
public void run() {
int passed = 0, failed = 0, count = 0;
Class<? extends BaseTest> clazz = this.getClass();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Test.class)) {
before();
try {
method.invoke(this);
BWMod.logger.warn("{} - Test '{}' - passed", ++count, method.getName());
passed++;
} catch (Throwable ex) {
BWMod.logger.error("{} - Test '{}' - failed: {}", ++count, method.getName(), ex.getCause());
failed++;
}
}
}
BWMod.logger.warn("Result {} : Total : {}, Passed: {}, Failed {}", clazz.getSimpleName(), count, passed, failed);
}
}
package betterwithmods.testing.base;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Before {}
package betterwithmods.testing.base;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment