use of com.laytonsmith.abstraction.MCPlayer 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.MCPlayer 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.MCPlayer in project CommandHelper by EngineHub.
the class StaticTest method GetOp.
public static MCPlayer GetOp(String name, MCServer s) {
MCPlayer p = GetOnlinePlayer(name, s);
when(p.isOp()).thenReturn(true);
return p;
}
use of com.laytonsmith.abstraction.MCPlayer in project CommandHelper by EngineHub.
the class StaticTest method GetOnlinePlayer.
public static MCPlayer GetOnlinePlayer(String name, String worldName, MCServer s) {
MCPlayer p = mock(MCPlayer.class);
MCWorld w = mock(MCWorld.class);
MCLocation fakeLocation = StaticTest.GetFakeLocation(w, 0, 0, 0);
MCItemStack fakeItemStack = mock(MCItemStack.class);
when(w.getName()).thenReturn(worldName);
when(p.getWorld()).thenReturn(w);
when(p.isOnline()).thenReturn(true);
when(p.getName()).thenReturn(name);
when(p.getServer()).thenReturn(s);
when(p.isOp()).thenReturn(true);
if (s != null && s.getOnlinePlayers() != null) {
Collection<MCPlayer> online = s.getOnlinePlayers();
boolean alreadyOnline = false;
for (MCPlayer o : online) {
if (o.getName().equals(name)) {
alreadyOnline = true;
break;
}
}
if (!alreadyOnline) {
online.add(p);
when(s.getOnlinePlayers()).thenReturn(new HashSet<MCPlayer>());
}
}
// Plethora of fake data
when(p.getCompassTarget()).thenReturn(fakeLocation);
when(p.getItemAt((Integer) Mockito.any())).thenReturn(fakeItemStack);
return p;
}
use of com.laytonsmith.abstraction.MCPlayer in project CommandHelper by EngineHub.
the class ExampleScript method getOutput.
public String getOutput() throws IOException, DataSourceException, URISyntaxException {
if (output != null) {
return output;
}
Script s = Script.GenerateScript(script, Static.GLOBAL_PERMISSION);
Environment env;
try {
env = Static.GenerateStandaloneEnvironment();
} catch (Profiles.InvalidProfileException ex) {
throw new RuntimeException(ex);
}
Class[] interfaces = new Class[] { MCPlayer.class };
MCPlayer p = (MCPlayer) Proxy.newProxyInstance(ExampleScript.class.getClassLoader(), interfaces, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("getName") || method.getName().equals("getDisplayName")) {
return "Player";
}
if (method.getName().equals("sendMessage")) {
playerOutput.append(args[0].toString()).append("\n");
}
if (method.getName().equals("isOnline")) {
return true;
}
return genericReturn(method.getReturnType());
}
});
// TODO: Remove this dependency. Make MCPlayer implement a generic "User" and make that
// part of the GlobalEnv.
env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
final StringBuilder finalOutput = new StringBuilder();
String thrown = null;
try {
List<Variable> vars = new ArrayList<>();
try {
MethodScriptCompiler.execute(originalScript, new File("/" + functionName + ".ms"), true, env, new MethodScriptComplete() {
@Override
public void done(String output) {
if (output != null) {
finalOutput.append(output);
}
}
}, null, vars);
} catch (ConfigCompileException | ConfigCompileGroupException ex) {
// We already checked for compile errors, so this won't happen
}
} catch (ConfigRuntimeException e) {
String name = e.getClass().getName();
if (e instanceof AbstractCREException) {
name = ((AbstractCREException) e).getName();
}
thrown = "\n(Throws " + name + ": " + e.getMessage() + ")";
}
String playerOut = playerOutput.toString().trim();
String finalOut = finalOutput.toString().trim();
String out = (playerOut.isEmpty() ? "" : playerOut) + (finalOut.isEmpty() || !playerOut.trim().isEmpty() ? "" : ":" + finalOut);
if (thrown != null) {
out += thrown;
}
return out;
}
Aggregations