Permalink
Browse files

Register a platform for expression tests

  • Loading branch information...
kenzierocks committed Nov 6, 2017
1 parent 56c8399 commit 2db72a40d6543fc2839fe023c97cde26f5172cd6
@@ -127,34 +127,34 @@ public double evaluate(double... values) throws EvaluationException {
((Variable) invokable).value = values[i];
}
pushInstance();
try {
Future<Double> result = evalThread.submit(new Callable<Double>() {
@Override
public Double call() throws Exception {
Future<Double> result = evalThread.submit(new Callable<Double>() {
@Override
public Double call() throws Exception {
pushInstance();
try {
return root.getValue();
} finally {
popInstance();
}
});
try {
return result.get(WorldEdit.getInstance().getConfiguration().calculationTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof ReturnException) {
return ((ReturnException) cause).getValue();
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new RuntimeException(cause);
} catch (TimeoutException e) {
result.cancel(true);
throw new EvaluationException(-1, "Calculations exceeded time limit.");
}
} finally {
popInstance();
});
try {
return result.get(WorldEdit.getInstance().getConfiguration().calculationTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof ReturnException) {
return ((ReturnException) cause).getValue();
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new RuntimeException(cause);
} catch (TimeoutException e) {
result.cancel(true);
throw new EvaluationException(-1, "Calculations exceeded time limit.");
}
}
@@ -0,0 +1,77 @@
package com.sk89q.worldedit.internal.expression;
import com.google.common.collect.ImmutableMap;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.AbstractPlatform;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.world.World;
import java.util.Map;
final class ExpressionPlatform extends AbstractPlatform {
@Override
public int resolveItem(String name) {
return 0;
}
@Override
public boolean isValidMobType(String type) {
return false;
}
@Override
public void reload() {
}
@Override
public Player matchPlayer(Player player) {
return null;
}
@Override
public World matchWorld(World world) {
return null;
}
@Override
public void registerCommands(Dispatcher dispatcher) {
}
@Override
public void registerGameHooks() {
}
@Override
public LocalConfiguration getConfiguration() {
return new LocalConfiguration() {
@Override
public void load() {
}
};
}
@Override
public String getVersion() {
return "INVALID";
}
@Override
public String getPlatformName() {
return "Expression Test";
}
@Override
public String getPlatformVersion() {
return "INVALID";
}
@Override
public Map<Capability, Preference> getCapabilities() {
return ImmutableMap.of(Capability.CONFIGURATION, Preference.PREFER_OTHERS);
}
}
@@ -19,6 +19,8 @@
package com.sk89q.worldedit.internal.expression;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
import com.sk89q.worldedit.internal.expression.lexer.LexerException;
import com.sk89q.worldedit.internal.expression.parser.ParserException;
import com.sk89q.worldedit.internal.expression.runtime.EvaluationException;
@@ -30,7 +32,15 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
public class ExpressionTest {
@Before
public void setup() {
WorldEdit.getInstance().getPlatformManager().register(new ExpressionPlatform());
WorldEdit.getInstance().getPlatformManager().handlePlatformReady(new PlatformReadyEvent());
}
@Test
public void testEvaluate() throws ExpressionException {
// check

0 comments on commit 2db72a4

Please sign in to comment.