use of com.laytonsmith.abstraction.MCServer in project CommandHelper by EngineHub.
the class StaticTest method GetFakeConsoleCommandSender.
public static MCConsoleCommandSender GetFakeConsoleCommandSender() {
MCConsoleCommandSender c = mock(MCConsoleCommandSender.class);
when(c.getName()).thenReturn("CONSOLE");
MCServer s = GetFakeServer();
when(c.getServer()).thenReturn(s);
return c;
}
use of com.laytonsmith.abstraction.MCServer in project CommandHelper by EngineHub.
the class StaticTest method TestBoilerplate.
/**
* Tests the boilerplate functions in a Function. While all functions should conform to at least this, it is useful
* to also use the more strict TestBoilerplate function.
*
* @param ff
* @param name
* @throws java.lang.Exception
*/
public static void TestBoilerplate(FunctionBase ff, String name) throws Exception {
if (!(ff instanceof Function)) {
return;
}
Function f = (Function) ff;
// For the "quality test code coverage" number, set this to true
boolean runQualityTestsOnly = false;
MCServer fakeServer = StaticTest.GetFakeServer();
MCPlayer fakePlayer = StaticTest.GetOnlinePlayer("Player01", fakeServer);
// make sure that these functions don't throw an exception. Any other results
// are fine
f.isRestricted();
f.runAsync();
f.preResolveVariables();
f.thrown();
// name should match the given value
if (!f.getName().equals(name)) {
fail("Expected name of function to be " + name + ", but was given " + f.getName());
}
// requirement set.
if (f.docs().length() <= 0) {
fail("docs must return a non-empty string");
}
TestDocs(f);
if (f.numArgs().length == 0) {
fail("numArgs must return an Integer array with more than zero values");
}
// If we are interested in tests that are specific to the functions however, we shouldn't run this.
if (!runQualityTestsOnly && f.getClass().getAnnotation(noboilerplate.class) == null) {
TestExec(f, fakePlayer, "fake player");
TestExec(f, null, "null command sender");
TestExec(f, StaticTest.GetFakeConsoleCommandSender(), "fake console command sender");
}
// Same thing for optimize/canOptimize and optimizeDynamic/canOptimizeDynamic
if (f instanceof Optimizable) {
Set<Optimizable.OptimizationOption> options = ((Optimizable) f).optimizationOptions();
if (options.contains(Optimizable.OptimizationOption.CONSTANT_OFFLINE) && options.contains(Optimizable.OptimizationOption.OPTIMIZE_CONSTANT)) {
fail(f.getName() + " declares both CONSTANT_OFFLINE and OPTIMIZE_CONSTANT, which are mutually exclusive.");
}
}
for (Method method : f.getClass().getDeclaredMethods()) {
if (method.getName().equals("execs")) {
if (!f.useSpecialExec()) {
fail(f.getName() + " declares execs, but returns false for useSpecialExec.");
}
}
if (f instanceof Optimizable) {
Set<Optimizable.OptimizationOption> options = ((Optimizable) f).optimizationOptions();
if (method.getName().equals("optimize")) {
if (!options.contains(Optimizable.OptimizationOption.OPTIMIZE_CONSTANT)) {
fail(f.getName() + " declares optimize, but does not declare that it can OPTIMIZE_CONSTANT");
}
}
if (method.getName().equals("optimizeDynamic")) {
if (!options.contains(Optimizable.OptimizationOption.OPTIMIZE_DYNAMIC)) {
fail(f.getName() + " declares optimizeDynamic, but does not declare that it can OPTIMIZE_DYNAMIC");
}
}
}
}
// now the only function left to test is exec. This cannot be abstracted, unfortunately.
}
use of com.laytonsmith.abstraction.MCServer in project CommandHelper by EngineHub.
the class StaticTest method GetFakeServer.
// TODO: Fix this
// public static void RunVars(List<Variable> vars, String script, MCCommandSender player) throws Exception{
// Env env = new Env();
// env.SetCommandSender(player);
// MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null));
// injectAliasCore();
// Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(script, null), env).get(0);
// s.compile();
// s.run(vars, env, null);
//
// }
// Blarg. Dumb thing.
// private static void injectAliasCore() throws Exception{
// PermissionsResolverManager prm = mock(PermissionsResolverManager.class);
// CommandHelperPlugin chp = mock(CommandHelperPlugin.class);
// AliasCore ac = new AliasCore(new File("plugins/CommandHelper/config.txt"),
// new File("plugins/CommandHelper/LocalPackages"),
// new File("plugins/CommandHelper/preferences.ini"),
// new File("plugins/CommandHelper/main.ms"), prm, chp);
// try{
// Field aliasCore = CommandHelperPlugin.class.getDeclaredField("ac");
// aliasCore.setAccessible(true);
// aliasCore.set(null, ac);
// } catch(Exception e){
// throw new RuntimeException("Core could not be injected", e);
// }
// }
/**
* Creates an entire fake server environment, adding players and everything.
*
* @return The fake MCServer
*/
public static MCServer GetFakeServer() {
MCServer fakeServer = mock(MCServer.class);
String[] pnames = new String[] { "player1", "player2", "player3" };
ArrayList<MCPlayer> pps = new ArrayList<MCPlayer>();
for (String p : pnames) {
MCPlayer pp = GetOnlinePlayer(p, fakeServer);
pps.add(pp);
}
when(fakeServer.getOnlinePlayers()).thenReturn(new HashSet<MCPlayer>());
CommandHelperPlugin.myServer = fakeServer;
return fakeServer;
}
use of com.laytonsmith.abstraction.MCServer in project CommandHelper by EngineHub.
the class StaticTest method InstallFakeConvertor.
/**
* Installs the fake convertor into the server, so event based calls will work. Additionally, adds the fakePlayer to
* the server, if player based events are to be called, this is the player returned.
*
* @param fakePlayer
* @throws java.lang.Exception
*/
public static void InstallFakeConvertor(MCPlayer fakePlayer) throws Exception {
InstallFakeServerFrontend();
try {
// We need to add the test directory to the ClassDiscovery path
// This should probably not be hard coded at some point.
ClassDiscovery.getDefaultInstance().addDiscoveryLocation(new File("./target/test-classes").toURI().toURL());
} catch (MalformedURLException ex) {
throw new RuntimeException(ex);
}
Implementation.setServerType(Implementation.Type.TEST);
MCServer fakeServer = GetFakeServer();
TestConvertor.fakeServer = fakeServer;
FakeServerMixin.fakePlayer = fakePlayer;
}
use of com.laytonsmith.abstraction.MCServer in project CommandHelper by EngineHub.
the class RandomTests method testStaticGetLocation.
/*@Test*/
public void testStaticGetLocation() {
MCWorld fakeWorld = mock(MCWorld.class);
MCServer fakeServer = mock(MCServer.class);
when(fakeServer.getWorld("world")).thenReturn(fakeWorld);
CommandHelperPlugin.myServer = fakeServer;
CArray ca1 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3));
CArray ca2 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3), C.onstruct("world"));
CArray ca3 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3), C.onstruct(45), C.onstruct(50));
CArray ca4 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3), C.onstruct("world"), C.onstruct(45), C.onstruct(50));
MCLocation l1 = ObjectGenerator.GetGenerator().location(ca1, fakeWorld, Target.UNKNOWN);
MCLocation l2 = ObjectGenerator.GetGenerator().location(ca2, fakeWorld, Target.UNKNOWN);
MCLocation l3 = ObjectGenerator.GetGenerator().location(ca3, fakeWorld, Target.UNKNOWN);
MCLocation l4 = ObjectGenerator.GetGenerator().location(ca4, fakeWorld, Target.UNKNOWN);
assertEquals(fakeWorld, l1.getWorld());
assertEquals(fakeWorld, l2.getWorld());
assertEquals(fakeWorld, l3.getWorld());
assertEquals(fakeWorld, l4.getWorld());
assertEquals(1, l1.getX(), 0.00000000000000001);
assertEquals(1, l2.getX(), 0.00000000000000001);
assertEquals(1, l4.getX(), 0.00000000000000001);
assertEquals(1, l4.getX(), 0.00000000000000001);
assertEquals(2, l1.getY(), 0.00000000000000001);
assertEquals(2, l2.getY(), 0.00000000000000001);
assertEquals(2, l3.getY(), 0.00000000000000001);
assertEquals(2, l4.getY(), 0.00000000000000001);
assertEquals(3, l1.getZ(), 0.00000000000000001);
assertEquals(3, l2.getZ(), 0.00000000000000001);
assertEquals(3, l3.getZ(), 0.00000000000000001);
assertEquals(3, l4.getZ(), 0.00000000000000001);
assertEquals(0, l1.getYaw(), 0.0000000000000000001);
assertEquals(0, l2.getYaw(), 0.0000000000000000001);
assertEquals(45, l3.getYaw(), 0.0000000000000000001);
assertEquals(45, l4.getYaw(), 0.0000000000000000001);
assertEquals(0, l1.getPitch(), 0.0000000000000000001);
assertEquals(0, l2.getPitch(), 0.0000000000000000001);
assertEquals(50, l3.getPitch(), 0.0000000000000000001);
assertEquals(50, l4.getPitch(), 0.0000000000000000001);
CommandHelperPlugin.myServer = null;
}
Aggregations