Separated tests using a custom test runner

Runs: 6/6 Errors: 0 Faliures: 4
This commit is contained in:
Norbi Peti 2017-06-23 16:49:57 +02:00
parent ad3d3ecaf9
commit 3e0a49659d
2 changed files with 147 additions and 10 deletions

View file

@ -1,10 +1,14 @@
package buttondevteam.chat;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import buttondevteam.chat.ObjectTestRunner.Objects;
import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
import buttondevteam.chat.formatting.ChatFormatter;
import buttondevteam.chat.formatting.TellrawPart;
@ -13,22 +17,43 @@ import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Color;
import junit.framework.TestCase;
@RunWith(ObjectTestRunner.class)
public class ChatFormatTest extends TestCase {
@Test
public void test() {
@Objects
public static List<Object> data() {
TestPrepare.PrepareServer();
final CommandSender sender = Mockito.mock(CommandSender.class);
DebugCommand.DebugMode = true;
testMessage(sender, "*test*", new TellrawPart("test").setItalic(true).setColor(Color.White));
testMessage(sender, "**test**", new TellrawPart("test").setBold(true).setColor(Color.White));
testMessage(sender, "***test***", new TellrawPart("test").setBold(true).setItalic(true).setColor(Color.White));
testMessage(sender, "***_test_***",
new TellrawPart("test").setBold(true).setItalic(true).setUnderlined(true).setColor(Color.White));
testMessage(sender, "***_~~test~~_***", new TellrawPart("test").setBold(true).setItalic(true)
.setUnderlined(true).setStrikethrough(true).setColor(Color.White));
List<Object> list = new ArrayList<Object>();
list.add(new ChatFormatTest(sender, "*test*", new TellrawPart("test").setItalic(true).setColor(Color.White)));
list.add(new ChatFormatTest(sender, "**test**", new TellrawPart("test").setBold(true).setColor(Color.White)));
list.add(new ChatFormatTest(sender, "***test***",
new TellrawPart("test").setBold(true).setItalic(true).setColor(Color.White)));
list.add(new ChatFormatTest(sender, "***_test_***",
new TellrawPart("test").setBold(true).setItalic(true).setUnderlined(true).setColor(Color.White)));
list.add(new ChatFormatTest(sender, "***_~~test~~_***", new TellrawPart("test").setBold(true).setItalic(true)
.setUnderlined(true).setStrikethrough(true).setColor(Color.White)));
list.add(new ChatFormatTest(sender, "¯\\\\\\_(ツ)\\_/¯", new TellrawPart("¯").setColor(Color.White),
new TellrawPart("\\").setColor(Color.White), new TellrawPart("_(ツ)").setColor(Color.White),
new TellrawPart("_/¯").setColor(Color.White)));
return list;
}
void testMessage(final CommandSender sender, final String message, TellrawPart... extras) {
private final CommandSender sender;
private final String message;
private final TellrawPart[] extras;
public ChatFormatTest(CommandSender sender, String message, TellrawPart... extras) {
this.sender = sender;
this.message = message;
this.extras = extras;
}
@Test
public void testMessage() {
ArrayList<ChatFormatter> cfs = ChatProcessing.addFormatters(Color.White);
final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, sender, null);
final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, chid);

View file

@ -0,0 +1,112 @@
package buttondevteam.chat;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.Parameterized;
import org.junit.runners.Suite;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;
/**
* Based on {@link Parameterized}
*
* @author NorbiPeti
*
*/
public class ObjectTestRunner extends Suite {
/**
* Annotation for a method which provides parameters to be injected into the test class constructor by <code>Parameterized</code>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Objects {
}
private class TestClassRunnerForObjects extends BlockJUnit4ClassRunner {
private List<Object> objectList;
private int fParameterSetNumber;
TestClassRunnerForObjects(Class<?> type, List<Object> objectList, int i) throws InitializationError {
super(type);
this.objectList = objectList;
fParameterSetNumber = i;
}
@Override
public Object createTest() throws Exception {
return objectList.get(fParameterSetNumber);
}
@Override
protected String getName() {
return String.format("[%s]", fParameterSetNumber);
}
@Override
protected String testName(final FrameworkMethod method) {
return String.format("%s[%s]", method.getName(), fParameterSetNumber);
}
@Override
protected void validateConstructor(List<Throwable> errors) {
validateOnlyOneConstructor(errors);
}
@Override
protected Statement classBlock(RunNotifier notifier) {
return childrenInvoker(notifier);
}
@Override
protected Annotation[] getRunnerAnnotations() {
return new Annotation[0];
}
}
private final ArrayList<Runner> runners = new ArrayList<Runner>();
/**
* Only called reflectively. Do not use programmatically.
*/
public ObjectTestRunner(Class<?> klass) throws Throwable {
super(klass, Collections.<Runner>emptyList());
List<Object> objectsList = getObjectsList(getTestClass());
for (int i = 0; i < objectsList.size(); i++)
runners.add(new TestClassRunnerForObjects(getTestClass().getJavaClass(), objectsList, i));
}
@Override
protected List<Runner> getChildren() {
return runners;
}
@SuppressWarnings("unchecked")
private List<Object> getObjectsList(TestClass klass) throws Throwable {
return (List<Object>) getObjectsMethod(klass).invokeExplosively(null);
}
public static FrameworkMethod getObjectsMethod(TestClass testClass) throws Exception {
List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Objects.class);
for (FrameworkMethod each : methods) {
int modifiers = each.getMethod().getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
return each;
}
throw new Exception("No public static parameters method on class " + testClass.getName());
}
}