Search in sources :

Example 11 with MCPlayer

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.
}
Also used : Function(com.laytonsmith.core.functions.Function) MCPlayer(com.laytonsmith.abstraction.MCPlayer) Optimizable(com.laytonsmith.core.Optimizable) MCServer(com.laytonsmith.abstraction.MCServer) Method(java.lang.reflect.Method)

Example 12 with MCPlayer

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;
}
Also used : MCPlayer(com.laytonsmith.abstraction.MCPlayer) ArrayList(java.util.ArrayList) MCServer(com.laytonsmith.abstraction.MCServer) CString(com.laytonsmith.core.constructs.CString)

Example 13 with MCPlayer

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;
}
Also used : MCPlayer(com.laytonsmith.abstraction.MCPlayer)

Example 14 with MCPlayer

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;
}
Also used : BukkitMCLocation(com.laytonsmith.abstraction.bukkit.BukkitMCLocation) MCLocation(com.laytonsmith.abstraction.MCLocation) MCItemStack(com.laytonsmith.abstraction.MCItemStack) MCPlayer(com.laytonsmith.abstraction.MCPlayer) MCWorld(com.laytonsmith.abstraction.MCWorld) BukkitMCWorld(com.laytonsmith.abstraction.bukkit.BukkitMCWorld)

Example 15 with MCPlayer

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;
}
Also used : Variable(com.laytonsmith.core.constructs.Variable) MCPlayer(com.laytonsmith.abstraction.MCPlayer) ArrayList(java.util.ArrayList) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) Profiles(com.laytonsmith.core.Profiles) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Script(com.laytonsmith.core.Script) AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) MethodScriptComplete(com.laytonsmith.core.MethodScriptComplete) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) File(java.io.File)

Aggregations

MCPlayer (com.laytonsmith.abstraction.MCPlayer)21 BukkitMCPlayer (com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer)6 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)5 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)4 CString (com.laytonsmith.core.constructs.CString)3 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)3 CREPlayerOfflineException (com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException)3 ArrayList (java.util.ArrayList)3 Player (org.bukkit.entity.Player)3 EventHandler (org.bukkit.event.EventHandler)3 MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)2 MCEntity (com.laytonsmith.abstraction.MCEntity)2 MCServer (com.laytonsmith.abstraction.MCServer)2 Environment (com.laytonsmith.core.environments.Environment)2 AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)2 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 MCCommand (com.laytonsmith.abstraction.MCCommand)1 MCConsoleCommandSender (com.laytonsmith.abstraction.MCConsoleCommandSender)1 MCItemStack (com.laytonsmith.abstraction.MCItemStack)1